Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Example</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  body {
    font-family: sans-serif;
  }
  #target {
    position: absolute;
    left: 0px;
    top: 3em;
    border: 1px solid black;
    background-color: #00d;
    width: 20px;
    height: 20px;
  }
  #output {
    margin-top: 6em;
  }
</style>
</head>
<body>
  <input type="button" id="theButton" value="Click to animate">
  <div id="target"></div>
  <div id="output"></div>
</body>
</html>
 
jQuery(function($) {
  
  var animating = false;
  var direction = "+";
  
  $("#theButton").click(function() {
    // Are we already animating?
    // (Of course, ideally you'd disable the button or similar)
    if (animating) {
      // Yes, say we're busy
      display("Animation still in progress");
    }
    else {
      // No, do it
      display("Animating...");
      animating = true;
      $("#target").animate({
        left: direction + "100"
      }, 3000, function() {
        // Completion callback, we're not animating anymore
        animating = false;
        display("Done");
      });
      direction = direction === "+" ? "-" : "+";
    }
  });
  
  function display(msg) {
    $("<p>").html(String(msg)).appendTo("#output");
  }
  
});
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers