https://jsfiddle.net/71zono60/1/
Basically, as I said in my comment, you want to trigger the scrolling on hover, hence the
$( '.widget' ).mouseover( ... );
And what you want to do, is check if the widget's position is out of the window's box, like so:
var winHeight = $( window ).height( );
var scrollTop = $( window ).scrollTop( );
var winBottom = winHeight + scrollTop; // this gives the vertical position of the last visible pixel
var thisPos = $( this ).offset( ).top;
var thisPosBottom = thisPos + $( this ).height( ); // this gives the vertical position of the widget's last pixel
// so if the widget's last pixel comes AFTER the window's last visible one, we need to trigger the scrolling function
if( thisPosBottom > winBottom )
$( 'body' ).animate( {
scrollTop: scrollTop + ( thisPosBottom - winBottom ) + 10
}, 500 );
Enjoy! :)
Note:
I decided not to set the scrollTop
to thisPos
( which is the widget's top offset ), because in this case your mouse is more than likely going to end up on another widget, which means it'll be highlighted and this will be triggered over and over until it reaches the end of the page, and you don't want that!