原生js提示框并自動(dòng)關(guān)閉(手工關(guān)閉)
js提示框,n秒后自動(dòng)關(guān)閉,多用于簡單的提示,無需人工參與,例如復(fù)制內(nèi)容的時(shí)候提示復(fù)制成功或者后臺(tái)提交信息的返回界面都可以用這個(gè)
JavaScript自動(dòng)關(guān)閉窗口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript自動(dòng)關(guān)閉窗口</title>
</head>
<body>
<input type="button" value="彈出alert框自動(dòng)關(guān)閉" onClick="Alert('不點(diǎn)擊確定三秒后自動(dòng)關(guān)閉')">
<script>
//自動(dòng)關(guān)閉提示框
function Alert(str) {
var msgw,msgh,bordercolor;
msgw=350;//提示窗口的寬度
msgh=80;//提示窗口的高度
titleheight=25 //提示窗口標(biāo)題高度
bordercolor="#336699";//提示窗口的邊框顏色
titlecolor="#99CCFF";//提示窗口的標(biāo)題顏色
var sWidth,sHeight;
//獲取當(dāng)前窗口尺寸
sWidth = document.body.offsetWidth;
sHeight = document.body.offsetHeight;
// //背景div
var bgObj=document.createElement("div");
bgObj.setAttribute('id','alertbgDiv');
bgObj.style.position="absolute";
bgObj.style.top="0";
bgObj.style.background="#E8E8E8";
bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
bgObj.style.opacity="0.6";
bgObj.style.left="0";
bgObj.style.width = sWidth + "px";
bgObj.style.height = sHeight + "px";
bgObj.style.zIndex = "10000";
document.body.appendChild(bgObj);
//創(chuàng)建提示窗口的div
var msgObj = document.createElement("div")
msgObj.setAttribute("id","alertmsgDiv");
msgObj.setAttribute("align","center");
msgObj.style.background="white";
msgObj.style.border="1px solid " + bordercolor;
msgObj.style.position = "absolute";
msgObj.style.left = "50%";
msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
//窗口距離左側(cè)和頂端的距離
msgObj.style.marginLeft = "-225px";
//窗口被卷去的高+(屏幕可用工作區(qū)高/2)-150
msgObj.style.top = document.body.scrollTop+(window.screen.availHeight/2)-150 +"px";
msgObj.style.width = msgw + "px";
msgObj.style.height = msgh + "px";
msgObj.style.textAlign = "center";
msgObj.style.lineHeight ="25px";
msgObj.style.zIndex = "10001";
document.body.appendChild(msgObj);
//提示信息標(biāo)題
var title=document.createElement("h4");
title.setAttribute("id","alertmsgTitle");
title.setAttribute("align","left");
title.style.margin="0";
title.style.padding="3px";
title.style.background = bordercolor;
title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
title.style.opacity="0.75";
title.style.border="1px solid " + bordercolor;
title.style.height="18px";
title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
title.style.color="white";
title.innerHTML="提示信息";
document.getElementById("alertmsgDiv").appendChild(title);
//提示信息
var txt = document.createElement("p");
txt.setAttribute("id","msgTxt");
txt.style.margin="16px 0";
txt.innerHTML = str;
document.getElementById("alertmsgDiv").appendChild(txt);
//設(shè)置關(guān)閉時(shí)間
window.setTimeout("closewin()",2000);
}
function closewin() {
document.body.removeChild(document.getElementById("alertbgDiv"));
document.getElementById("alertmsgDiv").removeChild(document.getElementById("alertmsgTitle"));
document.body.removeChild(document.getElementById("alertmsgDiv"));
}
</script>
</body>
</html> 彈出提示框,手動(dòng)關(guān)閉
<html>
<head>
<title>JavaScript自動(dòng)關(guān)閉窗口</title>
<meta http-equiv="content-Type" content="text/html;charset=gb2312">
</head>
<body>
<script>
function sAlert(strTitle,strContent){
var msgw,msgh,bordercolor;
msgw=300;//提示窗口的寬度
msgh=100;//提示窗口的高度
titleheight=25 //提示窗口標(biāo)題高度
bordercolor="#336699";//提示窗口的邊框顏色
titlecolor="#99CCFF";//提示窗口的標(biāo)題顏色
var sWidth,sHeight;
sWidth=document.body.offsetWidth;
sHeight=screen.height;
var bgObj=document.createElement("div");
bgObj.setAttribute('id','bgDiv');
bgObj.style.position="absolute";
bgObj.style.top="0";
bgObj.style.background="#777";
bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
bgObj.style.opacity="0.6";
bgObj.style.left="0";
bgObj.style.width=sWidth + "px";
bgObj.style.height=sHeight + "px";
bgObj.style.zIndex = "10000";
document.body.appendChild(bgObj);
var msgObj=document.createElement("div")
msgObj.setAttribute("id","msgDiv");
msgObj.setAttribute("align","center");
msgObj.style.background="white";
msgObj.style.border="1px solid " + bordercolor;
msgObj.style.position = "absolute";
msgObj.style.left = "50%";
msgObj.style.top = "50%";
msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
msgObj.style.marginLeft = "-225px" ;
msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
msgObj.style.width = msgw + "px";
msgObj.style.height =msgh + "px";
msgObj.style.textAlign = "center";
msgObj.style.lineHeight ="25px";
msgObj.style.zIndex = "10001";
var title=document.createElement("h4");
title.setAttribute("id","msgTitle");
title.setAttribute("align","right");
title.style.margin="0";
title.style.padding="3px";
title.style.background=bordercolor;
title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
title.style.opacity="0.75";
title.style.border="1px solid " + bordercolor;
title.style.height="18px";
title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
title.style.color="white";
title.style.cursor="pointer";
title.title = "點(diǎn)擊關(guān)閉";
title.innerHTML="<table border='0′ width='100%'><tr><td align='left'>"+ strTitle +"</td><td>關(guān)閉</td></tr></table></div>";
title.οnclick=function(){
document.body.removeChild(bgObj);
document.getElementById("msgDiv").removeChild(title);
document.body.removeChild(msgObj);
}
document.body.appendChild(msgObj);
document.getElementById("msgDiv").appendChild(title);
var txt=document.createElement("p");
txt.style.margin="1em 0"
txt.setAttribute("id","msgTxt");
txt.innerHTML=strContent;
document.getElementById("msgDiv").appendChild(txt);
function closewin() {
document.body.removeChild(document.getElementById("msgDiv"));
}
}
</script>
<input type="button" value="彈出alert框自動(dòng)關(guān)閉" onClick="sAlert('','你要輸出的內(nèi)容!')">
</body>
</html> 適合復(fù)制提示的提示框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.popupStyle{
width:180px;
height:50px;
background-color: rgb(85,85,85);
/* display:none; */
color:#fff;
text-align:center;
line-height:50px;
border-radius:10px;
padding:0;
position:fixed;
z-index:1;
top:40%;
left:50%;
transform:translateX(-50%);
}
</style>
</head>
<body style="text-align:center;">
<h2 style="margin-top:200px;">Pop Up</h2>
<div style="text-align:center;">
<a href="#" rel="external nofollow" rel="external nofollow" onclick="stylePopup(styleMsg)">stylePopup</a>
<a href="#" rel="external nofollow" rel="external nofollow" onclick="domPopup(domMsg)">domPopup</a>
</div>
<span class="popupStyle" id="popup">popupStyle</span>
<script>
console.log(getComputedStyle(popup).display)
const styleMsg='Popup of style'
const domMsg='Popup of dom'
function stylePopup(message){
const popup=document.getElementById('popup')
console.log(popup)
popup.innerHTML=message || 'default messsage'
popup.style.display='block'
setTimeout(()=>{
popup.style.display='none'
},1000)
}
function domPopup(message){
const span=document.createElement('span')
span.innerHTML=message || 'default'
span.className='popupStyle'
span.style.display='block'
document.body.appendChild(span)
setTimeout(()=>{
span.remove()
},1000)
}
</script>
</body>
</html>到此這篇關(guān)于原生js提示框并自動(dòng)關(guān)閉(手工關(guān)閉)的文章就介紹到這了,更多相關(guān)js提示框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- javascript實(shí)現(xiàn)倒計(jì)時(shí)提示框
- js制作提示框插件
- 原生js實(shí)現(xiàn)自定義消息提示框
- js實(shí)現(xiàn)搜索提示框效果
- JS實(shí)現(xiàn)提示框跟隨鼠標(biāo)移動(dòng)
- JavaScript實(shí)現(xiàn)短暫提示框功能
- 原生JavaScript實(shí)現(xiàn)Tooltip浮動(dòng)提示框特效
- js實(shí)現(xiàn)百度搜索提示框
- js提示框替代系統(tǒng)alert,自動(dòng)關(guān)閉alert對話框的實(shí)現(xiàn)方法
- javascript實(shí)現(xiàn)延時(shí)顯示提示框特效代碼
- JS實(shí)時(shí)彈出新消息提示框并有提示音響起的實(shí)現(xiàn)代碼
- JavaScript實(shí)現(xiàn)簡單Tip提示框效果
- js右下角彈出提示框示例代碼
- JS延時(shí)提示框?qū)崿F(xiàn)方法詳解
相關(guān)文章
一篇文章教你用JavaScript使用流程控制打印九九乘法表
這篇文章主要為大家詳細(xì)介紹了JavaScript使用流程控制打印九九乘法表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
JavaScript面向?qū)ο蟪绦蛟O(shè)計(jì)中對象的定義和繼承詳解
這篇文章主要介紹了JavaScript面向?qū)ο蟪绦蛟O(shè)計(jì)中對象的定義和繼承,結(jié)合實(shí)例形式詳細(xì)分析了javascript面向?qū)ο蟪绦蛟O(shè)計(jì)中對象定義、繼承、屬性、方法、深拷貝等相關(guān)概念與操作技巧,需要的朋友可以參考下2019-07-07
element-ui的回調(diào)函數(shù)Events的用法詳解
這篇文章主要介紹了element-ui的回調(diào)函數(shù)Events的用法,本文通過實(shí)例代碼給大家介紹了change回調(diào)函數(shù)的使用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
詳解JavaScript?(!!)?中的雙感嘆號(hào)是干什么用的
JavaScript?不是靜態(tài)語言,而是動(dòng)態(tài)語言,這意味著變量可以引用或保存任何類型的值,此外,該類型可以隨時(shí)更改,這篇文章主要介紹了JavaScript?(!!)?中的雙感嘆號(hào)作用,需要的朋友可以參考下2022-09-09
基于javascript實(shí)現(xiàn)右下角浮動(dòng)廣告效果
這篇文章主要介紹了基于javascript實(shí)現(xiàn)右下角浮動(dòng)廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
javascript之典型高階函數(shù)應(yīng)用介紹
這幾個(gè)方法均為javascript 1.6 數(shù)組新增的方法。是很典型的functional 函數(shù),當(dāng)然也非常實(shí)用。下面是functional的定義并不來自javascript2013-01-01
JS中如何將JSON數(shù)組轉(zhuǎn)化為url參數(shù)
這篇文章主要介紹了JS中如何將JSON數(shù)組轉(zhuǎn)化為url參數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
JS實(shí)現(xiàn)的省份級聯(lián)實(shí)例代碼
這篇文章主要介紹了js下省份級聯(lián)效果,需要的朋友可以參考一下2013-06-06
HTML+JavaScript實(shí)現(xiàn)掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了HTML+JavaScript實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

