Skip to content

Commit a31a24b

Browse files
committed
feat: Added scroll-to-top feature
1 parent b7219bf commit a31a24b

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

index.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@
5656
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
5757
<link rel="stylesheet" href="/materialjs/material.min.css" />
5858
<link rel="stylesheet" href="style.css" />
59+
<style type="text/css">
60+
html {
61+
scroll-behavior: smooth;
62+
}
63+
64+
#myBtn {
65+
display: none;
66+
position: fixed;
67+
bottom: 20px;
68+
right: 30px;
69+
z-index: 99;
70+
font-size: 25px;
71+
border: none;
72+
outline: none;
73+
background-color: #87CEFA;
74+
color: white;
75+
font-color: blue;
76+
cursor: pointer;
77+
padding: 14px;
78+
border-radius: 30%;
79+
}
80+
81+
.fa-angle-double-up {
82+
83+
font-weight: bolder;
84+
}
85+
86+
#myBtn:hover {
87+
background-color: #D3D3D3;
88+
}
89+
</style>
5990
<script defer src="/materialjs/material.min.js"></script>
6091
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
6192

@@ -132,6 +163,8 @@
132163
</div>
133164
</div>
134165

166+
<button onclick="topFunction()" id="myBtn" title="Go to top"><i class="fa fa-angle-double-up p-1 font-weight-bold" aria-hidden="true" style="color: black;"></i></button>
167+
135168
<div class="nwoc-content">
136169
<a id="about"></a>
137170
<h3>About</h3>
@@ -518,6 +551,27 @@ <h4>Tell your friends about NWoC</h4>
518551

519552
}());
520553
</script>
554+
<script>
555+
//Get the button
556+
var mybutton = document.getElementById("myBtn");
557+
558+
// When the user scrolls down 20px from the top of the document, show the button
559+
window.onscroll = function() {scrollFunction()};
560+
561+
function scrollFunction() {
562+
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
563+
mybutton.style.display = "block";
564+
} else {
565+
mybutton.style.display = "none";
566+
}
567+
}
568+
569+
// When the user clicks on the button, scroll to the top of the document
570+
function topFunction() {
571+
document.body.scrollTop = 0;
572+
document.documentElement.scrollTop = 0;
573+
}
574+
</script>
521575

522576
<script src="https://cdnjs.cloudflare.com/ajax/libs/tilt.js/1.2.1/tilt.jquery.min.js"></script>
523577
<script>

style.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@ body {
99
background-color: #cbeef8;
1010
}
1111

12+
html {
13+
scroll-behavior: smooth;
14+
}
15+
16+
#myBtn {
17+
display: none;
18+
position: fixed;
19+
bottom: 20px;
20+
right: 30px;
21+
z-index: 99;
22+
font-size: 25px;
23+
border: none;
24+
outline: none;
25+
background-color: #87CEFA;
26+
color: white;
27+
font-color: blue;
28+
cursor: pointer;
29+
padding: 14px;
30+
border-radius: 30%;
31+
}
32+
33+
.fa-angle-double-up {
34+
font-weight: bolder;
35+
}
36+
37+
#myBtn:hover {
38+
background-color: #D3D3D3;
39+
}
40+
1241
p {
1342
font-size: 16px;
1443
}

0 commit comments

Comments
 (0)