JavaScript 實(shí)現(xiàn)簡單的倒計時彈窗DEMO附圖
更新時間:2014年03月05日 16:47:56 作者:
做一個簡單的設(shè)置網(wǎng)頁,因為需要重啟設(shè)備功能,于是就想在上面加一個倒計時彈窗的界面,下面是具體的實(shí)現(xiàn),大家可以參考下
最近做一個簡單的設(shè)置網(wǎng)頁,因為需要重啟設(shè)備功能,于是就想在上面加一個倒計時彈窗的界面。
剛開始的想法是自定義一個alert彈窗,但很快就發(fā)現(xiàn),alert會一直停在那里等待點(diǎn)擊確認(rèn),而不是我想要的那種自動連續(xù)顯示的效果。
后來,才想到直接顯示和隱藏DIV制作成的彈窗,就可以實(shí)現(xiàn)了?;谶@個思路,于是有了下面的:
先看效果圖:
再看源代碼:
<!------------------ 重啟操作 準(zhǔn)備彈窗 --------------->
<div id="reboot_pre" style="width: 450px; height: 200px; margin-left:auto; margin-right:auto; margin-top:200px; visibility:hidden; background: #F0F0F0; border:1px solid #00DB00; z-index:9999">
<div style="width: 450px; height: 30px; background:#00DB00; line-height:30px;text-align: center;"><b>準(zhǔn)備中</b></div>
<br /><br />
<div><p style="margin-left:50px">正在努力為您準(zhǔn)備重啟操作... 還需稍候 <span id="reboot_pre_time">4</span> 秒</p></div>
<br />
<div><button type="button" style="width:70px; height:30px; margin-left:340px" onclick="reboot_cancel()">取消</button></div>
</div>
<!------------------ 重啟操作 準(zhǔn)備彈窗 --------------->
<!------------------ 重啟操作 進(jìn)行彈窗 --------------->
<div id="reboot_ing" style="width: 450px; height: 150px; margin-left:auto; margin-right:auto; margin-top:-150px; visibility: hidden; background: #F0F0F0; border:1px solid #00DB00">
<div style="width: 450px; height: 30px; background:#00DB00; line-height:30px;text-align: center;"><b>進(jìn)行中</b></div>
<br />
<div><p style="margin-left:40px">重啟操作正在進(jìn)行中... 還需稍候 <span id="reboot_ing_time">14</span> 秒</p></div>
<br />
<div id="progress_reboot" style="margin-left:40px;color:#00DB00;font-family:Arial;font-weight:bold;height:18px">|</div>
<br />
</div>
<!------------------ 重啟操作 進(jìn)行彈窗 --------------->
lt;script type="text/javascript">
var cancel_flag = 0;
var already = 0;
/* 網(wǎng)頁一加載就執(zhí)行的操作 */
window.onload = reboot();
/* 重啟按鈕的單擊操作 */
function reboot(){
if(confirm("這個操作會斷開現(xiàn)在所有的連接,并且重新啟動您的設(shè)備,確定要繼續(xù)操作嗎?")){
document.getElementById("reboot_pre_time").innerHTML = 4;
document.getElementById("reboot_ing_time").innerHTML = 14;
document.all.progress_reboot.innerHTML = "|";
download_flag = 0;
cancel_flag = 0;
already = 0;
setTimeout("showDiv('reboot_pre')",500);
delayPre_reboot("reboot_pre_time");
}
}
/* 重啟準(zhǔn)備彈窗計時 5秒 */
function delayPre_reboot(str) {
if(!cancel_flag){
var delay = document.getElementById(str).innerHTML;
if(delay > 0) {
delay--;
document.getElementById(str).innerHTML = delay;
setTimeout("delayPre_reboot('reboot_pre_time')", 1000);
} else {
hideDiv("reboot_pre");
setTimeout("showDiv('reboot_ing')",500);
delayDo_reboot("reboot_ing_time");
}
}
}
/* 重啟進(jìn)行中彈窗計時 15秒 */
function delayDo_reboot(str){
display_reboot(100);
var delay = document.getElementById(str).innerHTML;
if(delay > 0) {
delay--;
document.getElementById(str).innerHTML = delay;
setTimeout("delayDo_reboot('reboot_ing_time')", 1000);
} else {
hideDiv("reboot_ing");
alert("重啟成功!");
}
}
/* 重啟準(zhǔn)備時 取消按鈕的事件*/
function reboot_cancel(){
cancel_flag = 1;
hideDiv("reboot_pre");
alert("您已經(jīng)成功取消了重啟操作!");
}
/* 顯示彈窗 */
function showDiv (str){
document.getElementById(str).style.visibility = "visible";
}
/* 隱藏彈窗 */
function hideDiv (str){
document.getElementById(str).style.visibility = "hidden";
}
/* 重啟進(jìn)行中彈窗計時,緩沖條的移動*/
function display_reboot(max){
already++;
var dispObj = document.all.progress_reboot;
dispObj.style.width = 100.0*already/max+"px";
document.all.progress_reboot.innerHTML += "|||||";
var timer = window.setTimeout("display("+max+")",1000);
if (already >= max){
window.clearTimeout(timer);
}
}
</script>
剛開始的想法是自定義一個alert彈窗,但很快就發(fā)現(xiàn),alert會一直停在那里等待點(diǎn)擊確認(rèn),而不是我想要的那種自動連續(xù)顯示的效果。
后來,才想到直接顯示和隱藏DIV制作成的彈窗,就可以實(shí)現(xiàn)了?;谶@個思路,于是有了下面的:
先看效果圖:


再看源代碼:
復(fù)制代碼 代碼如下:
<!------------------ 重啟操作 準(zhǔn)備彈窗 --------------->
<div id="reboot_pre" style="width: 450px; height: 200px; margin-left:auto; margin-right:auto; margin-top:200px; visibility:hidden; background: #F0F0F0; border:1px solid #00DB00; z-index:9999">
<div style="width: 450px; height: 30px; background:#00DB00; line-height:30px;text-align: center;"><b>準(zhǔn)備中</b></div>
<br /><br />
<div><p style="margin-left:50px">正在努力為您準(zhǔn)備重啟操作... 還需稍候 <span id="reboot_pre_time">4</span> 秒</p></div>
<br />
<div><button type="button" style="width:70px; height:30px; margin-left:340px" onclick="reboot_cancel()">取消</button></div>
</div>
<!------------------ 重啟操作 準(zhǔn)備彈窗 --------------->
<!------------------ 重啟操作 進(jìn)行彈窗 --------------->
<div id="reboot_ing" style="width: 450px; height: 150px; margin-left:auto; margin-right:auto; margin-top:-150px; visibility: hidden; background: #F0F0F0; border:1px solid #00DB00">
<div style="width: 450px; height: 30px; background:#00DB00; line-height:30px;text-align: center;"><b>進(jìn)行中</b></div>
<br />
<div><p style="margin-left:40px">重啟操作正在進(jìn)行中... 還需稍候 <span id="reboot_ing_time">14</span> 秒</p></div>
<br />
<div id="progress_reboot" style="margin-left:40px;color:#00DB00;font-family:Arial;font-weight:bold;height:18px">|</div>
<br />
</div>
<!------------------ 重啟操作 進(jìn)行彈窗 --------------->
lt;script type="text/javascript">
var cancel_flag = 0;
var already = 0;
/* 網(wǎng)頁一加載就執(zhí)行的操作 */
window.onload = reboot();
/* 重啟按鈕的單擊操作 */
function reboot(){
if(confirm("這個操作會斷開現(xiàn)在所有的連接,并且重新啟動您的設(shè)備,確定要繼續(xù)操作嗎?")){
document.getElementById("reboot_pre_time").innerHTML = 4;
document.getElementById("reboot_ing_time").innerHTML = 14;
document.all.progress_reboot.innerHTML = "|";
download_flag = 0;
cancel_flag = 0;
already = 0;
setTimeout("showDiv('reboot_pre')",500);
delayPre_reboot("reboot_pre_time");
}
}
/* 重啟準(zhǔn)備彈窗計時 5秒 */
function delayPre_reboot(str) {
if(!cancel_flag){
var delay = document.getElementById(str).innerHTML;
if(delay > 0) {
delay--;
document.getElementById(str).innerHTML = delay;
setTimeout("delayPre_reboot('reboot_pre_time')", 1000);
} else {
hideDiv("reboot_pre");
setTimeout("showDiv('reboot_ing')",500);
delayDo_reboot("reboot_ing_time");
}
}
}
/* 重啟進(jìn)行中彈窗計時 15秒 */
function delayDo_reboot(str){
display_reboot(100);
var delay = document.getElementById(str).innerHTML;
if(delay > 0) {
delay--;
document.getElementById(str).innerHTML = delay;
setTimeout("delayDo_reboot('reboot_ing_time')", 1000);
} else {
hideDiv("reboot_ing");
alert("重啟成功!");
}
}
/* 重啟準(zhǔn)備時 取消按鈕的事件*/
function reboot_cancel(){
cancel_flag = 1;
hideDiv("reboot_pre");
alert("您已經(jīng)成功取消了重啟操作!");
}
/* 顯示彈窗 */
function showDiv (str){
document.getElementById(str).style.visibility = "visible";
}
/* 隱藏彈窗 */
function hideDiv (str){
document.getElementById(str).style.visibility = "hidden";
}
/* 重啟進(jìn)行中彈窗計時,緩沖條的移動*/
function display_reboot(max){
already++;
var dispObj = document.all.progress_reboot;
dispObj.style.width = 100.0*already/max+"px";
document.all.progress_reboot.innerHTML += "|||||";
var timer = window.setTimeout("display("+max+")",1000);
if (already >= max){
window.clearTimeout(timer);
}
}
</script>
您可能感興趣的文章:
- 關(guān)于vue.js彈窗組件的知識點(diǎn)總結(jié)
- js彈出框、對話框、提示框、彈窗實(shí)現(xiàn)方法總結(jié)(推薦)
- JS非Alert實(shí)現(xiàn)網(wǎng)頁右下角“未讀信息”效果彈窗
- JS實(shí)現(xiàn)可拖曳、可關(guān)閉的彈窗效果
- javascript實(shí)現(xiàn)倒計時并彈窗提示特效
- javascript實(shí)現(xiàn)的右下角彈窗實(shí)例
- js實(shí)現(xiàn)彈窗插件功能實(shí)例代碼分享
- 原生JS可拖動彈窗效果實(shí)例代碼
- Js控制彈窗實(shí)現(xiàn)在任意分辨率下居中顯示
- js點(diǎn)擊彈出div層實(shí)現(xiàn)可拖曳的彈窗效果
- js彈窗代碼 可以指定彈出間隔
- js實(shí)現(xiàn)彈窗暗層效果
相關(guān)文章
JavaScript switch case 的用法實(shí)例[范圍]
JavaScript switch case 的用法實(shí)例,大家可以參考下。2009-09-09Uni-App用uView組件庫中u-picker實(shí)現(xiàn)地區(qū)的省-市-區(qū)三級聯(lián)動、確認(rèn)及回顯
最近項目要使用uni-app遇到省市縣三級聯(lián)動的問題,下面這篇文章主要給大家介紹了關(guān)于Uni-App用uView組件庫中u-picker實(shí)現(xiàn)地區(qū)的省-市-區(qū)三級聯(lián)動、確認(rèn)及回顯的相關(guān)資料,需要的朋友可以參考下2023-12-12利用Bootstrap Multiselect實(shí)現(xiàn)下拉框多選功能
這篇文章主要介紹了利用Bootstrap Multiselect實(shí)現(xiàn)下拉框多選功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04