function coffee(){
var a= document.getElementById("cover").style.width;
var b= a.substring(0,3);//substring方法截取两个索引号之间的字符串
var picwidth=Number(b);
while(picwidth>0){
var i=0;
picwidth=picwidth-50;
var t=setTimeout("changeWidth(picwidth)",50);
}
}
function changeWidth(a){
document.getElementById("cover").style.width=a;
}
就是得不出正确的结果
我都试了一下 var t=setTimeout("changeWidth(picwidth)",50); 是这句话有问题。
希望的效果是触发coffee方法 每50ms id为cover的div的长度就变化一下 求高手指导
我知道jquery可以实现而且很简单,但是我要用纯js代码来实现
谢谢各位高手先
1.[quote]setTimeout() 只执行 code 一次。如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout()。[/quote]
while循环,是不会等setTimeout执行完才执行下一次循环的
2.
因此你要让你的代码每50ms执行一次,那就得这样写
setInterval(function(){},50);
[code="js"]
var picwidth= 0;
function coffee(){
var a= document.getElementById("cover").style.width;
var b= a.substring(0,3);//substring方法截取两个索引号之间的字符串
picwidth=Number(b);
var t=setInterval(function(){
if(picwidth <=0){
clearInterval(t);
}
changeWidth(picwidth);
picwidth=picwidth-50;
},50);
}
function changeWidth(a){
document.getElementById("cover").style.width=a;
}
[/code]