您好,登錄后才能下訂單哦!
這篇文章主要介紹“原生js提示框并自動關閉怎么實現(xiàn)”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“原生js提示框并自動關閉怎么實現(xiàn)”文章能幫助大家解決問題。
js提示框,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自動關閉窗口</title> </head> <body> <input type="button" value="彈出alert框自動關閉" onClick="Alert('不點擊確定三秒后自動關閉')"> <script> //自動關閉提示框 function Alert(str) { var msgw,msgh,bordercolor; msgw=350;//提示窗口的寬度 msgh=80;//提示窗口的高度 titleheight=25 //提示窗口標題高度 bordercolor="#336699";//提示窗口的邊框顏色 titlecolor="#99CCFF";//提示窗口的標題顏色 var sWidth,sHeight; //獲取當前窗口尺寸 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"; //窗口距離左側和頂端的距離 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); //提示信息標題 var title=document.createElement("h5"); 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); //設置關閉時間 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>
<html> <head> <title>JavaScript自動關閉窗口</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 //提示窗口標題高度 bordercolor="#336699";//提示窗口的邊框顏色 titlecolor="#99CCFF";//提示窗口的標題顏色 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("h5"); 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 = "點擊關閉"; title.innerHTML="<table border='0′ width='100%'><tr><td align='left'>"+ strTitle +"</td><td>關閉</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框自動關閉" onClick="sAlert('','你要輸出的內容!')"> </body> </html>
<!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 > <h3 >Pop Up</h3> <div > <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>
關于“原生js提示框并自動關閉怎么實現(xiàn)”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。