Animation How to - Click to fly in and out








Question

We would like to know how to click to fly in and out.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<style type='text/css'>
#id {<!--from  ww w.jav  a2s .c  o  m-->
  position: fixed;
  left: -160px;
  color:red;
  background:red;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    var i=0;
    jQuery("#btn").click(function() {
        var t = (Math.pow(-1,i))*160;
        jQuery("#id").stop(true, false).animate({
            left: t
        }, 500);
        i = (i>10) ? 0 : (i+1);
    });
});
</script>
</head>
<body>
  <img id="id" src="http://www.java2s.com/style/download.png" />
  <br />
  <br />
  <input id="btn" type="submit" value="click" />
</body>
</html>

The code above is rendered as follows: