錯誤提示Cannot read property 'timer' of undefined求解
function startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer= setInterval(function(){
var flag=true;
for(var attr in json){
//取當(dāng)前屬性的值
var cur= 0;
if(attr=='opacity'){
cur= Math.round(parseFloat(getStyle(obj,attr)*100)); //能讓范圍回到0~100
}
else{
cur = parseInt(getStyle(obj,attr));
}
//定義速度
var speed = (json[attr] - cur)/ 8 ;
// 取整速度
speed= speed >0?Math.ceil(speed):Math.floor(speed);
//立flag
if(cur!=json[attr]){
flag=false;
if(attr== 'opacity'){
//firefox瀏覽器
obj.style.opacity = (cur + speed)/100;
//IE瀏覽器?
obj.style.filter ?= 'alpha:(opacity:'+(cur+speed)+')';
}
else{
obj.style[attr] ? = cur + speed +'px';
}
}
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}
}
}
},30)
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];
}
}
錯誤位置js代碼第二行。這個框架在上一節(jié)里都沒有問題。
2016-09-13
我的可以正常運行。我的是 Dreamweaver CC 2015.02 Release 7884 Build 。你可以嘗試一下在 .html 頁面的 for 循環(huán)中為每個 obj 設(shè)置一下 timer 。像下邊這樣: