Dev Tips: JavaScript: Native Smooth Scrolling ⚡️

Dev Tips

 

JavaScript: Native Smooth Scrolling


A quick note from me


I'll be doing a Smashing Workshop on Testing + Browser Automation, please check it out, or their other workshops.  They've been doing some amazing work recently!


Introduction

You can smooth scroll to an element with zero dependencies. You can achieve this programmatically in JavaScript, and you can also enable smooth scrolling from CSS. Browser support is good!
 

Using JavaScript

To do this programmatically through JavaScript, use this code:

el.scrollIntoView({
    behavior: 'smooth'
})

There's actually two interesting things about that code above, yes you can smooth scroll, but secondly, turns out there's a built in method for scrolling an element into the viewport.
 

Using plain CSS

Depending on your use case, you might be able to avoid JavaScript entirely, and achieve this through CSS only:

html {
    scroll-behavior: smooth;
}

This does not affect manual scrolling performed by the user, but it will impact for example, an internal link on a page such a back to top hyperlink:

<!--
    This smooth scrolls to the top
    thanks to scroll-behavior: smooth
-->
<a href="#top">Back to top</a>

Browser support

Browser support is pretty good, but there are some limitations with Safari:


Users who prefer reduced motion

Consider using the CSS media feature: prefers-reduced-motion to toggle the 'smooth' option since not everyone wants heavy animations/transitions on a website.

@media (prefers-reduced-motion) {
    .animation {
        animation-name: none;
    }
}

As a reminder, prefers reduced motion does not mean no motion, so consider that point when altering your user experience for such users.

Here's the documentation for prefers-reduced-motion.

You can Emulate this CSS Media Feature from the Chrome DevTools Command Palette to make sure your media queries are working as expected:

  1. Use the shortcut Cmd + Shift + P
  2. Search for: reduced-motion
  3. Hit enter
Using DevTools to emulate prefers-reduced-motion


Conclusion

Hope you've learnt something new. JavaScript has lots of interesting features which aren't always obvious. If you want to look further, note that you can Automatically remove an event listener after it has executed.


See this tip online: umaar.com/dev-tips/235-smooth-scroll-into-view
 
Email Marketing Powered by Mailchimp

Copyright © 2020 Umar, All rights reserved.
You are receiving this email because you signed up for Dev Tips.

unsubscribe from this list    update subscription preferences 

Older messages

Dev Tips: DevTools: Customise Your User Agent

Thursday, July 30, 2020

Dev Tips DevTools: Customise Your User Agent Introduction You can set a custom user agent in the Network Overrides Pane. If for example, a website changes its behaviour based on the user agent (a

Dev Tips: DevTools: Tips for changing values in HTML & CSS

Thursday, July 23, 2020

Dev Tips DevTools: Tips for changing values in HTML & CSS Introduction You can easily change numeric values in DevTools. This is a handy feature for when you want to quickly prototype CSS property

Dev Tips: DevTools:️ How deactivated CSS styles works

Thursday, July 16, 2020

Dev Tips DevTools: How copying and pasting deactivated CSS styles works DevTools has some small, yet handy features when it comes to dealing with styles. If you copy some deactivated style rules, they

DevTools: Improve accessibility by emulating vision deficiencies

Friday, July 10, 2020

Dev Tips Chrome DevTools: Improve page accessibility by emulating vision deficiencies Web developer tooling to improve accessibility has improved rapidly over the years. Emulating vision deficiencies

Dev Tips: JavaScript: ⚡️ Improve the performance of your event listeners

Wednesday, July 1, 2020

Dev Tips I've been blogging more on my website, go and check it out! JavaScript: Improve the performance of your event listeners In your JavaScript code, you can automatically remove an event

You Might Also Like

Software Testing Weekly - Issue 217

Monday, April 29, 2024

How do you deal with conflicts in QA? ⚔️ View on the Web Archives ISSUE 217 April 29th 2024 COMMENT Welcome to the 217th issue! How do you deal with conflicts in QA? Ideally, you'd like to know how

📧 Did you watch the free MMA chapters? (1+ hours of content)

Monday, April 29, 2024

Did you watch the free MMA chapters? Hey there! 👋 I wish you a fantastic start to the week. Last week, I launched Modular Monolith Architecture. More than 300+ students are already deep into the MMA

WP Weekly 191 - Essentials - Duplicate in Core, White Label Kadence, Studio for Mac

Monday, April 29, 2024

Read on Website WP Weekly 191 / Essentials It seems many essential features are being covered in-house, be it the upcoming duplicate posts/pages feature in the WordPress core or the launch of Studio

SRE Weekly Issue #422

Monday, April 29, 2024

View on sreweekly.com A message from our sponsor, FireHydrant: FireHydrant is now AI-powered for faster, smarter incidents! Power up your incidents with auto-generated real-time summaries,

Quick question

Sunday, April 28, 2024

I want to learn how I can better serve you ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Kotlin Weekly #404 (NOT FOUND)

Sunday, April 28, 2024

ISSUE #404 28st of April 2024 Announcements Kotlin Multiplatform State of the Art Survey 2024 Help to shape and understand the Kotlin Multiplatform Ecosystem! It takes 4 minutes to fill this survey.

📲 Why Is It Called Bluetooth? — Check Out This AI Text to Song Generator

Sunday, April 28, 2024

Also: What to Know About Emulating Games on iPhone, and More! How-To Geek Logo April 28, 2024 📩 Get expert reviews, the hottest deals, how-to's, breaking news, and more delivered directly to your

Daily Coding Problem: Problem #1425 [Easy]

Sunday, April 28, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Microsoft. Suppose an arithmetic expression is given as a binary tree. Each leaf is an

PD#571 Software Design Principles I Learned the Hard Way

Sunday, April 28, 2024

If there's two sources of truth, one is probably wrong. And yes, please repeat yourself. ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

When Procrastination is Productive & Ghost integrating with ActivityPub

Sunday, April 28, 2024

Automattic, Texts, and Beeper join forces to build world's best inbox, Reflect launches its iOS app, how to start small rituals, and a lot more in this week's issue of Creativerly. Creativerly