函数阻止setInterval调用使width样式保持静态
Function to stop a setInterval call leaving the width style static
下面的代码(在这里找到)显示了一种进度条,它向右侧移动得非常快,重新开始不停止。这是通过在setInterval中改变元素的宽度来实现的。
我怎么能建立一个函数,冻结进度条运动时调用(停止从改变的宽度冻结它在那一刻的函数调用)?
我正在使用prototype/javascript(代码中的jQuery行是一种快速添加类以发布这篇文章的方法,但我不使用jQuery)。
<style>
.thepower {
opacity: 0;
background-color: #191919;
padding: 4px;
position: absolute;
overflow: hidden;
width: 300px;
height: 24px;
top:150px;
left:84px;
-webkit-border-radius: 16px;
border-radius: 16px;
-webkit-box-shadow: inset 0 1px 2px #000, 0 1px 0 #2b2b2b;
box-shadow: inset 0 1px 2px #000, 0 1px 0 #2b2b2b;
}
.visible.thepower {
opacity: 1;
}
.thepower .inner {
background: #999;
display: block;
position: absolute;
overflow: hidden;
max-width: 97.5% !important;
height: 24px;
text-indent: -9999px;
-webkit-border-radius: 12px;
border-radius: 12px;
-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3),
inset 0 -1px 3px rgba(0, 0, 0, 0.4),
0 1px 1px #000;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3),
inset 0 -1px 3px rgba(0, 0, 0, 0.4),
0 1px 1px #000;
}
.green .inner {
background: #7EBD01;
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#7EBD01), to(#568201));
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
// How it works:
/*
var counter = 0 (inside a function, window.onload) - A local variable is defined and initialised at zero.
window.setInterval(function(){ ... }, 50) - An interval is defined, activating the function (first argument) every 50 milliseconds (20x a second, adjust to your own wishes)
(++counter % 101) - Increments the counter by one, modulo 101:
The modulo operator calculates the remainder after division, ie: 0 % 101 = 0, 100 % 101 = 100 and 200 % 101 = 99, 201 % 101 = 100, 202 % 101 = 100
*/
window.onload = function(){
var counter = 0;
window.setInterval(function(){
$(".green").addClass("visible") ;
document.querySelector('.green.thepower.visible .inner').style.width = (++counter % 101) + '%';
}, 10);
}
</script>
<div id="thepower" ad-outlet="thepower">
<div class="green thepower"><div class="inner"></div></div>
</div>
可以使用clearInterval
方法停止执行使用setInterval
设置的方法。首先将setInterval
的结果保存到某个变量:
var interval;
window.onload = function(){
var counter = 0;
interval = window.setInterval(function(){
$(".green").addClass("visible") ;
document.querySelector('.green.thepower.visible .inner').style.width = (++counter % 101) + '%';
}, 10);
}
之后调用clearInterval某处传递保存值作为参数:
clearInterval(interval);
这是一种从你停下来的地方停止并继续的方法:
添加到你的HTML:
<button id="toggleProgress">Click</button>
并将javascript更改为:
var counter = 0;
var timer = null;
function progressBar(){
if (timer) {
clearTimeout(timer);
timer = null;
return;
}
timer = window.setInterval(function(){
$(".green").addClass("visible");
document.querySelector('.green.thepower.visible .inner').style.width = (++counter % 101) + '%';
}, 10);
}
window.onload = function() {
progressBar();
$('#toggleProgress').click(function() {
progressBar();
});
};
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 将样式表插入iframe
- 跟踪在页面加载时应用内联样式的JavaScript
- 如果文本字段为空,则使用JavaScript应用CSS样式
- 定义完全独立的样式信息
- 如何处理10页以上的静态页眉/页脚
- W3C循环样式的JavaScript
- jQuery/JavaScript在线公文包表单-打印样式表
- 使用递归、Ramda.js和无点样式重构字符串的getPermutations()
- FF和Chromium中CSS样式按钮的外观差异
- Grunt-connect在根目录之外提供静态文件
- 如何首先设置样式<td>表中包含在窗体中的元素
- javascript问题正文样式
- 使用具有内联样式的tidy-html5
- DataTables-创建自定义分页样式(加载更多样式)
- 来自文档或下一个静态父级的事件委派
- 将谷歌地图样式转换为谷歌静态地图 URI 时出现问题
- 添加<样式>并且<脚本>在Magento静态块中
- Javascript将position属性更改为固定位置属性的每个样式的静态
- 函数阻止setInterval调用使width样式保持静态