VML简单封装

兼容说明

兼容IE6-IE8

封装代码

(function(win, doc){
    win.vmlEnable = (function () {return function () {return !+"\v1";}})();
    if (!win.vmlEnable() || win.sunuiVml) {return;}
    if (!doc.namespaces["sunui-vml"]){
        doc.createStyleSheet().addRule(".sunui-vml", "behavior:url(#default#VML);display:inline-block;");
        doc.namespaces.add("sunui-vml", "urn:schemas-microsoft-com:vml");
    }
    win.sunuiVml = function(name){
        return new SunuiVml().create(name || "rect");
    };
    function SunuiVml () {}
    SunuiVml.prototype = {
        create : function(name){
            this.node = doc.createElement('<sunui-vml:' + name + ' class="sunui-vml">');
            return this;
        },
        appendTo: function(parent){
            if(this.node && parent.nodeType === 1){
                parent.appendChild(this.node);
            }
            return this;
        },
        attr : function(bag){
            for(var i in bag){
                if(bag.hasOwnProperty(i)){
                    this.node.setAttribute(i,bag[i])
                }
            }
            return this;
        },
        css: function(bag){
            for(var i in bag){
                if(bag.hasOwnProperty(i)) {
                    if (i === 'opacity') {
                        this.node.style.filter = 'alpha(opacity='+ bag[i] * 100 + ')';
                    } else {
                        this.node.style[i] = bag[i];
                    }
                }
            }
            return this;
        },
        hasClass: function (cls) {
            cls = cls || '';
            if ((cls = cls.replace(/\s/g, '')).length === 0) return false;
            return new win.RegExp(' ' + cls + ' ').test(' ' + (this.node.className || '').replace(/[\t\r\n]/g, '') + ' ');
        },
        addClass: function (cls) {
            var clss = null;
            var clazz = null;
            if (typeof cls === 'function') {
                cls = cls();
            }
            if (!cls) {return this}
            cls = cls.replace(/(^\s*)|(\s*$)/g, '');
            if (!cls) {return this}
            clss = cls.split(/\s+/g);
            clazz = (this.node.className || '').replace(/(^\s*)|(\s*$)/g, '').replace(/[\t\r\n]/g, '').split(/\s+/g);
            for (var i = 0; i < clss.length; i++) {
                if (!this.hasClass(clss[i])) {
                    clazz.push(clss[i])
                }
            }
            this.node.className = clazz.join(" ");
            return this;
        },
        removeClass: function (cls) {
            var clss = null;
            var clazz = '';
            if (typeof cls === 'function') {
                cls = cls();
            }
            if (!cls) {return this}
            cls = cls.replace(/(^\s*)|(\s*$)/g, '');
            if (!cls) {return this}
            clss = cls.split(/\s+/g);
            clazz = ' ' + this.node.className.replace(/[\t\r\n]/g, '') + ' ';
            for (var i = 0; i < clss.length; i++) {
                if (this.hasClass(clss[i])) {
                    while (clazz.indexOf(' ' + clss[i] + ' ') >= 0) {
                        clazz = clazz.replace(' ' + clss[i] + ' ', ' ');
                    }
                }
            }
            clazz = clazz.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
            this.node.className = clazz;
            return this;
        }
    }
})(window, document);

使用示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>IE6-IE8 VML</title>
</head>
<body style="background: #Fc0e4c">
    <div id="dd" style="position: relative;width: 200px;height: 100px;border: 1px solid #35FF87"></div>
</body>
</html>
window.onload = function () {
    if (vmlEnable()) {
        var re = sunuiVml("roundrect").css({width:"200px",height:"100px", margin: '-1px 0 0 -1px'})
            .attr({fillcolor:"#FFFF00", filled: true, stroked: true, strokeweight: '1px'})
            .appendTo(document.getElementById('dd'))
            .css({height: '120px'});
        re.css({rotation: 30});
        re.node.innerHTML = '<div style="width: 100px;height: 30px;background: #FF00FF;">测试</div>';

        sunuiVml("oval").css({position:"relative",left:'5px',top:'20px',
            width:"12px",height:'12px'}).attr({strokecolor:"blue", filled: false, strokeweight: '1px'}).appendTo(document.body);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值