为了账号安全,请及时绑定邮箱和手机立即绑定

​多物体的练市反应怎么做?

多物体的练市反应怎么做?

<!DOCTYPE html>

<html>

<head>

<title></title>

<style type="text/css">

*{

margin: 0px;

padding: 0px;

}

ul,li{

list-style: none;

}

ul li{

width: 200px;

height: 100px;

background: yellow;

margin-bottom: 20px;

filter:alpha(opacity:30);

opacity:0.3;

}

</style>

<script type="text/javascript">

var timer;

window.onload = function(){

var square = document.getElementsByTagName("li");

for(var i = 0;i<square.length;i++){

square[i].timer = null;

square[i].onmouseover = function(){

startMove(this,"width",400,function(){

startMove(this,"height",200);

});

//startMove(this,"opacity",100);

}

square[i].onmouseout = function(){

startMove(this,"width",200,function(){

startMove(this,"height",100);

});

//startMove(this,"opacity",30);

}

}

/*

//单物体的链式反应

var li = document.getElementById("li1");

li.timer = null;

li.onmouseover = function(){

startMove(li,"width",400,function(){

startMove(li,"height",200);

})

}

li.onmouseout = function(){

startMove(li,"width",200,function(){

startMove(li,"height",100);

})

}*/

}


//var timer = null;

function startMove(obj,attr,target,fn){

clearInterval(obj.timer);

obj.timer = setInterval(function(){

//var icur = parseInt(getStyle(obj,attr));

var icur = 0;

if(attr == "opacity"){

icur = Math.round(parseFloat(getStyle(obj,attr))*100);

}else{

icur = parseInt(getStyle(obj,attr));

}

var speed = (target-icur)/8;

speed = speed>0?Math.ceil(speed):Math.floor(speed);

if(icur == target){

clearInterval(obj.timer);

if(fn){

fn();

}

}else{

if(attr == "opacity"){

obj.style.filter = "alpha(opacity:'+(icur+speed)+')";

obj.style.opacity = (icur + speed)/100;

}else{

obj.style[attr] = icur+speed+"px";

}

//obj.style[attr] = icur+speed+"px";

}

},30)

}

function getStyle(obj,attr){

if(obj.currentStyle){

return obj.currentStyle[attr];

}else{

return getComputedStyle(obj,false)[attr];

}

}

</script>

</head>

<body>

<ul>

<li id="li1"></li>

<li></li>

<li></li>

</ul>

</body>

</html>

报错,链式反应中的参数this应该是有问题的,应该怎么写?


正在回答

1 回答

在使用this之前定义一个变量。var  _this=this。之后用到this的地方。用变量_this代替

1 回复 有任何疑惑可以回复我~
#1

Aishah_MK 提问者

非常感谢!
2016-10-24 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

​多物体的练市反应怎么做?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信