From 5541608278db49791f82c3dbd8de5603b9944536 Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Wed, 5 Jun 2013 13:56:20 +0530 Subject: [PATCH] Smooth scrolling for ULS window to viewport provide a jquery plugin function $.fn.scrollIntoView Change-Id: Id0e5d3b385a13ab38089f8ae82e886c29a35e27a --- src/jquery.uls.core.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/jquery.uls.core.js b/src/jquery.uls.core.js index 4593f8d..30549cb 100644 --- a/src/jquery.uls.core.js +++ b/src/jquery.uls.core.js @@ -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 ) );