jQuery UI, Draggable, Droppable, Auto Scroll

前端 未结 2 2072
耶瑟儿~
耶瑟儿~ 2021-02-09 11:06

I have a set of droppable li elements that accepts a draggable icon. The list of items is in a scrollable div element. I put together a simple example here: http://www.nerdydork

相关标签:
2条回答
  • 2021-02-09 11:29

    Setting the "refreshPositions: true" option for the draggable will cause jQuery to recalculate the offsets on every mouse event. That should fix your problem.

    0 讨论(0)
  • 2021-02-09 11:47

    you can also try to use an interval function: http://jsfiddle.net/msszhwzf/6/

    for (var i = 0; i < 10; i++) {
        $("#sortableContainer").append('<div class="sortable"></div>');
        $("#droppableContainer").append('<div class="droppable"></div>');
    };
    
    var adding = 0
    var scrollInterval = null;
    
    $("#sortableContainer").sortable({
        refreshPositions: true,
        start: function (e, ui) {
            scrollInterval = setInterval(function () {
                var foo = $("#droppableContainer").scrollTop();
                $("#droppableContainer").scrollTop(foo + adding);
            }, 50)
        },
        stop: function (e, ui) {
            clearInterval(scrollInterval);
        },
        sort: function (e, ui) {
            var top = e.pageY - $("#droppableContainer").offset().top
            if (top < 10) {
                adding = -15
            } else if (top > $("#droppableContainer").height() - 10) {
                adding = 15
            } else {
                adding = 0
            }
        },
    });
    $(".droppable").droppable({
        hoverClass: "active",
        accept: ".sortable",
        drop: function (event, ui) {
            ui.draggable.remove();
        },
    })
    
    0 讨论(0)
提交回复
热议问题