Skip to content

Commit 087cbe0

Browse files
authored
Merge pull request FromEndWorld#11 from sulibo/back-to-top-button
add back-to-top button
2 parents 6d688a5 + c622267 commit 087cbe0

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

_layouts/default.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
{% include footer.html %}
3131
</footer>
3232

33+
<!--back-top-button-->
34+
<script src="{{ site.url }}/{{ site.baseurl }}/assets/js/vanilla-back-to-top.min.js"></script>
35+
<script>addBackToTop()</script>
3336

3437
</html>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"use strict";
2+
3+
function addBackToTop() {
4+
var o, t, e, n, i = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
5+
r = i.backgroundColor,
6+
d = void 0 === r ? "#000" : r,
7+
a = i.cornerOffset,
8+
c = void 0 === a ? 20 : a,
9+
s = i.diameter,
10+
l = void 0 === s ? 45 : s,
11+
u = i.ease,
12+
p = void 0 === u ? function(o) {
13+
return .5 * (1 - Math.cos(Math.PI * o))
14+
} : u,
15+
m = i.id,
16+
h = void 0 === m ? "back-to-top" : m,
17+
b = i.innerHTML,
18+
v = void 0 === b ? '<svg viewBox="0 0 24 24"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"></path></svg>' : b,
19+
f = i.onClickScrollTo,
20+
x = void 0 === f ? 0 : f,
21+
w = i.scrollContainer,
22+
g = void 0 === w ? document.body : w,
23+
k = i.scrollDuration,
24+
y = void 0 === k ? 100 : k,
25+
T = i.showWhenScrollTopIs,
26+
M = void 0 === T ? 1 : T,
27+
z = i.size,
28+
E = void 0 === z ? l : z,
29+
C = i.textColor,
30+
L = void 0 === C ? "#fff" : C,
31+
N = i.zIndex,
32+
I = void 0 === N ? 1 : N,
33+
A = g === document.body,
34+
B = A && document.documentElement;
35+
o = Math.round(.43 * E), t = Math.round(.29 * E), e = "#" + h + "{background:" + d + ";-webkit-border-radius:50%;-moz-border-radius:50%;border-radius:50%;bottom:" + c + "px;-webkit-box-shadow:0 2px 5px 0 rgba(0,0,0,.26);-moz-box-shadow:0 2px 5px 0 rgba(0,0,0,.26);box-shadow:0 2px 5px 0 rgba(0,0,0,.26);color:" + L + ";cursor:pointer;display:block;height:" + E + "px;opacity:1;outline:0;position:fixed;right:" + c + "px;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-transition:bottom .2s,opacity .2s;-o-transition:bottom .2s,opacity .2s;-moz-transition:bottom .2s,opacity .2s;transition:bottom .2s,opacity .2s;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;width:" + E + "px;z-index:" + I + "}#" + h + " svg{display:block;fill:currentColor;height:" + o + "px;margin:" + t + "px auto 0;width:" + o + "px}#" + h + ".hidden{bottom:-" + E + "px;opacity:0}", (n = document.createElement("style")).appendChild(document.createTextNode(e)), document.head.insertAdjacentElement("afterbegin", n);
36+
var D = function() {
37+
var o = document.createElement("div");
38+
return o.id = h, o.className = "hidden", o.innerHTML = v, o.addEventListener("click", function(o) {
39+
o.preventDefault(),
40+
function() {
41+
var o = "function" == typeof x ? x() : x,
42+
t = window,
43+
e = t.performance,
44+
n = t.requestAnimationFrame;
45+
if (y <= 0 || void 0 === e || void 0 === n) return q(o);
46+
var i = e.now(),
47+
r = j(),
48+
d = r - o;
49+
n(function o(t) {
50+
var e = Math.min((t - i) / y, 1);
51+
q(r - Math.round(p(e) * d)), e < 1 && n(o)
52+
})
53+
}()
54+
}), document.body.appendChild(o), o
55+
}(),
56+
H = !0;
57+
58+
function S() {
59+
j() >= M ? function() {
60+
if (!H) return;
61+
D.className = "", H = !1
62+
}() : function() {
63+
if (H) return;
64+
D.className = "hidden", H = !0
65+
}()
66+
}
67+
68+
function j() {
69+
return g.scrollTop || B && document.documentElement.scrollTop || 0
70+
}
71+
72+
function q(o) {
73+
g.scrollTop = o, B && (document.documentElement.scrollTop = o)
74+
}(A ? window : g).addEventListener("scroll", S), S()
75+
}

0 commit comments

Comments
 (0)