-
Notifications
You must be signed in to change notification settings - Fork 952
/
Copy pathjquery.localScroll.min.js
70 lines (68 loc) · 2.15 KB
/
jquery.localScroll.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Copyright (c) 2007-2014 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com
* Licensed under MIT
* @author Ariel Flesler
* @version 1.3.5
*/
;
(function(a) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], a)
} else {
a(jQuery)
}
}(function($) {
var g = location.href.replace(/#.*/, '');
var h = $.localScroll = function(a) {
$('body').localScroll(a)
};
h.defaults = {
duration: 1000,
axis: 'y',
event: 'click',
stop: true,
target: window
};
$.fn.localScroll = function(a) {
a = $.extend({}, h.defaults, a);
if (a.hash && location.hash) {
if (a.target) window.scrollTo(0, 0);
scroll(0, location, a)
}
return a.lazy ? this.on(a.event, 'a,area', function(e) {
if (filter.call(this)) {
scroll(e, this, a)
}
}) : this.find('a,area').filter(filter).bind(a.event, function(e) {
scroll(e, this, a)
}).end().end();
function filter() {
return !!this.href && !!this.hash && this.href.replace(this.hash, '') == g && (!a.filter || $(this).is(a.filter))
}
};
h.hash = function() {};
function scroll(e, a, b) {
var c = a.hash.slice(1),
elem = document.getElementById(c) || document.getElementsByName(c)[0];
if (!elem) return;
if (e) e.preventDefault();
var d = $(b.target);
if (b.lock && d.is(':animated') || b.onBefore && b.onBefore(e, elem, d) === false) return;
if (b.stop) d._scrollable().stop(true);
if (b.hash) {
var f = elem.id === c ? 'id' : 'name',
$a = $('<a> </a>').attr(f, c).css({
position: 'absolute',
top: $(window).scrollTop(),
left: $(window).scrollLeft()
});
elem[f] = '';
$('body').prepend($a);
location.hash = a.hash;
$a.remove();
elem[f] = c
}
d.scrollTo(elem, b).trigger('notify.serialScroll', [elem])
};
return h
}));