Skip to content

Commit

Permalink
Merge pull request #26 from erikyo/backToTopModule
Browse files Browse the repository at this point in the history
added backToTop module
  • Loading branch information
gardenboi authored Nov 8, 2023
2 parents 668fb8c + e07f811 commit 0c43229
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/scripts/modules/backToTop/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function backToTop() {
const button: HTMLDivElement = document.createElement( 'div' );
button.setAttribute( 'id', 'backtotop' );
const scrollToTop = () => {
window.scrollTo( {
top: 0,
behavior: 'smooth',
} );
};
button.onclick = scrollToTop;
document.body.appendChild( button );
}
6 changes: 6 additions & 0 deletions src/scripts/modules/scrollControl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ function scrollCallback() {
}
}

if ( Ypos >= window.innerHeight ) {
document.body.classList.remove( 'above-the-fold' );
} else {
document.body.classList.add( 'above-the-fold' );
}

if ( Ypos < 5 ) {
document.body.classList.add( 'top' );
} else {
Expand Down
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';

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 0c43229

Please sign in to comment.