为什么下面函数用到的timer,在上面的函数用不用声明?
timer 和alpha 这两个没弄到公共属性时候,老师都给var声明了,但是放到上面的函数中就没声明?
<script type="text/javascript">
window.onload=function(){
var oDiv=document.getElementsByTagName('div');
for (var i = 0; i < oDiv.length; i++) {
oDiv[i].timer=null;
oDiv[i].alpha=30;
oDiv[i].onmouseover=function(){
change(this,100);
};
oDiv[i].onmouseout=function(){
change(this,30);
}
}
}
/*var alpha=30;*/
function change(obj,Target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var speed=0;
if(obj.alpha>Target){
speed=-10;
}else{
speed=10;
}
if(obj.alpha==Target){
clearInterval(obj.timer)
}else{
obj.alpha+=speed;
obj.style.filter=='alpha(opacity:'+obj.alpha+')';
obj.style.opacity=obj.alpha/100;
}
}, 30)
}