天天看點

利用HTML中的DIV層做的消息視窗

由于JS裡自帶的alert()消息提示不太友好,是以可以通過一個DIV層來模拟alert資訊框,模拟的消息框IE,FF;裡都适用,代碼如下:

test.html:

利用HTML中的DIV層做的消息視窗

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

利用HTML中的DIV層做的消息視窗

< html  xmlns ="http://www.w3.org/1999/xhtml" >

利用HTML中的DIV層做的消息視窗

< head >

利用HTML中的DIV層做的消息視窗

< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />

利用HTML中的DIV層做的消息視窗

< title > 消息視窗測試 </ title >

利用HTML中的DIV層做的消息視窗

< script  language ="javascript"  src ="win.js"  type ="text/javascript" ></ script >

利用HTML中的DIV層做的消息視窗

</ head >

利用HTML中的DIV層做的消息視窗

< body >

利用HTML中的DIV層做的消息視窗

< input  type ="button"  value ="點這裡"  onclick ="alertWin('标題','這裡是内容',300,200)"   />

利用HTML中的DIV層做的消息視窗

</ body >

利用HTML中的DIV層做的消息視窗

</ html >

 win.js:

利用HTML中的DIV層做的消息視窗

function  alertWin(title, msg, w, h)

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

... { 

利用HTML中的DIV層做的消息視窗

    var titleheight = "22px"; // 提示視窗标題高度 

利用HTML中的DIV層做的消息視窗

    var bordercolor = "#666699"; // 提示視窗的邊框顔色 

利用HTML中的DIV層做的消息視窗

    var titlecolor = "#FFFFFF"; // 提示視窗的标題顔色 

利用HTML中的DIV層做的消息視窗

    var titlebgcolor = "#666699"; // 提示視窗的标題背景色

利用HTML中的DIV層做的消息視窗

    var bgcolor = "#FFFFFF"; // 提示内容的背景色

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    var iWidth = document.documentElement.clientWidth; 

利用HTML中的DIV層做的消息視窗

    var iHeight = document.documentElement.clientHeight; 

利用HTML中的DIV層做的消息視窗

    var bgObj = document.createElement("div"); //添加背景,在原頁面上面蓋上一層DIV,作用是禁止使用者與視窗後的控件互動

利用HTML中的DIV層做的消息視窗

    bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";

利用HTML中的DIV層做的消息視窗

    document.body.appendChild(bgObj); 

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    var msgObj=document.createElement("div");//添加提示視窗

利用HTML中的DIV層做的消息視窗

    msgObj.style.cssText = "position:absolute;font:11px '宋體';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;";

利用HTML中的DIV層做的消息視窗

    document.body.appendChild(msgObj);

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    var table = document.createElement("table");

利用HTML中的DIV層做的消息視窗

    msgObj.appendChild(table);

利用HTML中的DIV層做的消息視窗

    table.style.cssText = "margin:0px;border:0px;padding:0px;";

利用HTML中的DIV層做的消息視窗

    table.cellSpacing = 0;

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    var tr = table.insertRow(-1);

利用HTML中的DIV層做的消息視窗

    var titleBar = tr.insertCell(-1);//添加視窗标題欄

利用HTML中的DIV層做的消息視窗

    titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋體';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor;

利用HTML中的DIV層做的消息視窗

    titleBar.style.paddingLeft = "10px";

利用HTML中的DIV層做的消息視窗

    titleBar.innerHTML = title;

利用HTML中的DIV層做的消息視窗

    var moveX = 0;

利用HTML中的DIV層做的消息視窗

    var moveY = 0;

利用HTML中的DIV層做的消息視窗

    var moveTop = 0;

利用HTML中的DIV層做的消息視窗

    var moveLeft = 0;

利用HTML中的DIV層做的消息視窗

    var moveable = false;

利用HTML中的DIV層做的消息視窗

    var docMouseMoveEvent = document.onmousemove;

利用HTML中的DIV層做的消息視窗

    var docMouseUpEvent = document.onmouseup;

利用HTML中的DIV層做的消息視窗

    titleBar.onmousedown = function()

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    ...{

利用HTML中的DIV層做的消息視窗

        var evt = getEvent();

利用HTML中的DIV層做的消息視窗

        moveable = true; 

利用HTML中的DIV層做的消息視窗

        moveX = evt.clientX;//moveX、moveY為滑鼠點選坐标,moveTop、moveLeft為視窗坐标

利用HTML中的DIV層做的消息視窗

        moveY = evt.clientY;

利用HTML中的DIV層做的消息視窗

        moveTop = parseInt(msgObj.style.top);

利用HTML中的DIV層做的消息視窗

        moveLeft = parseInt(msgObj.style.left);

利用HTML中的DIV層做的消息視窗

        document.onmousemove = function() 

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

        ...{

利用HTML中的DIV層做的消息視窗

            if (moveable) 

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

            ...{

利用HTML中的DIV層做的消息視窗

                var evt = getEvent();

利用HTML中的DIV層做的消息視窗

                var x = moveLeft + evt.clientX - moveX;//(evt.clientX - moveX)為滑鼠偏移量

利用HTML中的DIV層做的消息視窗

                var y = moveTop + evt.clientY - moveY;

利用HTML中的DIV層做的消息視窗

                if ( x > 0 &&( x + w < iWidth - 3) && y > 0 && (y + h < iHeight - 3) ) //限制視窗不出界

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

                ...{

利用HTML中的DIV層做的消息視窗

                    msgObj.style.left = x + "px";

利用HTML中的DIV層做的消息視窗

                    msgObj.style.top = y + "px";

利用HTML中的DIV層做的消息視窗

                }

利用HTML中的DIV層做的消息視窗

            }    

利用HTML中的DIV層做的消息視窗

        };

利用HTML中的DIV層做的消息視窗

        document.onmouseup = function () 

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

        ...{ 

利用HTML中的DIV層做的消息視窗

            if (moveable) 

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

            ...{ 

利用HTML中的DIV層做的消息視窗

                document.onmousemove = docMouseMoveEvent;

利用HTML中的DIV層做的消息視窗

                document.onmouseup = docMouseUpEvent;

利用HTML中的DIV層做的消息視窗

                moveable = false; 

利用HTML中的DIV層做的消息視窗

                moveX = 0;

利用HTML中的DIV層做的消息視窗

                moveY = 0;

利用HTML中的DIV層做的消息視窗

                moveTop = 0;

利用HTML中的DIV層做的消息視窗

                moveLeft = 0;

利用HTML中的DIV層做的消息視窗

            } 

利用HTML中的DIV層做的消息視窗

        };

利用HTML中的DIV層做的消息視窗

    }

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    var closeBtn = tr.insertCell(-1);//添加關閉欄

利用HTML中的DIV層做的消息視窗

    closeBtn.style.cssText = "cursor:pointer; padding:2px;background-color:" + titlebgcolor;

利用HTML中的DIV層做的消息視窗

    closeBtn.innerHTML = "<span style='font-size:15pt; color:"+titlecolor+";'>×</span>";

利用HTML中的DIV層做的消息視窗

    closeBtn.onclick = function()

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    ...{ 

利用HTML中的DIV層做的消息視窗

        document.body.removeChild(bgObj); 

利用HTML中的DIV層做的消息視窗

        document.body.removeChild(msgObj); 

利用HTML中的DIV層做的消息視窗

    } 

利用HTML中的DIV層做的消息視窗

    var msgBox = table.insertRow(-1).insertCell(-1);//添加消息欄

利用HTML中的DIV層做的消息視窗

    msgBox.style.cssText = "font:10pt '宋體';";

利用HTML中的DIV層做的消息視窗

    msgBox.colSpan  = 2;

利用HTML中的DIV層做的消息視窗

    msgBox.innerHTML = msg;

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    // 獲得事件Event對象,用于相容IE和FireFox

利用HTML中的DIV層做的消息視窗

    function getEvent() 

利用HTML中的DIV層做的消息視窗
利用HTML中的DIV層做的消息視窗

    ...{

利用HTML中的DIV層做的消息視窗

        return window.event || arguments.callee.caller.arguments[0];

利用HTML中的DIV層做的消息視窗

    }

利用HTML中的DIV層做的消息視窗

}  

繼續閱讀