[英]Increase Div Width with Javascript setTimeout()
我們如何使用setTimeout()增加或減小DIV的寬度?
setTimeout(function() { document.getElementById('foo').style.width = '300px' },
2000);
相關博客和規范:
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
https://developer.mozilla.org/en/DOM/window.setTimeout
如果要首先通過獲取當前寬度來增加或減少,則可以查看clientWidth():
https://developer.mozilla.org/en/DOM/element.clientWidth
或使用jQuery的width():
jQuery變得非常流行,甚至被大公司使用,因此您可以考慮使用它。
<div id="progressbar" style="background-color:green; margin-top: 10px; width:0px; height:10px;"></div>
<script>
// set var to rep width of progress bar div
var dwidth=0;
// declare global variable for instance of Interval Timer so other funcs can access it.
IntervalId =0;
// grow progress bar each second
IntervalId = setInterval(function() { dwidth +=5; document.getElementById('progressbar').style.width = dwidth+'px';}, 1000);
// stop progress bar after 10 seconds
/* in real world an event handler would do this when iframe handling upload script loads response from upload */
setTimeout(function() { clearInterval ( IntervalId ); document.getElementById('progressbar').style.display='none' , 10000);
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.