jQuery droppable and scrollable divs

后端 未结 3 1268
Happy的楠姐
Happy的楠姐 2020-12-10 14:38

I have a little problem with jQuery UI\'s droppable component, but I\'m not quite sure whether I have that problem because of my code or because of a bug in the component.

3条回答
  •  青春惊慌失措
    2020-12-10 15:13

    Check the droppable element's bounds against the parent container and break the execution of the function if the droppable's bottom is above the parent's top or the droppable's top is beneath the parent's bottom:

    $('.item').droppable( {
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        accept: "#draggable",
        drop: function( event, ui ) {
            var cTop = $(this).closest(".box").position().top + parseInt($(this).closest(".box").css("margin-top"));
            var cBtm = $(this).closest(".box").position().top + parseInt($(this).closest(".box").css("margin-top")) + $(this).closest(".box").height();
            var dTop = $(this).position().top + parseInt($(this).css("margin-top"));
            var dBtm = $(this).position().top + parseInt($(this).css("margin-top")) + $(this).height()
    
            if (dBtm > cTop && dTop < cBtm) {
                alert("Dropped.");
            }
        }
    });
    

    Example: http://jsfiddle.net/lthibodeaux/2p56Y/6/

    I realize it's not elegant, but it seems workable. I admit to only cursory testing of this script.

提交回复
热议问题