How to bind JQuery draggable to a hover event

血红的双手。 提交于 2019-12-23 03:05:15

问题


I'm having a tough time in finding a solution for following effect.

Imagine having a container "#container" containing:

  • An element "#element" that should only be draggable horizontally and within it's parent ("#container")
  • A handle "#handler" which should be the dragging handle.

Now the whole thing shold be dragged on hover.

Up to now I have following code, but don't know what to do:

<script>
    $("#element").dragable({handle: "#handler",  axis: "x", containment: "#container"});
</script>

How can I do this?


回答1:


You don't mention when you want it to -stop- dragging. My solution is to do so when the mouse leaves the container, though you don't have to. You could have the dragging element follow the mouse everywhere on the screen like those creepy X-eyes on screen...

http://jsfiddle.net/pabo/5xAkQ/

// set the element to be draggable
$('div#drag').draggable({
    containment: "parent"
});

// mousing over the handle starts dragging
$('div#handle').hover(function (e) {
    e.type = 'mousedown';
    $('div#drag').trigger(e);
});

// mouse leaving the container stops dragging
$('div#container').mouseleave(function (e) {
   e.type = 'mouseup';
    $('div#drag').trigger(e);
});


来源:https://stackoverflow.com/questions/9920473/how-to-bind-jquery-draggable-to-a-hover-event

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