generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add return-to-top block * add automatic injection of return-to-top button
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#return-to-top { | ||
display: block; | ||
position: fixed; | ||
top: 50%; | ||
right: 10px; | ||
z-index: 499; | ||
opacity: .5; | ||
transition: opacity .2s ease-in-out,visibility .2s linear; | ||
cursor: pointer; | ||
} | ||
|
||
#return-to-top.hidden { | ||
opacity: 0; | ||
} | ||
|
||
#return-to-top > .icon-container { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
background-color: var(--tan); | ||
border-radius: 50%; | ||
height: 50px; | ||
width: 50px; | ||
|
||
--icon-size: 36px; | ||
} | ||
|
||
#return-to-top > .icon-container > .icon { | ||
margin-top: 2px; | ||
filter: var(--clr-filter-white); | ||
} | ||
|
||
@media print, screen and (max-width: 720px) { | ||
#return-to-top { | ||
height: 50px; | ||
width: 50px; | ||
margin-top: -25px; | ||
} | ||
|
||
#return-to-top > picture { | ||
display: none; | ||
} | ||
} | ||
|
||
@media print, screen and (min-width: 720px) { | ||
#return-to-top { | ||
height: 170px; | ||
width: 50px; | ||
margin-top: -75px; | ||
} | ||
|
||
#return-to-top:hover { | ||
opacity: 1; | ||
} | ||
|
||
#return-to-top > .icon-container { | ||
display: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
buildBlock, createOptimizedPicture, decorateBlock, decorateIcons, loadBlock, | ||
} from '../../scripts/aem.js'; | ||
|
||
function onScroll() { | ||
const link = document.getElementById('return-to-top'); | ||
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { | ||
link.classList.remove('hidden'); | ||
} else { | ||
link.classList.add('hidden'); | ||
} | ||
} | ||
|
||
function scrollTop() { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
} | ||
|
||
export default function decorate(block) { | ||
const link = document.createElement('a'); | ||
link.id = 'return-to-top'; | ||
link.classList.add('hidden'); | ||
link.onclick = scrollTop; | ||
block.appendChild(link); | ||
|
||
// Desktop | ||
const picture = createOptimizedPicture('/cigaradvisor/blocks/return-to-top/return-to-top.webp'); | ||
link.appendChild(picture); | ||
|
||
// Mobile | ||
const iconContainer = document.createElement('div'); | ||
iconContainer.classList.add('icon-container'); | ||
const icon = document.createElement('span'); | ||
icon.classList.add('icon', 'icon-angle-up'); | ||
iconContainer.appendChild(icon); | ||
decorateIcons(iconContainer); | ||
link.appendChild(iconContainer); | ||
|
||
window.onscroll = onScroll; | ||
} | ||
|
||
export async function loadReturnToTop(main) { | ||
const container = document.createElement('aside'); | ||
main.insertAdjacentElement('afterend', container); | ||
const block = buildBlock('return-to-top', ''); | ||
container.append(block); | ||
decorateBlock(block); | ||
return loadBlock(block); | ||
} |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters