-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.smooth-scroll.min.js
184 lines (184 loc) · 5.49 KB
/
jquery.smooth-scroll.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
!(function (t) {
"function" == typeof define && define.amd
? define(["jquery"], t)
: t(
"object" == typeof module && module.exports ? require("jquery") : jQuery
);
})(function (m) {
function e(t) {
var e = [],
l = t.dir && "left" === t.dir ? "scrollLeft" : "scrollTop";
return (
this.each(function () {
var t = m(this);
if (this !== document && this !== window) {
if (
document.scrollingElement &&
(this === document.documentElement || this === document.body)
)
return e.push(document.scrollingElement), !1;
0 < t[l]()
? e.push(this)
: (t[l](1), 0 < t[l]() && e.push(this), t[l](0));
}
}),
e.length ||
this.each(function () {
this === document.documentElement &&
"smooth" === m(this).css("scrollBehavior") &&
(e = [this]),
e.length || "BODY" !== this.nodeName || (e = [this]);
}),
"first" === t.el && 1 < e.length && (e = [e[0]]),
e
);
}
var h = {},
o = /^([\-\+]=)(\d+)/;
m.fn.extend({
scrollable: function (t) {
t = e.call(this, { dir: t });
return this.pushStack(t);
},
firstScrollable: function (t) {
t = e.call(this, { el: "first", dir: t });
return this.pushStack(t);
},
smoothScroll: function (t, l) {
if ("options" === (t = t || {}))
return l
? this.each(function () {
var t = m(this),
e = m.extend(t.data("ssOpts") || {}, l);
t.data("ssOpts", e);
})
: this.first().data("ssOpts");
function e(t) {
function e(t) {
return t.replace(/(:|\.|\/)/g, "\\$1");
}
var l = m(this),
o = m.extend({}, d, l.data("ssOpts") || {}),
n = d.exclude,
s = o.excludeWithin,
r = 0,
i = 0,
c = !0,
a = {},
f = m.smoothScroll.filterPath(location.pathname),
h = m.smoothScroll.filterPath(this.pathname),
u = location.hostname === this.hostname || !this.hostname,
h = o.scrollTarget || h === f;
if (
((f = e(this.hash)) && !m(f).length && (c = !1),
o.scrollTarget || (u && h && f))
) {
for (; c && r < n.length; ) l.is(e(n[r++])) && (c = !1);
for (; c && i < s.length; ) l.closest(s[i++]).length && (c = !1);
} else c = !1;
c &&
(o.preventDefault && t.preventDefault(),
m.extend(a, o, { scrollTarget: o.scrollTarget || f, link: this }),
m.smoothScroll(a));
}
var d = m.extend({}, m.fn.smoothScroll.defaults, t);
return (
null !== t.delegateSelector
? this.off("click.smoothscroll", t.delegateSelector).on(
"click.smoothscroll",
t.delegateSelector,
e
)
: this.off("click.smoothscroll").on("click.smoothscroll", e),
this
);
},
});
function u(t) {
var e = { relative: "" },
l = "string" == typeof t && o.exec(t);
return (
"number" == typeof t
? (e.px = t)
: l && ((e.relative = l[1]), (e.px = parseFloat(l[2]) || 0)),
e
);
}
function d(t) {
var e = m(t.scrollTarget);
t.autoFocus &&
e.length &&
(e[0].focus(),
e.is(document.activeElement) || (e.prop({ tabIndex: -1 }), e[0].focus())),
t.afterScroll.call(t.link, t);
}
(m.smoothScroll = function (t, e) {
if ("options" === t && "object" == typeof e) return m.extend(h, e);
var l,
o,
n,
s = u(t),
r = 0,
i = "offset",
c = "scrollTop",
a = {},
f = {};
s.px
? (l = m.extend({ link: null }, m.fn.smoothScroll.defaults, h))
: ((l = m.extend({ link: null }, m.fn.smoothScroll.defaults, t || {}, h))
.scrollElement &&
((i = "position"),
"static" === l.scrollElement.css("position") &&
l.scrollElement.css("position", "relative")),
e && (s = u(e))),
(c = "left" === l.direction ? "scrollLeft" : c),
l.scrollElement
? ((o = l.scrollElement),
!s.px && /^(?:HTML|BODY)$/.test(o[0].nodeName) && (r = o[c]()))
: (o = m("html, body").firstScrollable(l.direction)),
l.beforeScroll.call(o, l),
(n = s.px
? s
: {
relative: "",
px:
(m(l.scrollTarget)[i]() && m(l.scrollTarget)[i]()[l.direction]) ||
0,
}),
(a[c] = n.relative + (n.px + r + l.offset)),
"auto" === (r = l.speed) &&
(r = Math.abs(a[c] - o[c]()) / l.autoCoefficient),
(f = {
duration: r,
easing: l.easing,
complete: function () {
d(l);
},
}),
l.step && (f.step = l.step),
o.length ? o.stop().animate(a, f) : d(l);
}),
(m.smoothScroll.version = "2.2.0"),
(m.smoothScroll.filterPath = function (t) {
return (t = t || "")
.replace(/^\//, "")
.replace(/(?:index|default).[a-zA-Z]{3,4}$/, "")
.replace(/\/$/, "");
}),
(m.fn.smoothScroll.defaults = {
exclude: [],
excludeWithin: [],
offset: 0,
direction: "top",
delegateSelector: null,
scrollElement: null,
scrollTarget: null,
autoFocus: !1,
beforeScroll: function () {},
afterScroll: function () {},
easing: "swing",
speed: 400,
autoCoefficient: 2,
preventDefault: !0,
});
});