Увеличьте ширину Div с помощью Javascript setTimeout () - PullRequest
0 голосов
/ 28 октября 2010

Как мы можем увеличить или уменьшить ширину DIV с помощью setTimeout () ??

Ответы [ 2 ]

1 голос
/ 28 октября 2010
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 ():

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

jQuery становится очень популярным и дажеиспользуется большими корпорациями, так что вы можете рассмотреть возможность его использования.

0 голосов
/ 01 июля 2011
<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>
...