js 适用于多浏览器的弹出窗口

最近由于项目需要用到Js 操作弹出窗口显示,结合网络中的资源写了如下代码,如有不足,欢迎大家指点。

 

///创建弹出DIV层
function openDiv()
{
    oPopup = _createPopup();   
    oPopupBody = oPopup.document.body;
    
    if(oPopupBody.addEventListener) //Firefox
    {
        oPopupBody.addEventListener('mouseout',hidePop,false);
        oPopupBody.addEventListener('mouseover',clrPopTimer,false);
    }
    else //IE
    {
        oPopupBody.attachEvent('onmouseout',hidePop);
        oPopupBody.attachEvent('onmouseover',clrPopTimer);    
    }
}

function hidePop(){
    if (!oPopup.isOpen) {
        oPopTimer = setTimeout('oPopup.hide()',10);                       
    }
}

function clrPopTimer(){
    clearTimeout(oPopTimer);
}

/*** 弹出窗口 ****/
var isIe = (document.all) ? true:false;
var eDiv = document.createElement('div'); //显示弹出窗口的图层
  
var _createPopup = function() { 
        var SetElementStyles = function(element, styleDict) { 
            var style = element.style; 
            for (var styleName in styleDict) {
                style[styleName] = styleDict[styleName] ;  
            }
        }         
       
        SetElementStyles( eDiv, { 'position': 'absolute', 'top': 0 + 'px', 'left': 0 + 'px', 'width': 0 + 'px', 'height': 0 + 'px', 'zIndex': 1000, 'display' : 'none', 'overflow' : 'hidden' } ) ; 
        eDiv.body = eDiv; 
        var opened = false; 
        var setOpened = function(b) { 
            opened = b;  
        }
         
        var getOpened = function() { 
            return opened ;  
        }
         
        var getCoordinates = function(oElement) { 
            var coordinates = {x:0,y:0};  
            while(oElement) { 
                coordinates.x += oElement.offsetTop ; 
                coordinates.y += oElement.offsetLeft; 
                oElement = oElement.offsetParent; 
            } 
            return coordinates; 
        }
         
        return {
                 htmlTxt : '',
                 document : eDiv,
                 isOpen : getOpened(),
                 isShow : false,
                 hide : function() {
                            SetElementStyles( eDiv, { 'top': 0 + 'px', 'left': 0 + 'px', 'width': 0 + 'px', 'height': 0 + 'px', 'display' : 'none' } );
                            eDiv.innerHTML = '';
                            this.isShow = false;
                        },
                 show : function(iX, iY, iWidth, iHeight, oElement) {
                            if (!getOpened())
                            {
                                $(document.body).append(eDiv);
                                setOpened(true);
                            };
                            
                            this.htmlTxt = eDiv.innerHTML;
                            if (this.isShow)
                            {
                                this.hide();
                            };
                           
                            eDiv.innerHTML = this.htmlTxt;
                            var coordinates = getCoordinates(oElement);
                            eDiv.style.top = (iX + coordinates.x) + 'px' ;
                            eDiv.style.left = (iY + coordinates.y) + 'px';
                            eDiv.style.width = iWidth + 'px';
                            eDiv.style.height = iHeight + 'px';          
                            eDiv.style.display = 'block';
                            this.isShow = true;
                        }
                } 
    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值