Skip to content

Commit

Permalink
use native css smooth scrolling
Browse files Browse the repository at this point in the history
use polyfill for browsers that don't support it natively (Safari), and
respect user preference for reduced motion (currently only supported in
Firefox)
  • Loading branch information
willnorris committed Oct 22, 2021
1 parent 708972e commit 554a287
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
17 changes: 15 additions & 2 deletions assets/css/year-in-review.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@
}


/* General */
html {
scroll-padding-top: 0rem;
// smooth scrolling unless user has requested not to
--scroll-behavior: smooth;
scroll-behavior: smooth;

@media (prefers-reduced-motion: reduce) {
--scroll-behavior: auto;
scroll-behavior: auto;
}
}

.YIR-wrapper {
color: white;
Expand Down Expand Up @@ -519,10 +529,13 @@ span.metric-name {
padding-top: var(--feather-grid-mega); /* FIX - DELETE if i make top nav stick to top */
}

html {
scroll-padding-top: 2.5rem;
}
nav {
background-color: var(--tw-color-blue-medium);
}

// Copied from main.css because mobile navigation is activated at a wider width on this page
#menu-toggle {
display: block;
Expand Down
28 changes: 11 additions & 17 deletions static/js/year-in-review.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,15 @@ document.querySelectorAll("#nav-menu a").forEach(e => {
e.addEventListener("click", () => {
document.getElementById('nav-menu').classList.remove('active')
})
})

// Smooth scrolling sections
// https://codepen.io/nailaahmad/pen/MyZXVE
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {

var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});

// polyfill smooth scrolling if needed
if (!('scrollBehavior' in document.documentElement.style)) {
Promise.all([
import('https://unpkg.com/smoothscroll-polyfill/dist/smoothscroll.min.js'),
import('https://unpkg.com/smoothscroll-anchor-polyfill')
])
.then(([smoothscrollPolyfill]) => {
smoothscrollPolyfill.polyfill();
});
}

0 comments on commit 554a287

Please sign in to comment.