Note that there are some explanatory texts on larger screens.

plurals
  1. POAdjust position of a group of adjacent divs in non-linear fashion
    primarykey
    data
    text
    <p>This isn't so much a jQuery question as it is an overall conceptual question. In my example I can populate a container with divs that have a top value set in a nonlinear fashion. The top value of each one is calculated based on a formula that takes into account the top position of the one to its left as well as the height of the container (line 33 of fiddle).</p> <pre><code>//this formula sets the top value for each new child added to the container //height is 100% of its parent which is 20% of the body //newOne:last is the most recently added child and will have an initial top value of 10% parseInt($(this).next().css('top'), 10) / $('#queue').height()) * 75 + (parseInt($('.newOne:last').css('top'), 10) * 2) + '%' </code></pre> <p>I more of less stumbled upon this by chance and it seems to work 'ok', but if an optimization is obvious to you, please point it out :) What I'm having trouble coming up with is an elegant formula for how to adjust the children smoothly during a drag event. I'm thinking the top value needs to be adjusted based on some manipulation of the left offset, but after hours of experimenting, I haven't found anything that keeps the original position intact when I start dragging and continues adjusting the values smoothly during my drag. The children should gradually approach a minimum top value of 10% as I drag left (child with left offset of 0 will have a top value of 10%), and gradually move away from that top value back toward their initial position as I drag right. </p> <pre><code>$('#queue').draggable({ axis: "x", scroll: false, drag: function(){ //adjust values of each child $('.newOne').each(function(){ var percentLeft = $(this).offset().left / $('footer').width() * 100 var thisLeft = parseInt($(this).css('left'), 10) / $(window).width() * 100; var thisTop = parseInt($(this).css('top'), 10) / $('#queue').height() * 100; if (percentLeft &gt;= 0){ //top value of each one gradually decreases... //as it gets closer to an offset value of 0 and minimum top value of 10% //non-linear attempt but not even close //$(this).css('top', $(this).css('top', 10 + (thisTop - 10 / thisLeft) + '%')); //linear step $(this).css({'top': 8 + (percentLeft/2) + '%'}); } }); } }); </code></pre> <p><a href="http://jsfiddle.net/5RRCS/17/" rel="nofollow">http://jsfiddle.net/5RRCS/17/</a></p> <p>P.S. I know I'm asking a lot here, but hopefully someone is up to the challenge :)</p> <p><strong>Update:</strong> Stumbled onto exp method and did something like this:</p> <pre><code>adjustTop = function(offset){ return 100 * (1.0-Math.min(0.98,(0.83 + ( 0.17/ (Math.exp(0.007*offset))) )) ) + '%'; }; $(this).css('top', adjustTop($(this).offset().left) ); </code></pre>
    singulars
    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. 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