Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Animation Left to right not working

Why is this not working? This should be animate the arrow from left to right and then back to left again and so on, but this is not working. Why is that?

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>
like image 916
Jie Avatar asked Mar 08 '23 22:03

Jie


1 Answers

Add style position:absolute to the icon

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>
like image 176
XYZ Avatar answered Mar 27 '23 05:03

XYZ