如何在拖动时启动鼠标悬停事件

V.R*_*kov 26 javascript jquery draggable hover

当我将一个元素拖到另一个div我有鼠标悬停事件的元素上时,该事件不会触发.但是,如果我将鼠标悬停在它上面而不拖动它就可以了.

有没有办法检测元素上的悬停事件,如果我在其上拖动另一个?

emr*_*duz 13

以下是使用XY坐标解决方案的示例.

关于jsfiddle的工作示例

这个例子可以改进,但是一个很好的起点.

只需跟踪鼠标位置并检查它是否位于可放置对象的任何边界框内.因此,如果mouseup事件在其中任何一个上触发,则删除拖动的对象.

你也可以使用你拖动的对象的坐标来检测它是否在一个可放置的盒子上,但它需要更多的代码来找到边界框坐标,并且使用鼠标对我来说已经足够了.

代码使用jQuery但没有jQueryUI.我测试过Chrome,Firefox和Opera,但不是IE :)

如果jsfiddle无法访问,我也在这里添加代码.

HTML

<p>Drag orange boxes to grey ones</p>
<div class="droppable"></div>
<div class="droppable"></div>
<div class="droppable"></div>
<div class="droppable"></div>

<div class="draggable"></div>
<div class="draggable"></div>
<div class="draggable"></div>
Run Code Online (Sandbox Code Playgroud)

CSS

.droppable {
    width:50px;
    height:50px;
    float: left;
    background-color: #DDD;
    margin: 5px;
}

.draggable {
    width:40px;
    height:40px;
    float: right;
    background-color: #FC0;
    margin: 5px;
    cursor: pointer;
}

.dropped {
    background-color: #FC0;
}

.somethingover {
    background-color: #FCD;
}
Run Code Online (Sandbox Code Playgroud)

JS

var dragged, mousex, mousey, coordinates = [];

var continueDragging = function(e) {
    // Change the location of the draggable object
    dragged.css({
        "left": e.pageX - (dragged.width() / 2),
        "top": e.pageY - (dragged.height() / 2)
    });

    // Check if we hit any boxes
    for (var i in coordinates) {
        if (mousex >= coordinates[i].left && mousex <= coordinates[i].right) {
            if (mousey >= coordinates[i].top && mousey <= coordinates[i].bottom) {
                // Yes, the mouse is on a droppable area
                // Lets change the background color
                coordinates[i].dom.addClass("somethingover");
            }
        } else {
            // Nope, we did not hit any objects yet
            coordinates[i].dom.removeClass("somethingover");
        }
    }

    // Keep the last positions of the mouse coord.s
    mousex = e.pageX;
    mousey = e.pageY;
}

var endDragging = function(e) {
    // Remove document event listeners
    $(document).unbind("mousemove", continueDragging);
    $(document).unbind("mouseup", endDragging);

    // Check if we hit any boxes
    for (var i in coordinates) {
        if (mousex >= coordinates[i].left && mousex <= coordinates[i].right) {
            if (mousey >= coordinates[i].top && mousey <= coordinates[i].bottom) {
                // Yes, the mouse is on a droppable area
                droptarget = coordinates[i].dom;
                droptarget.removeClass("somethingover").addClass("dropped");
                dragged.hide("fast", function() {
                    $(this).remove();
                });
            }
        }
    }

    // Reset variables
    mousex = 0;
    mousey = 0;
    dragged = null;
    coordinates = [];
}

var startDragging = function(e) {
    // Find coordinates of the droppable bounding boxes
    $(".droppable").each(function() {
        var lefttop = $(this).offset();
        // and save them in a container for later access
        coordinates.push({
            dom: $(this),
            left: lefttop.left,
            top: lefttop.top,
            right: lefttop.left + $(this).width(),
            bottom: lefttop.top + $(this).height()
        });
    });

    // When the mouse down event is received
    if (e.type == "mousedown") {
        dragged = $(this);
        // Change the position of the draggable
        dragged.css({
            "left": e.pageX - (dragged.width() / 2),
            "top": e.pageY - (dragged.height() / 2),
            "position": "absolute"
        });
        // Bind the events for dragging and stopping
        $(document).bind("mousemove", continueDragging);
        $(document).bind("mouseup", endDragging);
    }
}

// Start the dragging
$(".draggable").bind("mousedown", startDragging);
Run Code Online (Sandbox Code Playgroud)


Wol*_*War 8

在所有提出的答案中,我没有看到最简单明了的答案(也许我在OP问题中遗漏了一些东西).但是,如果有人偶然发现这个问题,需要在纯JS中快速简单地解决问题.

您可以通过更改元素className ondragover并更改回原始类ondragleave来实现

my_element.ondragover = function(ev) {  
 ev.preventDefault();  
 this.className = 'myElem_dragover';  
}  
my_element.ondragleave = function(ev) {  
 ev.preventDefault();  
 this.className = 'myElem_orig';  
}
Run Code Online (Sandbox Code Playgroud)

CSS

.myElem_orig {     //this is your initial class for element
  top: 30px;
  left: 20px;
  .....
  background-color: blue;  
}  

.myElem_orig:hover {   //this is hover state, just changing bg color
  background-color: red;
}

.myElem_dragover { //new class, needs all attributes from original class
  top: 30px;
  left: 20px;
  ........ 
  background-color: red; //behaves the same like hover does
}
Run Code Online (Sandbox Code Playgroud)

编辑:
忘了提,你需要带回原班ondrop太多,否则格将留在的dragover类