summaryrefslogtreecommitdiff
path: root/lib/jquery.localscroll.js
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2013-02-23 01:09:30 -0500
committerBharat Mediratta <bharat@menalto.com>2013-02-23 01:09:30 -0500
commit9d6ed037bb5b346fba900c17ba57b34dae3b6fc8 (patch)
tree2caddad82bfa6a6ad5f45ea0199c2d0539278026 /lib/jquery.localscroll.js
parent42878556cf38bccfea2b9b38ab2c8dd90d1ebc88 (diff)
parentf95159c0b886a2ff724d6fa9a9315460186b02ef (diff)
Merge branch 'jquery_190'
Diffstat (limited to 'lib/jquery.localscroll.js')
-rw-r--r--lib/jquery.localscroll.js138
1 files changed, 131 insertions, 7 deletions
diff --git a/lib/jquery.localscroll.js b/lib/jquery.localscroll.js
index fa583a45..b2acafa3 100644
--- a/lib/jquery.localscroll.js
+++ b/lib/jquery.localscroll.js
@@ -1,9 +1,133 @@
-/**
- * jQuery.LocalScroll - Animated scrolling navigation, using anchors.
- * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
+/*!
+ * jQuery.LocalScroll
+ * Copyright (c) 2007-2010 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Dual licensed under MIT and GPL.
- * Date: 3/11/2009
+ * Date: 05/31/2010
+ *
+ * @projectDescription Animated scrolling navigation, using anchors.
+ * http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html
* @author Ariel Flesler
- * @version 1.2.7
- **/
-;(function($){var l=location.href.replace(/#.*/,'');var g=$.localScroll=function(a){$('body').localScroll(a)};g.defaults={duration:1e3,axis:'y',event:'click',stop:true,target:window,reset:true};g.hash=function(a){if(location.hash){a=$.extend({},g.defaults,a);a.hash=false;if(a.reset){var e=a.duration;delete a.duration;$(a.target).scrollTo(0,a);a.duration=e}i(0,location,a)}};$.fn.localScroll=function(b){b=$.extend({},g.defaults,b);return b.lazy?this.bind(b.event,function(a){var e=$([a.target,a.target.parentNode]).filter(d)[0];if(e)i(a,e,b)}):this.find('a,area').filter(d).bind(b.event,function(a){i(a,this,b)}).end().end();function d(){return!!this.href&&!!this.hash&&this.href.replace(this.hash,'')==l&&(!b.filter||$(this).is(b.filter))}};function i(a,e,b){var d=e.hash.slice(1),f=document.getElementById(d)||document.getElementsByName(d)[0];if(!f)return;if(a)a.preventDefault();var h=$(b.target);if(b.lock&&h.is(':animated')||b.onBefore&&b.onBefore.call(b,a,f,h)===false)return;if(b.stop)h.stop(true);if(b.hash){var j=f.id==d?'id':'name',k=$('<a> </a>').attr(j,d).css({position:'absolute',top:$(window).scrollTop(),left:$(window).scrollLeft()});f[j]='';$('body').prepend(k);location=e.hash;k.remove();f[j]=d}h.scrollTo(f,b).trigger('notify.serialScroll',[f])}})(jQuery); \ No newline at end of file
+ * @version 1.2.8b
+ *
+ * @id jQuery.fn.localScroll
+ * @param {Object} settings Hash of settings, it is passed in to jQuery.ScrollTo, none is required.
+ * @return {jQuery} Returns the same jQuery object, for chaining.
+ *
+ * @example $('ul.links').localScroll();
+ *
+ * @example $('ul.links').localScroll({ filter:'.animated', duration:400, axis:'x' });
+ *
+ * @example $.localScroll({ target:'#pane', axis:'xy', queue:true, event:'mouseover' });
+ *
+ * Notes:
+ * - The plugin requires jQuery.ScrollTo.
+ * - The hash of settings, is passed to jQuery.ScrollTo, so the settings are valid for that plugin as well.
+ * - jQuery.localScroll can be used if the desired links, are all over the document, it accepts the same settings.
+ * - If the setting 'lazy' is set to true, then the binding will still work for later added anchors.
+ * - If onBefore returns false, the event is ignored.
+ */
+;(function( $ ){
+ var URI = location.href.replace(/#.*/,''); // local url without hash
+
+ var $localScroll = $.localScroll = function( settings ){
+ $('body').localScroll( settings );
+ };
+
+ // Many of these defaults, belong to jQuery.ScrollTo, check it's demo for an example of each option.
+ // @see http://flesler.demos.com/jquery/scrollTo/
+ // The defaults are public and can be overriden.
+ $localScroll.defaults = {
+ duration:1000, // How long to animate.
+ axis:'y', // Which of top and left should be modified.
+ event:'click', // On which event to react.
+ stop:true, // Avoid queuing animations
+ target: window, // What to scroll (selector or element). The whole window by default.
+ reset: true // Used by $.localScroll.hash. If true, elements' scroll is resetted before actual scrolling
+ /*
+ lock:false, // ignore events if already animating
+ lazy:false, // if true, links can be added later, and will still work.
+ filter:null, // filter some anchors out of the matched elements.
+ hash: false // if true, the hash of the selected link, will appear on the address bar.
+ */
+ };
+
+ // If the URL contains a hash, it will scroll to the pointed element
+ $localScroll.hash = function( settings ){
+ if( location.hash ){
+ settings = $.extend( {}, $localScroll.defaults, settings );
+ settings.hash = false; // can't be true
+
+ if( settings.reset ){
+ var d = settings.duration;
+ delete settings.duration;
+ $(settings.target).scrollTo( 0, settings );
+ settings.duration = d;
+ }
+ scroll( 0, location, settings );
+ }
+ };
+
+ $.fn.localScroll = function( settings ){
+ settings = $.extend( {}, $localScroll.defaults, settings );
+
+ return settings.lazy ?
+ // use event delegation, more links can be added later.
+ this.bind( settings.event, function( e ){
+ // Could use closest(), but that would leave out jQuery -1.3.x
+ var a = $([e.target, e.target.parentNode]).filter(filter)[0];
+ // if a valid link was clicked
+ if( a )
+ scroll( e, a, settings ); // do scroll.
+ }) :
+ // bind concretely, to each matching link
+ this.find('a,area')
+ .filter( filter ).bind( settings.event, function(e){
+ scroll( e, this, settings );
+ }).end()
+ .end();
+
+ function filter(){// is this a link that points to an anchor and passes a possible filter ? href is checked to avoid a bug in FF.
+ return !!this.href && !!this.hash && this.href.replace(this.hash,'') == URI && (!settings.filter || $(this).is( settings.filter ));
+ };
+ };
+
+ function scroll( e, link, settings ){
+ var id = link.hash.slice(1),
+ elem = document.getElementById(id) || document.getElementsByName(id)[0];
+
+ if ( !elem )
+ return;
+
+ if( e )
+ e.preventDefault();
+
+ var $target = $( settings.target );
+
+ if( settings.lock && $target.is(':animated') ||
+ settings.onBefore && settings.onBefore(e, elem, $target) === false )
+ return;
+
+ if( settings.stop )
+ $target._scrollable().stop(true); // remove all its animations
+
+ if( settings.hash ){
+ var attr = elem.id == id ? 'id' : 'name',
+ $a = $('<a> </a>').attr(attr, id).css({
+ position:'absolute',
+ top: $(window).scrollTop(),
+ left: $(window).scrollLeft()
+ });
+
+ elem[attr] = '';
+ $('body').prepend($a);
+ location = link.hash;
+ $a.remove();
+ elem[attr] = id;
+ }
+
+ $target
+ .scrollTo( elem, settings ) // do scroll
+ .trigger('notify.serialScroll',[elem]); // notify serialScroll about this change
+ };
+
+})( jQuery ); \ No newline at end of file