Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><strong>Requirements</strong></p> <ul> <li>boxes are hidden until whole page loads</li> <li>boxes are set to random positions around browser view port</li> <li>boxes need to animate to their proper positions</li> </ul> </blockquote> <p>You can store the original position and after your random placement start animating them to go back to their starting position. I also cached any DOM reference which were repeated to speed up the code execution (most likely not noticeably but it's good practice)</p> <h3><a href="http://jsfiddle.net/az2SL/" rel="nofollow">DEMO</a> - Boxes placed at random and then animated back to their proper positions.</h3> <p>I have separated the 2 actions, placing the boxes at random locations using your original code but now also storing the original position in associated data using <a href="http://api.jquery.com/data/" rel="nofollow"><strong>jQuery's Data</strong></a> which is then used to animate them back to their proper positions.</p> <p>You can now randomise and re-set the boxes on demand by calling either method:</p> <pre><code>$(document).ready(function() { var h = $(window).height(); var w = $(window).width(); var $allBoxes = $('#intro .box'); placeBoxesRandomly($allBoxes, h, w); placeBoxesToDataStorePositions($allBoxes); });​ </code></pre> <p><code>placeBoxesRandomly</code> still does what it did before except it now also associates the original starting position data with the elements:</p> <pre><code>function placeBoxesRandomly($boxes, h, w) { $boxes.each(function() { var $box = $(this); var position = $box.position(); tLeft = w - Math.floor(Math.random() * 900), tTop = h - Math.floor(Math.random() * 900); $box.css({ "left": tLeft, "top": tTop }); $box.data("start-position-left", position.left); $box.data("start-position-top", position.top); }); } </code></pre> <p><code>placeBoxesToDataStorePositions</code> places the boxes to the positions stored in the associated data.</p> <pre><code>function placeBoxesToDataStorePositions($boxes) { $boxes.each(function() { var $box = $(this); $box.animate({ "left": $box.data("start-position-left"), "top": $box.data("start-position-top") }, 3000); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload