| 1 |
/*! |
| 2 |
* jQuery.localScroll |
| 3 |
* Copyright (c) 2007 Ariel Flesler - aflesler<a>gmail<d>com | https://github.com/flesler |
| 4 |
* Licensed under MIT |
| 5 |
* http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html |
| 6 |
* @author Ariel Flesler |
| 7 |
* @version 2.0.0 |
| 8 |
*/ |
| 9 |
;(function(plugin) { |
| 10 |
// AMD Support |
| 11 |
if (typeof define === 'function' && define.amd) { |
| 12 |
define(['jquery'], plugin); |
| 13 |
} else { |
| 14 |
plugin(jQuery); |
| 15 |
} |
| 16 |
}(function($) { |
| 17 |
var URI = location.href.replace(/#.*/, ''); // local url without hash |
| 18 |
|
| 19 |
var $localScroll = $.localScroll = function(settings) { |
| 20 |
$('body').localScroll(settings); |
| 21 |
}; |
| 22 |
|
| 23 |
// Many of these defaults, belong to jQuery.ScrollTo, check it's demo for an example of each option. |
| 24 |
// @see http://demos.flesler.com/jquery/scrollTo/ |
| 25 |
// The defaults are public and can be overriden. |
| 26 |
$localScroll.defaults = { |
| 27 |
duration: 1000, // How long to animate. |
| 28 |
axis: 'y', // Which of top and left should be modified. |
| 29 |
event: 'click', // On which event to react. |
| 30 |
stop: true, // Avoid queuing animations |
| 31 |
target: window, // What to scroll (selector or element). The whole window by default. |
| 32 |
autoscroll: true // If true, applies the scrolling at initial page load. |
| 33 |
/* |
| 34 |
lock: false, // ignore events if already animating |
| 35 |
lazy: false, // if true, links can be added later, and will still work. |
| 36 |
filter: null, // filter some anchors out of the matched elements. |
| 37 |
hash: false, // if true, the hash of the selected link, will appear on the address bar. |
| 38 |
onBefore: null // called before scrolling, "this" contains the settings and gets 3 arguments |
| 39 |
*/ |
| 40 |
}; |
| 41 |
|
| 42 |
$.fn.localScroll = function(settings) { |
| 43 |
settings = $.extend({}, $localScroll.defaults, settings); |
| 44 |
|
| 45 |
if (settings.autoscroll && settings.hash && location.hash) { |
| 46 |
if (settings.target) window.scrollTo(0, 0); |
| 47 |
scroll(0, location, settings); |
| 48 |
} |
| 49 |
|
| 50 |
return settings.lazy ? |
| 51 |
// use event delegation, more links can be added later. |
| 52 |
this.on(settings.event, 'a,area', function(e) { |
| 53 |
if (filter.call(this)) { |
| 54 |
scroll(e, this, settings); |
| 55 |
} |
| 56 |
}) : |
| 57 |
// bind concretely, to each matching link |
| 58 |
this.find('a,area') |
| 59 |
.filter(filter).bind(settings.event, function(e) { |
| 60 |
scroll(e, this, settings); |
| 61 |
}).end() |
| 62 |
.end(); |
| 63 |
|
| 64 |
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. |
| 65 |
return !!this.href && !!this.hash && this.href.replace(this.hash,'') === URI && (!settings.filter || $(this).is(settings.filter)); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
// Not needed anymore, kept for backwards compatibility |
| 70 |
$localScroll.hash = function() {}; |
| 71 |
|
| 72 |
function scroll(e, link, settings) { |
| 73 |
var id = link.hash.slice(1), |
| 74 |
elem = document.getElementById(id) || document.getElementsByName(id)[0]; |
| 75 |
|
| 76 |
if (!elem) return; |
| 77 |
|
| 78 |
if (e) e.preventDefault(); |
| 79 |
|
| 80 |
var $target = $(settings.target); |
| 81 |
|
| 82 |
if (settings.lock && $target.is(':animated') || |
| 83 |
settings.onBefore && settings.onBefore(e, elem, $target) === false) |
| 84 |
return; |
| 85 |
|
| 86 |
if (settings.stop) { |
| 87 |
$target.stop(true); // remove all its animations |
| 88 |
} |
| 89 |
|
| 90 |
if (settings.hash) { |
| 91 |
var attr = elem.id === id ? 'id' : 'name', |
| 92 |
$a = $('<a> </a>').attr(attr, id).css({ |
| 93 |
position:'absolute', |
| 94 |
top: $(window).scrollTop(), |
| 95 |
left: $(window).scrollLeft() |
| 96 |
}); |
| 97 |
|
| 98 |
elem[attr] = ''; |
| 99 |
$('body').prepend($a); |
| 100 |
location.hash = link.hash; |
| 101 |
$a.remove(); |
| 102 |
elem[attr] = id; |
| 103 |
} |
| 104 |
|
| 105 |
$target |
| 106 |
.scrollTo(elem, settings) // do scroll |
| 107 |
.trigger('notify.serialScroll',[elem]); // notify serialScroll about this change |
| 108 |
} |
| 109 |
|
| 110 |
// AMD requirement |
| 111 |
return $localScroll; |
| 112 |
|
| 113 |
})); |