2015-04-05 74 views
2

我正在制作典型JS'alert()'框的样式化HTML版本。暂停文档 - HTML,CSS,JavaScript

这只是一个很好的使用'display:none'和'display:block'切换框。

但是,这并没有一个JS“警报()”的功能框,因为这样做

for(var c = 0; c < 10; c++){ //like the joke? (c++) 
    cool.alert('You have seen '+c+' alerts'); 
} 

不会创造连续10个警告框,但使框的显示“块” 10倍。

是否有任何方法暂停文档,直到警告框关闭,以便循环会暂停?

这里的所有相关代码:

<button onclick="cool.alert('Hi')">Alert box</button><div id='block'></div> 
 
<div id='box'> 
 
\t <p id='text'></p><hr id='hr'> 
 
    <div id='Ok' onclick='cool.alertclear()'>Ok</div> 
 
</div> 
 
<script> 
 
var cover = document.getElementById('block'); 
 
var box = document.getElementById('box'); 
 
var text = document.getElementById('text'); 
 
var ok = document.getElementById('Ok'); 
 
var hr = document.getElementById('hr'); 
 
var cool = { 
 
    alert: function(input){ 
 
    \t cover.style.display = 'block'; 
 
     box.style.display = 'block'; 
 
     ok.style.display = 'block'; 
 
     text.innerHTML = input; 
 
    }, 
 
    alertclear: function(){ 
 
    \t cover.style.display = 'none'; 
 
     box.style.display = 'none'; 
 
     ok.style.display = 'none'; 
 
    } 
 
} 
 
</script> 
 
<style> 
 
#block{ 
 
\t position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    height: 100%; 
 
    width: 100%; 
 
    background: rgba(0,0,0,0.6); 
 
    display: none; 
 
    z-index: 100; 
 
} 
 
#box{ 
 
\t position: absolute; 
 
    top: 25%; 
 
    left: 35%; 
 
    height: 30%; 
 
    width: 30%; 
 
    border-radius: 10px; 
 
    background: rgba(255,255,255,0.8); 
 
    box-shadow: 0 0 50px rgba(0,0,0,0.2); 
 
    display: none; 
 
\t z-index: 101; 
 
    color: #000; 
 
    padding: 10px; 
 
    cursor: default; 
 
} 
 
#text{ 
 
\t height: 60%; 
 
    word-break: break-all; 
 
    overflow-x: hidden; 
 
    overflow-y: auto; 
 
} 
 
#Yes{ 
 
\t position: absolute; 
 
    bottom: 5%; 
 
    right: 25%; 
 
    height: 15%; 
 
    width: 18%; 
 
    border-radius: 5px; 
 
    background: linear-gradient(left top, #00FF00, #00DD00); 
 
    background: -webkit-linear-gradient(left top, #00FF00, #00DD00); 
 
    cursor: hand; 
 
    text-align: center; 
 
    font-size: 1.5em; 
 
    color: white; 
 
    display: none; 
 
} 
 
#No{ 
 
\t position: absolute; 
 
    bottom: 5%; 
 
    right: 5%; 
 
    height: 15%; 
 
    width: 18%; 
 
    border-radius: 5px; 
 
    background: linear-gradient(left top, #ff6c6c, #ff4e4e); 
 
    background: -webkit-linear-gradient(left top, #ff6c6c, #ff4e4e); 
 
    cursor: hand; 
 
    text-align: center; 
 
    font-size: 1.5em; 
 
    color: white; 
 
    display: none; 
 
} 
 
#Ok, #Go{ 
 
\t position: absolute; 
 
    bottom: 5%; 
 
    right: 5%; 
 
    height: 15%; 
 
    width: 18%; 
 
    border-radius: 5px; 
 
    background: grey; 
 
    cursor: hand; 
 
    text-align: center; 
 
    font-size: 1.5em; 
 
    color: white; 
 
    display: none; 
 
} 
 
#Prompt{ 
 
\t position: absolute; 
 
    top: 30%; 
 
    left: 5%; 
 
    height: 40%; 
 
    width: 90%; 
 
    resize: none; 
 
    overflow-x: hidden; 
 
    overflow-y: auto; 
 
    word-break: break-all; 
 
    display: none; 
 
} 
 
#hr{ 
 
\t position: relative; 
 
    bottom: 0%; 
 
} 
 
</style>

+0

**注:**我不想setTimeout – Tobsta 2015-04-05 08:09:55

+1

如果想要使用jQuery可以使用.promise()。done – 2015-04-05 08:16:50

+0

@MoisheLipsker,你能否详细解释一下?我将把jQuery放在哪里?我会把它放在cool.alert()函数中吗?我最后怎么说? $(文档)? – Tobsta 2015-04-05 08:20:27

回答

2

你可以跟踪警报的是这样的:

var inputArr = []; 
var showing = false; 
var cool = { 
    alert: function(input){ 
     if(!showing) { 
      cool.show(input); 
      showing = true; 
     } else { 
      inputArr.push(input); 
     } 
    }, 
    alertclear: function(){ 
     cover.style.display = 'none'; 
     box.style.display = 'none'; 
     ok.style.display = 'none'; 
     if(inputArr.length>0) { 
      input = inputArr.shift(); 
      cool.show(input); 
     } else { 
      showing = false; 
     } 
    }, 
    show: function(input) { 
     cover.style.display = 'block'; 
     box.style.display = 'block'; 
     ok.style.display = 'block'; 
     text.innerHTML = input; 
    } 
} 
+0

哇!谢谢。完美的作品。 – Tobsta 2015-04-05 10:02:26

+0

其实,我已经用我的确认和提示函数试过了,它工作正常,但是当我尝试调用不同类型的对话时,它或者只是做同样的事情,或者给我一个'范围错误:调用堆栈溢出' (笑) – Tobsta 2015-04-05 10:26:30

+0

我想如果你这样做,你需要把它包装在一个类中,并为触发不同对话框的不同控件创建一个新实例。 – 2015-04-05 11:38:31