Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance go-to-top button #665

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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",
"lottie-react": "^2.4.0",
"react": "^16.10.2",
"react-dom": "^16.10.2",
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() {
@@ -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>
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 0.5s;
opacity: 0;
position: fixed;
bottom: 20px;
right: 30px;
Loading