-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_button.html
55 lines (52 loc) · 1.39 KB
/
top_button.html
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
<!DOCTYPE html>
<html>
<head>
<style>
button{
background-image:url(image/top.png);
background-size: 100%;
width: 80px;
height: 80px;
}
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: #20B2AA;
color: white;
cursor: pointer;
padding: 10px;
border-radius: 20px;
}
#myBtn:hover {
background-size: 100%;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<script>
// When the user scrolls down 50px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
}
else {
document.getElementById("myBtn").style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
</script>
</body>
</html>