Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 remaining divs.How do i do this in jquery? any idea?

I am using Nestable not draggable()

like image 245
Achaius Avatar asked Jul 27 '13 06:07

Achaius


People also ask

How do you scroll automatically to the bottom of the page using jQuery?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.

How do you automatically scroll to the bottom of a page?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.

How do I automatically scroll down in HTML?

You can use . scrollIntoView() for this. It will bring a specific element into the viewport.

How do I make Div scroll automatically?

Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.


2 Answers

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()
like image 165
apaul Avatar answered Sep 19 '22 13:09

apaul


If you want to know bottom of window you can check

$(window).height()

and $(window).scrollTop();

like image 25
Hamed Khosravi Avatar answered Sep 20 '22 13:09

Hamed Khosravi