2015-11-27 6 views
5

上にマウスを持っだから私はこのJSプログラム持っている: http://codepen.io/anon/pen/avgQVaは、入力した

$('.divider').draggable({ 
    axis: 'x', 
    drag: function(e, ui) { 
    $('.right').width(100 - ui.position.left); 
    $('.yellow').css('right', ui.position.left); 
    } 
}); 

を私は右に灰色の仕切りを移動することによって、赤RECTを明らかにすることができますが、私はいつでも私のマウスを追跡するために、この分周器を必要とします私はこのブロックに入る。これはどうすればいいですか?

+1

http://stackoverflow.com/a/12152244/631652をご確認ください – Parixit

答えて

4
// Handle the mouse move event on the parent div 
$("div:first").mousemove(function(e) { 
    // calculate the mouse position relative to the div element position on the page 
    var x = e.pageX - $(this).offset().left; 
    $('.divider').css('left', x); 
    $('.left').css('width', x); 
}); 

はそれを動作させるためには、私もCSSを微調整する必要がありました:

.left { 
    left: 0px; 
/* This makes the left div render "above" the others, so when we change its width it shows up */ 
    z-index: 1; 
} 

はデモ:http://codepen.io/anon/pen/epwQLr

+0

は私よりわずかに速いです。ここで動作します – ggzone

+0

ありがとう、しかし、それは私が必要とするexatclyではありません - 黄色のrectの中の内容はまだとどまるべきです。 – WhilseySoon

+0

私は答えを更新しました。 –

関連する問題