Click here to Skip to main content
15,896,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a basic animation which toggles an image down the screen. Is there an easy way to move it back up to the top of the screen once it has moved down a certain distance?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
jQuery(document).ready(function(){
  $('#clickme').click(function() {
  $('#rds').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
  }, 5000, function() {
    // Animation complete.
  });
});
});
</script>
<div id="clickme">
  Click here
</div>
<img id="rds" src="rds.jpg" alt="" width="100" height="123"
  style="position: relative; left: 10px;" />
</body>

</html>
Posted

1 solution

Your code does not move the image "down" the screen; it moves it to the right. So I am going with the assumption that you want to reset it to the left after it has traveled to the right a certain distance.

Just add the following code to the click event handler:
JavaScript
if (parseInt($('#rds').css('left')) > 400) {
    $('#rds').css('left', '10px');
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900