JavaScript實(shí)現(xiàn)alert彈框效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)alert彈框的具體代碼,供大家參考,具體內(nèi)容如下
因本人水平有限,不足之處還望大家指正。
先上圖:
為什么會(huì)出現(xiàn)這個(gè)需求?瀏覽器自帶的alert不好用嗎?
自帶的alert在不同的瀏覽器是有差異的,而且樣式也不美觀,用戶體驗(yàn)度不是很好。所以我們要自己寫一個(gè)alert彈框,這樣我們就可以按照我們自己的需求,把a(bǔ)lert彈框做的美觀一點(diǎn)。
以下是alert.js代碼:
var myAlert = { alertbox : function(alertContent){ var windowWidth = window.innerWidth; windowHeight = window.innerHeight; alertContainer = document.createElement("div"); alertContainer.id = "myAlertBox"; alertContainer.style.cssText="position:absolute;left:0px;top:0px;width:100%;z-index:9999;"; alertContainer.style.height = windowHeight+"px"; alertOpacity = document.createElement("div"); alertOpacity.style.cssText="position:absolute;left:0px;top:0px;width:100%;background:#000;opacity:0.5;z-index:9999;"; alertOpacity.style.height = windowHeight+"px"; alertContainer.appendChild(alertOpacity) alertMainBox = document.createElement("div"); alertMainBox.style.cssText="position:absolute;width:200px;height:200px;line-height:200px;text-align:center;background:green;z-index:10000;" alertMainBoxLeft = (windowWidth-200)/2; alertMainBoxTop = (windowHeight-200)/2; alertMainBox.style.left = alertMainBoxLeft+"px"; alertMainBox.style.top = alertMainBoxTop+"px"; alertMainBox.innerHTML = alertContent; alertContainer.appendChild(alertMainBox); alertClose = document.createElement("div"); alertClose.id = "closeBox"; alertClose.style.cssText = "position:absolute;right:0px;top:0px;width:30px;height:30px;background:red;cursor:pointer"; alertMainBox.appendChild(alertClose); document.body.appendChild(alertContainer); closeButton = document.getElementById("closeBox"); console.log(closeButton) closeButton.onclick = function(){ document.body.removeChild(document.getElementById("myAlertBox")); } } }
以下是具體要用時(shí)的代碼:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Study</title> </head> <script type="text/javascript" charset="utf-8" src="alert.js"></script> <body> <script type="text/javascript" charset="utf-8"> myAlert.alertbox("這是自定義alert框"); </script> </body> </html>
用法很簡(jiǎn)單,引入alert.js文件,要用時(shí)直接myAlert.alertbox("內(nèi)容");和使用alert一樣。
在這塊給大家提供個(gè)思路,供大家參考。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 拖拽實(shí)現(xiàn)(附注釋),最經(jīng)典簡(jiǎn)單短小精悍!
JavaScript拖拽實(shí)現(xiàn)(附注釋),最經(jīng)典!最簡(jiǎn)單!短小精悍!2009-04-04JS HTML5實(shí)現(xiàn)拖拽移動(dòng)列表效果
這篇文章主要為大家詳細(xì)介紹了JS HTML5實(shí)現(xiàn)拖拽移動(dòng)列表效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07動(dòng)態(tài)加載iframe時(shí)get請(qǐng)求傳遞中文參數(shù)亂碼解決方法
這篇文章主要介紹了動(dòng)態(tài)加載iframe時(shí)get請(qǐng)求傳遞中文參數(shù)亂碼解決方法,需要的朋友可以參考下2014-05-05JS實(shí)現(xiàn)服務(wù)五星好評(píng)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)服務(wù)五星好評(píng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09JavaScript極簡(jiǎn)入門教程(二):對(duì)象和函數(shù)
這篇文章主要介紹了JavaScript極簡(jiǎn)入門教程(二):對(duì)象和函數(shù),本文講解了對(duì)象基礎(chǔ)知識(shí)、函數(shù)基礎(chǔ)知識(shí)、函數(shù)調(diào)用、異常、繼承等內(nèi)容,需要的朋友可以參考下2014-10-10es6函數(shù)之箭頭函數(shù)用法實(shí)例詳解
這篇文章主要介紹了es6函數(shù)之箭頭函數(shù)用法,結(jié)合實(shí)例形式詳細(xì)分析了es6箭頭函數(shù)基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04textarea不能通過maxlength屬性來限制字?jǐn)?shù)的解決方法
textarea稱文本域,又稱文本區(qū),其不能通過maxlength屬性來限制字?jǐn)?shù),為此必須尋求其他方法來加以限制以達(dá)到預(yù)設(shè)的需求2014-09-09