Note that there are some explanatory texts on larger screens.

plurals
  1. PODragging a div to resize an image.
    text
    copied!<p>I tried to setup a div so that when dragged, my image's width grows and shrinks the same distance. However, if I move my div 1px, my image's width changes more than 1px.</p> <p>Any suggestion? </p> <p><a href="http://jsfiddle.net/Ghm4N/1/" rel="nofollow">http://jsfiddle.net/Ghm4N/1/</a></p> <pre><code>function drag(el){ var dragging = dragging || false, x, handle_left, handle; el.onmousedown = function(e){ e.preventDefault(); dragging = true; handle = $(e.target); x = e.clientX; handle_left = handle.position().left; window.onmousemove = function(e){ if(dragging == true){ var distance_w = e.clientX - x; handle.css('left', (handle_left + distance_w)+'px'); $('.normal').css('width', $('.normal').width() + distance_w); console.log(distance_w); console.log($('.normal').width()+distance_w); return false; } }; window.onmouseup = function(e){ dragging &amp;&amp; (dragging = false); }; }; }; var el = $('.btn'); for(var i = 0; i &lt; el.length; i++){ drag(el[i]); }; </code></pre> <p>HTML</p> <pre><code>&lt;div class="container"&gt; &lt;div class="wp_normal"&gt; &lt;div class="normal"&gt; &lt;img src="http://25.media.tumblr.com/284263a747fda7627c76920071ef580d/tumblr_mf8mf2FzZm1qm9rypo1_1280.jpg"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="btn"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>CSS</p> <pre><code>.container{ position: relative; width: 1024px; margin: 0 auto; } .wp_normal{ position: absolute; z-index: 1; left: 0; top: 0; } .normal{ position: relative; width:500px; height: 280px; margin: 0 auto; overflow:hidden; } .normal img{ position:absolute; width: 1024px; height: 280px; } .btn{ position: absolute; z-index: 3; width: 100px; height: 100px; background-color: red; left: 450px; } </code></pre>
 

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