问答详情
源自:5-1 JS链式动画

startMove传入的参数,用指定的可以实现链式效果,但是用this却不行,求解

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>任意属性值</title>
<style type="text/css">
*{
margin: 0 auto;
padding: 0;

}

#box {
top:50px;
left:50px;
position: absolute;
list-style: none;
width:200px;
height: 200px;
background-color: yellow;
opacity:0.3;
border: 4px solid black;
*/
}
</style>
<script type="text/javascript">
window.onload=function(){
var box_1=document.getElementById('box');
box_1.onmouseover=function(){
startMove(this,'height',400,function(){
startMove(this,'opacity',100);
});};//将这里的this 换成指定的box_1就可以实现链式效果。不知道为什么this不行。
box_1.onmouseout=function(){
startMove(this,'height',200);};
var timer=null;
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}

function startMove(obj,attr,tar,fn){
clearInterval(timer);
timer=setInterval(function(){
var icurr=0;
if(attr=='opacity'){
icurr=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icurr=parseInt(getStyle(obj,attr));}
var incre=(tar-icurr)/10;
incre=incre>0?Math.ceil(incre):Math.floor(incre);
if(icurr==tar){
clearInterval(timer);
if(fn){
fn();}
}else{
if(attr=='opacity'){
obj.style[attr]=(icurr+incre)/100;}
else{
obj.style[attr]=icurr+incre+'px';}
}
},30)

}

}
</script>
</head>
<body>
<div id='box'>
</div>

</body>
</html>


提问者:myrtis 2015-09-25 07:36

个回答

  • Y_du
    2015-09-25 08:35:49
    已采纳

     对象不一样,box_1是一个具体的对象。而this这是是win本身。

  • 慕侠1994676
    2017-04-01 17:31:23

    oBtnPrev.onmouseout=oMarkLeft.onmouseout=function(){

          startMove(oBtnPrev,'opacity',0);

          };

    为什么我的谷歌运行总是错误了


  • Fstar
    2016-03-29 19:34:56

    this是指单击事件的目标节点,第一次是有效的,到了第二次调用this的时候this就没有对应的值了,第一次用的this没有被传进第二个回调函数