简体   繁体   English

jQuery Droppout事件触发得太早

[英]JQuery droppable out event triggers too early

I am using JQuery droppable container for dropping other elements. 我正在使用JQuery droppable容器删除其他元素。 A added hoverClass when users mouse is over the container. 当用户将鼠标悬停在容器上方时,添加了hoverClass。 In this class I set new width of the container. 在这个类中,我设置了容器的新宽度。 But this new width in not considered when mouse is leaving the container. 但是,当鼠标离开容器时,不会考虑此新宽度。 Out event is triggered as container would have old width. 由于容器将具有旧宽度,因此触发了Out事件。 What is even more strange is that if I move dragging element very slowly that new width of container is considered. 更奇怪的是,如果我缓慢移动拖动元素,则会考虑新的容器宽度。

I have made the fiddle example here: http://jsfiddle.net/SLGdE/709/ 我在这里做了小提琴的例子: http : //jsfiddle.net/SLGdE/709/

Tested in Chrome and FF. 经过Chrome和FF测试。

How can I achieve that new width of droppable container is considered? 如何实现考虑可放置容器的新宽度?

$(function() {
    $("#draggable").draggable({
      revert:true
    });

    $("#droppable").droppable({
        tolerance: "pointer",
        hoverClass: "over"
    });
});

You need to add a class and customize it. 您需要添加一个类并对其进行自定义。 From jQueryUi documentation : 从jQueryUi 文档中

$( "#droppable" ).droppable({
  drop: function( event, ui ) {
    $( this )
      .addClass( "ui-state-highlight" )
  }
});

Try this http://jsfiddle.net/SLGdE/710/ : 试试这个http://jsfiddle.net/SLGdE/710/

$(function() {
$( "#draggable" ).draggable({revert:true});
$( "#droppable" ).droppable({
    tolerance: "pointer",
    hoverClass: "over",
    over:function(event, ui){
        $(this).width($(this).width());//This is the update
    },
    out:function(event, ui){
        //alert("out")        
    }
 });
});

Add refreshPositions with value true to your draggable . 将值true refreshPositions添加到draggable Note, however, that there will be a performance hit per https://api.jqueryui.com/draggable/#option-refreshPositions . 但是请注意,每个https://api.jqueryui.com/draggable/#option-refreshPositions都会对性能造成影响。 So it is likely that your workaround is a better option, but maybe this answer could help someone else. 因此,您的解决方法可能是一个更好的选择,但也许此答案可以帮助其他人。

$( "#draggable" ).draggable({
    revert:true, 
    refreshPositions: true
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM