jQuery Drag and Follow Mouse

眉间皱痕 提交于 2019-12-23 22:23:51

问题


I'm trying to create a two-column slideable region with a drag-bar in the center, see this Fiddle: http://jsfiddle.net/W7tGj/2/

I'm trying to avoid adding jQ-UI to the mix, so any help would be appreciated. I feel like I'm missing something simple.


回答1:


First : - add container div to check mousemove

<div id="content-div">
  <div id="left-panel">f</div>

  <div id="drag-bar">f</div>

  <div id="right-panel">f</div>
</div>

Second : - add mousemove event into div container

var movebar = false; 

$('#drag-bar').mousedown(function(e){
    movebar = true;
});

$('#drag-bar').mouseup(function(e){
    movebar = false;
});

$('#content-div').mousemove(function(e){
    if(movebar)
    {
        var x = e.pageX;
        $('#left-panel').css({'width': x+'px'});
        $('#right-panel').css({'margin-left': (x+5)+'px'});
    }
});



回答2:


try it this way, http://jsfiddle.net/W7tGj/6/, although it still cannot support drag, it does do the right thing when mouse is down



来源:https://stackoverflow.com/questions/12151768/jquery-drag-and-follow-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!