//call this function when we want to initiate the listener for moving the mouse
//For instance, call this function once the user starts dragging an element
function startMouseMove() {
$document.on('mousemove', scrollWindow);
}
function scrollWindow(e) {
const y = e.pageY; //This collects details on where your mouse is located vertically
const container = $('.my-container'); //Replace this with the element class(.) or id(#) that needs to scroll
const buffer = 200; //You can set this directly to a number or dynamically find some type of buffer for the top and bottom of your page
const containerBottom = container.offset().top + container.outerHeight(); //Find the bottom of the container
const containerTop = container.offset().top; //Find the top position of the container
const scrollPosition = container.scrollTop(); //Find the current scroll position
const scrollRate = 20; //increase or decrease this to speed up or slow down scrolling
if (containerBottom - y < buffer) { //If the bottom of the container's position minus y is less than the buffer, scroll down!
container.scrollTop(scrollPosition + scrollRate);
} else if (containerTop + y < buffer) { //If the top of the container's position plus y is less than the buffer, scroll up!
container.scrollTop(scrollPosition - scrollRate);
}
}
//call this function when we want to stop the listener for moving the mouse
//For instance, call this function once the user drops a dragging element
function stopMouseMove() {
$document.unbind('mousemove', scrollWindow);
}