2010-10-28 109 views

回答

1
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的widt H():

http://api.jquery.com/width/

jQuery是變得非常受歡迎,甚至被大公司所使用,所以你可以考慮使用它。

+0

謝謝哥們你能不能幫我多一點,讓想我想打一個進度條的影響,我有一個div誰的寬度是在開始1個像素,然後我想增加它的寬度爲1個像素與setTimeout的幫助我該怎麼做?其背景已經設置爲紅色 – Seeker 2010-10-28 06:59:24

0
<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>