-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS-SmoothScrollToTopFAB.js
More file actions
86 lines (74 loc) · 2.15 KB
/
JS-SmoothScrollToTopFAB.js
File metadata and controls
86 lines (74 loc) · 2.15 KB
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
// Add this : <script src="./JS-SmoothScrollToTopFAB.js"></script>
function SFAB() { window.SFAB = undefined
var B = document.createElement("div");
B.className = "FABdo"; B.id = "FABup";
function isScroll() {clearTimeout(isScroll.on); isScroll.on=setTimeout(function(){isScroll.on=0},10)}
function home() {window.scrollTo({top:0, behavior:(isScroll.on?"auto":"smooth")})}
B.addEventListener("click", home, {passive: true});
function atTop(y) {return +(window.pageYOffset < (y||3))}
function sticky() {isScroll(); var IO = +!atTop(100); if (IO!=B.getAttribute("IO")) B.setAttribute("IO", IO)}
window.addEventListener("scroll", sticky, {passive: true})
sticky();
const css = document.createElement("style");
css.textContent = `
.FABdo {
all: initial;
--FABsize: calc(8vh + 4px);
z-index: 10001;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
user-select: none;
position: fixed;
border-radius: 100%;
padding: calc(var(--FABsize) / 3);
width: var(--FABsize);
height: var(--FABsize);
background: hsl(0,0%,33%);
text-align: center;
border: calc(var(--FABsize) / 2) solid transparent;
background-clip: padding-box;
opacity:.66;
transition: transform .4s 0s;
}
.FABdo:not([IO="0"]) {
transition: transform .2s 0s;
}
.FABdo:before {
content: "";
font-size: 0;
display: block;
height: var(--FABsize);
background: white;
top:-6.7%;
position: relative;
clip-path: polygon(50% 0%, 100% 86.6%, 0% 86.6%);
}
.FABdo[IO]:active:hover {
box-shadow: inset 0 0 0 0.5px hsl(0,0%,33%), inset 0 0 0 4px var(--highlight, highlight);
opacity: .99;
}
@media (hover: hover) and (pointer: fine) {
.FABdo:hover {
opacity: .825;
}
.FABdo[IO="0"]:hover {
transition: transform .4s 2.5s;
}
}
.FABdo[D="1"] {
opacity: 0.25 !important;
}
/**/
#FABup {
bottom: 0;
right: 0;
transform: translateY(0px);
}
#FABup[IO="0"]:not(:active) {
transform: translateY(100%);
}
`
document.head.appendChild(css);
document.body.appendChild(B)
}
if (document.readyState != "loading") SFAB(); else window.addEventListener("load", SFAB);