如何在Mouseover事件中使用Hover

时间:2012-12-18 20:19:45

标签: javascript jquery drag-and-drop

我正在尝试为我的网站使用jQuery UI或HTML5拖放功能创建拖放功能。我已经找出拖动部分,但我无法在特定丢弃区域中放弃。这是我的代码:

HTML

<ul id="grid">
     <li class="drop">
          <div class="drag" id="el_1"><div>
     </li>
     <li class="drop">
          <div class="drag" id="el_2"><div>
     </li>
     <li class="drop">
          <div class="drag" id="el_3"><div>
     </li>
     <li class="drop"></li>
     <li class="drop"></li>
     <li class="drop"></li>
</ul>
<script>
     $(function() {
          $('.handle').mousedown(drag_and_drop);
     });
</script>

CSS

#grid{
     position:relative;
     width:360px;
     height:auto;
}
.drop{
     width:100px;
     height:100px;
     margin:10px;
}
.drag{
     position:absolute;
     width:100px;
     height:100px;
}
#el_1{ top:10px; left:10px}
#el_2{ top:10px; left:130px}
#el_3{ top:10px; left:250px}

SCRIPT

function drag_and_drop(e){
        var drag = $(this);
        var drop = drag.parent();

        drag.addClass('draggable');

        drag_h = drag.outerHeight(),
        drag_w = drag.outerWidth(),
        pos_y = drag.offset().top + drag_h - e.pageY,
        pos_x = drag.offset().left + drag_w - e.pageX;

        $('.draggable').css('z-index', 9000).mousemove(function(e) {

/*

IN THIS MOUSEMOVE FUNCTION IS THERE ANY WAY TO DETECT IF THE DRAGGABLE DIV IS "HOVERING" OVER THE li.drop ELEMENTS?

*/

            $('.draggable').offset({
                top: e.pageY + pos_y - drag_h,
                left: e.pageX + pos_x - drag_w

            }); 
        }).mouseup(function() {
            drag.removeClass('draggable');
        });   
    e.preventDefault(); // stops the browser default drag method    
};

有没有办法,一旦我开始拖动一个对象,它可以检测到它是否超过特定的li.drop - 从中​​我可以将其放入?

1 个答案:

答案 0 :(得分:0)

您可以使用下面的内容来获取当前鼠标位置并编写另一个函数以查看该位置是否在可放置的divs范围内。好文章:

http://docs.jquery.com/Tutorials:Mouse_Position#How_do_I_find_the_mouse_position.3F