Skip to content

Commit

Permalink
added a scroll to top button (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetThakur authored Oct 16, 2024
1 parent 88babad commit fe48fc3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
20 changes: 20 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
scroll-behavior: smooth;
}

/*scroll to top button*/
#scrollToTopBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
background-color: #555;
color: white;
cursor: pointer;
padding: 10px;
border-radius: 5px;
font-size: 12px;
}

#scrollToTopBtn:hover {
background-color: #333;
}
/* Menu Bar */

.menu-container {
Expand Down
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
</head>

<body>

<button id="scrollToTopBtn" title="Go to top">
<i class="fa fa-arrow-up"></i>
</button>


<div class="banner section">
<nav>
<div class="logo">
Expand Down
25 changes: 21 additions & 4 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@
document.getElementById("closeMenu").addEventListener("click", function () {
document.getElementById("menuContainer").style.display = "none";
});
document.querySelectorAll(".close").addEventListener("click", function () {
document.getElementById("menuContainer").style.display = "none";
document.querySelectorAll(".close").forEach((element) => {
element.addEventListener("click", function () {
document.getElementById("menuContainer").style.display = "none";
});
});

function handleClose() {
document.getElementById("menuContainer").style.display = "none";
}

let mybutton = document.getElementById("scrollToTopBtn");
window.onscroll = function() {scrollFunction()};

function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}

mybutton.onclick = function() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}

// functionality of the scroll effect for the navbar
const nav = document.querySelector('nav');
const whereWePlantSection = document.querySelector('.locations');

Expand All @@ -59,4 +75,5 @@
}
}

window.addEventListener('scroll', handleScroll);
window.addEventListener('scroll', handleScroll);

0 comments on commit fe48fc3

Please sign in to comment.