Skip to content

Commit

Permalink
siplified backToTop
Browse files Browse the repository at this point in the history
  • Loading branch information
gardenboi committed Nov 8, 2023
1 parent 668fb8c commit 97e1b47
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/scripts/modules/backToTop/backToTop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function backToTop() {
const button: HTMLDivElement = document.createElement( 'div' );
button.setAttribute( 'id', 'backtotop' );
const scrollToTop = () => {
window.scrollTo( 0, 0 );
};
button.onclick = scrollToTop;
document.body.appendChild( button );
window.addEventListener( 'scroll', () => {
if (
window.scrollY >=
( document.body.scrollHeight - window.innerHeight ) / 2
) {
document.body.classList.remove( 'above-the-fold' );
} else {
document.body.classList.add( 'above-the-fold' );
}
} );
}
3 changes: 3 additions & 0 deletions src/scripts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { modulrMasonryController } from './modules/masonry';
import { modulrSelectController } from './modules/select';
import { modulrGrid } from './modules/grid';
import { modulrScrollTo } from './modules/scroll/scroll';
import { backToTop } from './modules/backToTop/backToTop';

window.addEventListener( 'DOMContentLoaded', async () => {
/* enable scroll animations */
Expand All @@ -18,6 +19,8 @@ window.addEventListener( 'DOMContentLoaded', async () => {
modulrGrid();
/* enable oxone like animation for grid elements */
modulrScrollTo();
/* create a back-to-top button */
backToTop();
} );

window.addEventListener( 'load', async () => {
Expand Down
20 changes: 20 additions & 0 deletions src/styles/patterns/_back-to-top.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#backtotop {
box-sizing: border-box;
position: fixed;
opacity: 0;
visibility: hidden;
bottom: 20px;
right: 20px;
width : 50px;
height : 50px;
background-color: #ffffff;
border: 2px solid #666;
border-radius: 50%;
cursor: pointer;
transition: opacity 350ms, visibility 350ms;
}

body:not(.above-the-fold) #backtotop {
visibility: visible;
opacity: 1;
}
1 change: 1 addition & 0 deletions src/styles/patterns/patterns.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
@import "_grid.scss";
@import "_headline.scss";
@import "_text-efx.scss";
@import "_back-to-top.scss";

0 comments on commit 97e1b47

Please sign in to comment.