How to scroll the window automatically when mouse moves bottom of the page using jquery

后端 未结 4 651
执笔经年
执笔经年 2020-12-31 05:44

I have 50 divs,But in my window it shows only 25,I do drag and drop activity on these divs.So If i drag my first div near 25th div,It should scroll automatically to show the

4条回答
  •  遥遥无期
    2020-12-31 06:27

    This will need some fine tuning depending on your specific use case but it seems to work fairly well.

    Working Example

    $('.dd').nestable({ /* config options */
    });
    
    $(window).mousemove(function (e) {
        var x = $(window).innerHeight() - 50,
            y = $(window).scrollTop() + 50;
        if ($('.dd-dragel').offset().top > x) {
            //Down
            $('html, body').animate({
                scrollTop: 300 // adjust number of px to scroll down
            }, 600);
        }
        if ($('.dd-dragel').offset().top < y) {
            //Up
            $('html, body').animate({
                scrollTop: 0
            }, 600);
        } else {
            $('html, body').animate({
    
            });
        }
    });
    

    Related API documentation:

    • .mousemove()
    • .innerHeight()
    • .scrollTop()
    • .offset()
    • .animate()

提交回复
热议问题