Skip to content

Commit

Permalink
enhance go-to-top btn
Browse files Browse the repository at this point in the history
  • Loading branch information
KwFung7 committed Nov 22, 2023
1 parent 27b12fd commit 867bbf2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"enzyme-adapter-react-16": "^1.15.6",
"gh-pages": "^2.1.1",
"jest-canvas-mock": "^2.3.1",
"lodash": "^4.17.21",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-easy-emoji": "^1.8.1",
Expand Down
31 changes: 21 additions & 10 deletions src/containers/topbutton/Top.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import React, { useEffect } from "react";
import _ from "lodash";
import "./Top.scss";

export default function Top() {
Expand All @@ -12,18 +13,28 @@ export default function Top() {
document.body.scrollTop > 20 ||
document.documentElement.scrollTop > 20
) {
document.getElementById("topButton").style.visibility = "visible";
document.getElementById("topButton").style.opacity = 1;
} else {
document.getElementById("topButton").style.visibility = "hidden";
document.getElementById("topButton").style.opacity = 0;
}
}
window.onscroll = function () {
scrollFunction();
};
window.onload = function () {
scrollFunction();
}; //To make sure that this button is not visible at starting.
// When the user clicks on the button, scroll to the top of the document

useEffect(() => {
window.onscroll = function () {
if (document.getElementById("topButton").style.opacity === "1") {
document.getElementById("topButton").style.opacity = 0;
}
};
window.addEventListener('scrollend', (event) => {
const debounced = _.debounce(scrollFunction, 500);
debounced();
});
window.onload = function () {
scrollFunction();
}; //To make sure that this button is not visible at starting.
// When the user clicks on the button, scroll to the top of the document
}, []);

return (
<button onClick={TopEvent} id="topButton" title="Go to top">
<i className="fas fa-hand-point-up" aria-hidden="true"></i>
Expand Down
3 changes: 2 additions & 1 deletion src/containers/topbutton/Top.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@import "../../_globalColor";

#topButton {
visibility: hidden;
transition: opacity .5s;
opacity: 0;
position: fixed;
bottom: 20px;
right: 30px;
Expand Down

0 comments on commit 867bbf2

Please sign in to comment.