自訂彈窗Style樣式,彈窗style樣式

來源:互聯網
上載者:User

自訂彈窗Style樣式,彈窗style樣式

由於系統預設alert快顯視窗不能自訂樣式,有可能不符合網站的風格,雖然網上應該有很多這樣的JS

但是還是自己寫的比較放心,順便練習一下對DOM的操作

支援IE6下的SELECT不能遮罩的問題,Google支援圓角,IE6下就比較醜了,四四方方的,不過可以自訂自己喜歡的樣式

聽取建議後,修改了position:fixed, IE6下用hack處理了。

點擊看效果:

點擊類比Alert彈出框

點擊類比Alert彈出框

點擊類比Alert彈出框

所需CSS:

    <style type="text/css">        #alertMsg {            display: none;            width: 400px;            border: 1px solid #ddd;            border-radius: 5px;            box-shadow: 1px 1px 10px black;            padding: 10px;            font-size: 12px;            position: absolute;            text-align: center;            background: #fff;            z-index: 100000;        }        #alertMsg_info {            padding: 2px 15px;            line-height: 1.6em;            text-align: left;        }        #alertMsg_btn1, #alertMsg_btn2 {            display: inline-block;            background: url(images/gray_btn.png) no-repeat left top;            padding-left: 3px;            color: #000000;            font-size: 12px;            text-decoration: none;            margin-right: 10px;            cursor: pointer;        }        #alertMsg_btn1 cite, #alertMsg_btn2 cite {            line-height: 24px;            display: inline-block;            padding: 0 13px 0 10px;            background: url(images/gray_btn.png) no-repeat right top;            font-style: normal;        }    </style>

 使用方法,直接調用函數,傳遞所需定義的資訊,支援定義是否有取消鍵:

alertMsg(msg, mode)  //mode為空白,即只有一個確認按鈕,mode為1時有確認和取消兩個按鈕

 

點擊類比Alert彈出框

點擊類比Alert彈出框

點擊類比Alert彈出框

函數代碼:添加了一個擷取視窗尺寸的函數,又長長了很多,可以把擷取視窗的尺寸另外立一個函數放公用庫裡面,這裡只是為了方便示範,寫到一個函數裡面

function alertMsg(msg, mode) { //mode為空白,即只有一個確認按鈕,mode為1時有確認和取消兩個按鈕        msg = msg || '';        mode = mode || 0;        var top = document.body.scrollTop || document.documentElement.scrollTop;        var isIe = (document.all) ? true : false;        var isIE6 = isIe && !window.XMLHttpRequest;        var sTop = document.documentElement.scrollTop || document.body.scrollTop;        var sLeft = document.documentElement.scrollLeft || document.body.scrollLeft;        var winSize = function(){            var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight;            // innerHeight擷取的是可視視窗的高度,IE不支援此屬性            if (window.innerHeight && window.scrollMaxY) {                xScroll = document.body.scrollWidth;                yScroll = window.innerHeight + window.scrollMaxY;            } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac                xScroll = document.body.scrollWidth;                yScroll = document.body.scrollHeight;            } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari                xScroll = document.body.offsetWidth;                yScroll = document.body.offsetHeight;            }            if (self.innerHeight) {    // all except Explorer                windowWidth = self.innerWidth;                windowHeight = self.innerHeight;            } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode                windowWidth = document.documentElement.clientWidth;                windowHeight = document.documentElement.clientHeight;            } else if (document.body) { // other Explorers                windowWidth = document.body.clientWidth;                windowHeight = document.body.clientHeight;            }            // for small pages with total height less then height of the viewport            if (yScroll < windowHeight) {                pageHeight = windowHeight;            } else {                pageHeight = yScroll;            }            // for small pages with total width less then width of the viewport            if (xScroll < windowWidth) {                pageWidth = windowWidth;            } else {                pageWidth = xScroll;            }            return{                'pageWidth':pageWidth,                'pageHeight':pageHeight,                'windowWidth':windowWidth,                'windowHeight':windowHeight            }        }();        //alert(winSize.pageWidth);        //遮罩層        var styleStr = 'top:0;left:0;position:absolute;z-index:10000;background:#666;width:' + winSize.pageWidth + 'px;height:' +  (winSize.pageHeight + 30) + 'px;';        styleStr += (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //遮罩層DIV        var shadowDiv = document.createElement('div'); //添加陰影DIV        shadowDiv.style.cssText = styleStr; //添加樣式        shadowDiv.id = "shadowDiv";        //如果是IE6則建立IFRAME遮罩SELECT        if (isIE6) {            var maskIframe = document.createElement('iframe');            maskIframe.style.cssText = 'width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;position:absolute;visibility:inherit;z-index:-1;filter:alpha(opacity=0);';            maskIframe.frameborder = 0;            maskIframe.src = "about:blank";            shadowDiv.appendChild(maskIframe);        }        document.body.insertBefore(shadowDiv, document.body.firstChild); //遮罩層加入文檔        //彈出框        var styleStr1 = 'display:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 200) + 'px;top:' + (winSize.windowHeight / 2 - 150) + 'px;_top:' + (winSize.windowHeight / 2 + top - 150)+ 'px;'; //彈出框的位置        var alertBox = document.createElement('div');        alertBox.id = 'alertMsg';        alertBox.style.cssText = styleStr1;        //建立彈出框裡面的內容P標籤        var alertMsg_info = document.createElement('P');        alertMsg_info.id = 'alertMsg_info';        alertMsg_info.innerHTML = msg;        alertBox.appendChild(alertMsg_info);        //建立按鈕        var btn1 = document.createElement('a');        btn1.id = 'alertMsg_btn1';        btn1.href = 'javas' + 'cript:void(0)';        btn1.innerHTML = '<cite>確定</cite>';        btn1.onclick = function () {            document.body.removeChild(alertBox);            document.body.removeChild(shadowDiv);            return true;        };        alertBox.appendChild(btn1);        if (mode === 1) {            var btn2 = document.createElement('a');            btn2.id = 'alertMsg_btn2';            btn2.href = 'javas' + 'cript:void(0)';            btn2.innerHTML = '<cite>取消</cite>';            btn2.onclick = function () {                document.body.removeChild(alertBox);                document.body.removeChild(shadowDiv);                return false;            };            alertBox.appendChild(btn2);        }        document.body.appendChild(alertBox);    }
 

點擊類比Alert彈出框

點擊類比Alert彈出框

點擊類比Alert彈出框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.