

oBtn4.onclick=function(){
boxrun( oDiv3,'width',400,20,function(){
alert('aaa')
})
}
function boxrun(obj,attr,target,speed,callback){
clearInterval(obj.timrA)
var nowValue=https://bbs.csdn.net/topics/parseInt(getComputedStyle(obj,null)[attr])
if( nowValue>target){
speed=-speed
}
obj.timrA=setInterval(function(){
var oldValue=https://bbs.csdn.net/topics/parseInt(getComputedStyle(obj,null)[attr])
var newValue=https://bbs.csdn.net/topics/oldValue+speed
if( (speed<0&&newValue<target)||(speed>0&&newValue>target)){
newValue=https://bbs.csdn.net/topics/target
}
obj.style[attr]=newValue+"px"
if(newValue=https://bbs.csdn.net/topics/=target){
clearInterval(obj.timrA)
callback()&&callback
}
},30)
}
為什么newValue沒到達target值時執行了alert()
uj5u.com熱心網友回復:
大概是瀏覽器更新DOM屬性需要一定的時間,在更新之前彈出了提示框,導致更新中斷了。通過getComputedStyle得到的值已經是匹配的了,但元素的實際大小還未更新
uj5u.com熱心網友回復:
可以用DIV模擬彈出提示框,不阻塞行程,或者延時彈出提示框if (newValue == target) {
clearInterval(obj.timrA)
setTimeout(callback,30)
}
uj5u.com熱心網友回復:
callback && setTimeout(callback, 0);轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/197664.html
標籤:JavaScript
