2010-10-28 110 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>