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)文章
關(guān)于scrollLeft,scrollTop的瀏覽器兼容性測(cè)試
彈窗在谷歌瀏覽器chrome下的位置跟在別的瀏覽器下不一樣,接下來(lái)介紹下scrollLeft,scrollTop的瀏覽器兼容性,感興趣的你可以參考下哈2013-03-03
Three.js中的紋理圖像應(yīng)用和屬性調(diào)整方法
在three.js中紋理貼圖是用來(lái)給物體表面添加圖案、顏色或者其他視覺(jué)效果的一種技術(shù),這篇文章主要給大家介紹了關(guān)于Three.js中紋理圖像應(yīng)用和屬性調(diào)整的相關(guān)資料,需要的朋友可以參考下2024-01-01
JavaScript設(shè)計(jì)模式之緩存代理模式原理與簡(jiǎn)單用法示例
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之緩存代理模式原理與簡(jiǎn)單用法,結(jié)合實(shí)例形式簡(jiǎn)要分析了javascript緩存代理模式的基本原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
three.js創(chuàng)造時(shí)空裂縫特效實(shí)現(xiàn)示例
這篇文章主要為大家介紹了three.js創(chuàng)造時(shí)空裂縫特效實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Lottie動(dòng)畫前端開(kāi)發(fā)使用技巧
這篇文章主要為大家介紹了Lottie動(dòng)畫前端開(kāi)發(fā)使用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
如何解決easyui自定義標(biāo)簽 datagrid edit combobox 手動(dòng)輸入保存不上
這篇文章主要介紹了如何解決easyui自定義標(biāo)簽 datagrid edit combobox 手動(dòng)輸入保存不上,需要的朋友可以參考下2015-12-12

