Smooth scrolling for ULS window to viewport

provide a jquery plugin function $.fn.scrollIntoView

Change-Id: Id0e5d3b385a13ab38089f8ae82e886c29a35e27a
This commit is contained in:
Santhosh Thottingal
2013-06-05 13:56:20 +05:30
parent db87f74096
commit 5541608278

View File

@@ -181,7 +181,7 @@
$( '.uls-menu' ).hide();
this.$menu.show();
this.$menu[0].scrollIntoView();
this.$menu.scrollIntoView();
this.shown = true;
if ( !this.isMobile() ) {
@@ -423,5 +423,35 @@
};
}
/*
* Simple scrollIntoView plugin.
* Scrolls the element to the viewport smoothly if it is not already.
*/
$.fn.scrollIntoView = function () {
return this.each( function () {
var scrollPosition,
padding = 10,
$window = $( window ),
windowHeight = $window.height(),
windowScrollTop = $window.scrollTop(),
windowBottom = windowScrollTop + windowHeight,
$element = $( this ),
panelHeight = $element.height(),
panelTop = $element.offset().top,
panelBottom = panelTop + panelHeight;
if ( ( panelTop < windowScrollTop ) || ( panelBottom > windowBottom ) ) {
if ( panelHeight > windowHeight ) {
scrollPosition = panelTop - padding;
} else {
scrollPosition = panelBottom - windowHeight + padding;
}
$( 'html, body' ).stop().animate( {
scrollTop: scrollPosition
}, 500 );
}
} );
}
$.fn.uls.Constructor = ULS;
} ( jQuery ) );