Web 前端开发 技术干货让前端飞

构建兼容IE8的可视化项目

2018-01-08  本文已影响0人  金字笙调

在一些可视化项目需求中,常常要求我们兼容到ie8,这里就简单介绍一下怎么在可视化项目里处理ie8的兼容问题。

项目使用的技术

ie8有哪些不兼容

解决方案

在ie8下使用 react

配置项如下

(进行代码拆分)

var es3ifyPlugin = require('es3ify-webpack-plugin');
module.exports = {
 ...
 entry: {
        main: ['babel-polyfill', './src/index.jsx'],
        vendor: ['es5-shim', 'es5-shim/es5-sham', 'console-polyfill']
 },
 plugins: [
       new es3ifyPlugin(),
       ...
 ],
 ...
}

或(不进行拆分)


var es3ifyPlugin = require('es3ify-webpack-plugin');
module.exports = {
 ...
 entry: [
    'es5-shim',
    'es5-shim/es5-sham',
    'console-polyfill',  
    'babel-polyfill', 
    './src/index'
 ],
 plugins: [
       new es3ifyPlugin(),
       ...
 ],
 ...
}

不支持indexOf trim

手动加入一下代码,添加在模板index.html中

if (!Array.prototype.indexOf){
        Array.prototype.indexOf = function(elt /*, from*/)
        {
            var len = this.length >>> 0;
            var from = Number(arguments[1]) || 0;
            from = (from < 0)
                ? Math.ceil(from)
                : Math.floor(from);
            if (from < 0)
            from += len;
            for (; from < len; from++)
            {
            if (from in this &&
                this[from] === elt)
                return from;
            }
            return -1;
        };
        if (!String.prototype.trim) {
            String.prototype.trim = function () {
                return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
            };
        }
}

不支持中文命名文件路径

解决方法: 使用encodeURL

mapUrl = `/static/data/map/${encodeURI(province)}.json`;

不支持渐变

progid:DXImageTransform.Microsoft.gradient(startColorstr='#B2FFFFFF', endColorstr='#00FFFFFF',GradientType=0 );

其格式为 #AARRGGBB 。 AA 、 RR 、 GG 、 BB 为十六进制正整数。取值范围为 00 - FF 。 RR 指定红色值, GG 指定绿色值, BB 指定蓝色值,参阅 #RRGGBB 颜色单位。 AA 指定透明度。 00 是完全透明。 FF 是完全不透明。超出取值范围的值将被恢复为默认值。

不支持透明度

filter: alpha(opacity=40);

不支持border-radius

解决方法: 使用pie.js

下载并引入pie.js, pie.js的内容如下。


image.png

在样式中, 引入pie.htc即可

{
  border-radius: 10px;
   behavior: url(/static/plugins/pie/PIE.htc);  /** 引入pie.htc
}

不支持 input placeholder属性

解决方案: js模拟 placeholder

react Input组件代码示例

import styles from './index.scss';
import React, { Component } from 'react';
import classNames from 'classnames';

class Input extends Component {
    constructor(props) {
        super(props);
        this.supportPlaceholder = '';
    }

    componentDidMount() {
        this.props.callback && this.props.callback();
        var supportPlaceholder = 'placeholder' in document.createElement('input');
        this.supportPlaceholder = supportPlaceholder;
        this.placeholder();
    }

    componentDidUpdate(){

        if($(this.textInput).val() || document.activeElement === this.textInput){
            return false;
        }
        this.placeholder();
        

    placeholder = () => {
        let $self = $(this.textInput);
        if(!this.supportPlaceholder && $self.attr('type') === 'text'){        
            
            let defaultValue = this.props.defaultValue;
            const value = this.props.value;
            const inputText = value ? value : defaultValue ? defaultValue : this.props.placeholder;
            if(!defaultValue){
                $self.val(inputText);
            }           
        }
    }

    focus = () =>{
        if($(this.textInput).val() === this.props.placeholder){
            $(this.textInput).val('');
        }
    }

    blur = () => {
        if($(this.textInput).val() === ''){
            $(this.textInput).val(this.props.placeholder).addClass('phcolor');
        }
    }

    keyDown = () => {
        $(this.textInput).removeClass('phcolor');
    }

    render() {
        const { supportPlaceholder } = this;
        let {className, style, ...other} = this.props;
        return (
            <input 
                type="text"
                className={classNames('input', 'border_color_gray', 'synBlockBg', className)}
                {...other}
                style={style}
                onFocus={() => !supportPlaceholder && this.focus()}
                onBlur={() => !supportPlaceholder && this.blur()}
                // onKeyDown={() => !supportPlaceholder && this.keyDown()}
                ref={(input) => { this.textInput = input; }} 
            />
        );
    }
}

export default Input;

不支持 svg canvas绘图元素

如图,绘制连线

示例

正常情况下,使用svg 或者 canvas 很容易实现,但是由于ie8不支持,我们只能选用其它方式。

解决方法:使用vml 元素。
在这里 我们使用了 zrender这个库。 ( "zrender": "3.6.1" )

import zrender from 'zrender';
import Line from 'zrender/lib/graphic/shape/Line';
import 'zrender/lib/vml/vml.js'; // ie8

具体代码实现方式,请看点击这里 zrender.js

谢谢阅览,欢迎指教。

上一篇 下一篇

猜你喜欢

热点阅读