Skip to content

Commit 97e1b47

Browse files
committed
siplified backToTop
1 parent 668fb8c commit 97e1b47

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function backToTop() {
2+
const button: HTMLDivElement = document.createElement( 'div' );
3+
button.setAttribute( 'id', 'backtotop' );
4+
const scrollToTop = () => {
5+
window.scrollTo( 0, 0 );
6+
};
7+
button.onclick = scrollToTop;
8+
document.body.appendChild( button );
9+
window.addEventListener( 'scroll', () => {
10+
if (
11+
window.scrollY >=
12+
( document.body.scrollHeight - window.innerHeight ) / 2
13+
) {
14+
document.body.classList.remove( 'above-the-fold' );
15+
} else {
16+
document.body.classList.add( 'above-the-fold' );
17+
}
18+
} );
19+
}

src/scripts/scripts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { modulrMasonryController } from './modules/masonry';
66
import { modulrSelectController } from './modules/select';
77
import { modulrGrid } from './modules/grid';
88
import { modulrScrollTo } from './modules/scroll/scroll';
9+
import { backToTop } from './modules/backToTop/backToTop';
910

1011
window.addEventListener( 'DOMContentLoaded', async () => {
1112
/* enable scroll animations */
@@ -18,6 +19,8 @@ window.addEventListener( 'DOMContentLoaded', async () => {
1819
modulrGrid();
1920
/* enable oxone like animation for grid elements */
2021
modulrScrollTo();
22+
/* create a back-to-top button */
23+
backToTop();
2124
} );
2225

2326
window.addEventListener( 'load', async () => {

src/styles/patterns/_back-to-top.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#backtotop {
2+
box-sizing: border-box;
3+
position: fixed;
4+
opacity: 0;
5+
visibility: hidden;
6+
bottom: 20px;
7+
right: 20px;
8+
width : 50px;
9+
height : 50px;
10+
background-color: #ffffff;
11+
border: 2px solid #666;
12+
border-radius: 50%;
13+
cursor: pointer;
14+
transition: opacity 350ms, visibility 350ms;
15+
}
16+
17+
body:not(.above-the-fold) #backtotop {
18+
visibility: visible;
19+
opacity: 1;
20+
}

src/styles/patterns/patterns.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
@import "_grid.scss";
55
@import "_headline.scss";
66
@import "_text-efx.scss";
7+
@import "_back-to-top.scss";

0 commit comments

Comments
 (0)