Jquery动画从左到右不起作用

时间:2017-06-06 08:47:09

标签: javascript jquery html css

为什么这不起作用?这应该是从左到右,然后再从左到右依次为箭头设置动画,依此类推,但这不起作用。那是为什么?



function moveRight(){
    $('#myIcon').animate({left: "+=50"}, 1000, function(){
        $('#myIcon').animate({left: "-=50"}, 1000, moveRight)
    })
}
moveRight();

a{
  text-decoration: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="btn btn-danger btn-lg">
<span id="myIcon">←</span>&nbsp; Go Back
</a>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:7)

将样式position:absolute添加到图标

function moveRight(){
    $('#myIcon').animate({left: "+=50"}, 1000, function(){
        $('#myIcon').animate({left: "-=50"}, 1000, moveRight)
    })
}
moveRight();
a{
  text-decoration: none;
}

#myIcon{
 position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="btn btn-danger btn-lg">
<span id="myIcon">←</span>&nbsp; Go Back
</a>