javascript 自定义dialog

/*
    cWidth : 弹窗宽度
    cHeight : 弹窗高度
    title : 弹窗标题
    info : 弹窗内容(原生DOM对象)
*/
function MarkBox(cWidth, cHeight, title, info) {
    var reg = /^[0-9]*[1-9][0-9]*$/;
    this.title = title;
    this.mid = "markId";
    this.cid = "contentId";
    this.info = info;
    this.w = reg.test(cWidth) ? cWidth : 300;
    this.h = reg.test(cHeight) ? cHeight : 200;
}

MarkBox.prototype = {
    close: function() {
        document.getElementById("closeId").onclick();
    },
    init: function() {
        this.de = document.documentElement ? document.documentElement : document.body;
        // var flag = document.getElementById("closeId");
        // if(!flag){
        this.createMarkBox(this.w, this.h);
        // }
        var mark = document.getElementById(this.mid);
        var content = document.getElementById(this.cid);
        mark.style.display = 'block';
        content.style.display = 'block';
        this.resetOver(mark, content, this.w, this.h);

        var _this = this;
        document.getElementById("closeId").onclick = function() {
            mark.style.display = 'none';
            content.style.display = 'none';
        }

        window.onresize = function() {
            _this.resetOver(mark, content, _this.w, _this.h);
        }
        window.onscroll = function() {
            _this.resetOver(mark, content, _this.w, _this.h);
        }
    },
    resetCss: function(element, props) {
        for (var p in props) {
            element.style[p] = props[p];
        }
    },
    createMarkBox: function(w, h) {
        var flag = document.getElementById(this.cid);
        if (flag) {
            var ot = flag.children[0];
            var oc = flag.children[1];
            if(oc != this.info){
                oc.style.display = 'none';
                document.body.appendChild(oc);
                this.info.style.display = 'block';
                flag.appendChild(this.info);
            }else{
                // oc.style.display = 'block';
            }
        } else {
            var mark = document.createElement("div");
            mark.id = this.mid;
            document.body.appendChild(mark);

            var content = document.createElement("div");
            var html = "<div style='line-height:40px;height:40px;padding-left:10px;width:" + w + ";background:#76A6F5;'><span>" + this.title + "</span><span id='closeId' style='float:right;color:red;line-height:40px;margin-right:5px;cursor:pointer'>关闭</span></div>";
            content.innerHTML = html;
            content.id = this.cid;
            this.info.style.display = 'block';
            content.appendChild(this.info);
            document.body.appendChild(content);
            this.resetOver(mark, content, w, h);
        }
    },
    resetOver: function(mark, content, w, h) {
        //重设遮罩层
        this.resetCss(mark, {
            position: "absolute",
            zIndex: "998",
            top: "0px",
            left: "0px",
            backgroundColor: "gray",
            opacity: "0.3",
            // display:"block",
            filter: "alpha(opacity=30)",
            width: "100%",
            height: "100%"
        });
        //重设内容
        var eh = this.de.clientHeight - h;
        var ew = this.de.clientWidth - w;
        eh = eh < 0 ? 0 : eh;
        ew = ew < 0 ? 0 : ew;
        this.resetCss(content, {
            position: "absolute",
            zIndex: "999",
            width: w + "px",
            height: h + "px",
            // display: "block",
            background: "lightgreen",
            left: parseInt(ew / 2) + "px",
            top: parseInt(eh / 2) + "px",
            overFlow: "hidden"
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值