set backgroundPosition and setInterval
set backgroundPosition and setInterval
所以我做了一个代码
function slide()
{
var x=1;
while (x<=512)
{
document.getElementById("slide").style.backgroundPosition = x+"px 0px";
x++;
}
}
JSFIDDLE
我想从backgroundPosition - 0
转换到backgroundPosition 512
。上面的代码立即从0变为512,我想让它像1<100ms中断>,2,3。。。
我尝试将x++
更改为setTimeout(function(){x++), 100)
,但它没有任何作用。
使用setTimeout()并调用函数:
function slide(x)
{
if(x==undefined) var x = 1;
if(x >= 512) return;
document.getElementById("slide").style.backgroundPosition = x+"px 0px";
x++;
setTimeout(function() {
// recall function and add other code if you want.
slide(x);
}, 100);
}
slide();
相关文章:
- Jquery setInterval and clearInterval
- JavaScript setTimeout and setInterval
- setInterval and jquery
- ClearInterval() and SetInterval()
- javascript setInterval() and Variable Scope
- setInterval and Ajax
- jquery next () and setInterval ()
- fadein() and fadeout() using with setinterval()
- SetInterval and clearInterval explanation
- Javascript setInterval and PHP Date()
- set backgroundPosition and setInterval
- AJAX and setInterval for window.location.hash
- setTimeOut() and setInterval() in Node
- While loop and setInterval()
- setInterval() and .delay()
- JS: setInterval and clearIntervals with jQuery
- getLocation and setInterval
- JavaScript: clear setInterval on blur, and resume on focus
- Setinterval and Focus Jquery
- setInterval and function