Saturday, August 6, 2022
HomeProgrammingHow I Added Scroll Snapping To My Twitter Timeline | CSS-Tips

How I Added Scroll Snapping To My Twitter Timeline | CSS-Tips


CSS Scroll Snap permits web sites to snap the net web page or another scroll container to a particular scroll place when the consumer performs a scrolling operation. This characteristic has been supported in all trendy browsers for over two years, however many web sites that would profit from it are nonetheless not utilizing it.

Scroll snapping might be most related to horizontal carousels (see Chris’s CSS-only method) and specific net pages divided into full-screen slides. However why cease there? I imagine that snapping can enhance the scrolling expertise on any net web page that lays out gadgets in a grid or feed.

For instance, most buying web sites present merchandise in a grid. Ideally, the consumer wish to leap between the grid rows with minimal effort. The consumer can press House to scroll the web page by roughly one display screen (viewport peak), however relying on the peak of the grid rows, the scroll place will finally get “out of sync” with the grid, and the consumer must re-adjust it manually.

If we add scroll snapping to this web page, the consumer can constantly scroll to the subsequent row with the House key (urgent Shift + House will scroll to the earlier row). It’s fairly easy.

I believe that scroll snapping could be a welcome addition to this web site. And it’s not even that difficult to implement. The CSS code that I used for this instance is comparatively easy:

html {
  scroll-snap-type: y proximity;
}

.product-item {
  scroll-snap-align: begin;
  scroll-margin-top: 75px; /* peak of net web page’s sticky header */
}

You don’t have to attend if a web site you go to frequently hasn’t but added scroll snapping and also you suppose it could enhance your scrolling expertise. You may add scroll snapping your self — with consumer kinds.

Including consumer kinds to web sites

Within the video above, you’ll be able to see that I chosen a consumer.css file in Safari’s superior preferences. This file is a consumer fashion sheet. It incorporates CSS kinds that I’ve written, saved in an area .css file, and added to Safari. These “consumer kinds” are then utilized to each net web page I open in Safari.

Chrome and Firefox don’t enable customers to pick out a consumer fashion sheet. Firefox supported an identical characteristic referred to as userContent.css previously, however that characteristic was deprecated and disabled by default in 2019. I like to recommend the Stylus browser extension for these two browsers (and different Chromium-based browsers).

One important benefit of Stylus is that it lets you write consumer kinds for particular web sites and URLs. Safari’s consumer fashion sheet applies to all web sites, however this may be labored round, e.g., through the use of the brand new :has() pseudo-class to create selectors that solely match particular web sites.

The Stylus extension has been reviewed by each Chrome and Firefox groups and acquired a badge that denotes excessive requirements.

The CSS Cascading module defines a Consumer Origin for kinds the consumer provides. Safari’s consumer fashion sheet belongs to this origin, however the Stylus extension injects consumer kinds to the Writer Origin, the place the web site’s fashion sheets dwell. Particularly, Stylus inserts consumer kinds on to the web page through a <fashion> aspect on the finish of <html> which makes it the closing fashion sheet on the web page. Technically, this implies kinds added through Stylus are labeled as creator kinds since they’re not within the Consumer Origin, however I’ll proceed to name them consumer kinds as a result of the consumer provides them.

Nevertheless, it’s price preserving this distinction in thoughts as a result of it impacts the cascade. When selector specificity is equal, an actual consumer fashion is weaker than the web page’s personal fashion. This makes consumer kinds a superb match for consumer defaults. Below the identical circumstances, a mode added through Stylus is stronger than the web page‘s fashion, so Stylus can’t as simply be used to outline consumer defaults.

If we add !necessary to the combination, each actual consumer kinds and kinds added through Stylus are stronger than the web page’s kinds. So once you wish to impose your consumer kinds on a web site, it doesn’t matter should you use Safari’s “Model sheet” choice or the Stylus extension. Your !necessary kinds will win both manner.

Within the subsequent part, I’ll use a set of !necessary consumer kinds to implement scroll snapping on the timeline web page of Twitter’s web site. My purpose is to hurry up the method of studying my Twitter timeline by avoiding awkward scroll positions the place the topmost tweet is just partially on display screen.

Scroll snap for Twitter’s timeline

After some experimentation, I’ve settled on the next CSS code. These kinds work nicely in Firefox, however I’ve skilled some points in Chrome and Safari. I’ll describe these points in additional element later within the article, however for now, let’s concentrate on the habits in Firefox.

html {
  scroll-snap-type: y necessary !necessary;
}

/* tweets within the timeline are <article> components */
article {
  scroll-snap-align: begin !necessary;
}

/* un-stick the sticky header and make it “snappable” as nicely */
[aria-label="Home timeline"] > :first-child {
  place: static !necessary;
  scroll-snap-align: begin !necessary;
}

/* conceal the “new Tweets accessible” floating toast notification */
[aria-label="New Tweets are available."] {
  show: none !necessary;
}

It’s essential so as to add !necessary to every declaration as a result of all of the consumer kinds should win over the net web page’s personal kinds for our customized scroll snapping implementation to work appropriately. I want that as an alternative of repeatedly writing !necessary, I might simply put my consumer kinds in an “necessary layer,” however such a CSS characteristic doesn’t exist (but).

Watch the video under to see my scroll snap consumer kinds in motion. Discover how every press on the House key scrolls the subsequent set of tweets into view, and the primary tweet of every set is aligned to the highest fringe of the viewport. This enables me to learn my timeline extra shortly. After I want to return to the earlier set of tweets, I can press Shift + House.

What I like about such a scroll snapping is that it permits me to foretell how far the web page will scroll at any time when I press House. Every scroll distance equals the mixed heights of the seen tweets which can be solely on the display screen. In different phrases, the partially seen tweet on the backside of the display screen will transfer to the highest of the display screen, which is exactly what I need.

I do know prematurely that urgent House will scroll Dave’s tweet to the highest of the display screen.

To check out my scroll snap consumer kinds by yourself Twitter timeline, observe these steps:

  1. Set up the Stylus extension with Firefox Add-ons or the Chrome Net Retailer.
  2. Navigate to your Twitter timeline at https://twitter.com/dwelling.
  3. Click on the Stylus icon within the browser’s toolbar and click on “this URL” within the pop-up.
  4. Stylus will open a code editor in a brand new browser tab. Copy-paste my scroll snap consumer kinds into the editor and press the Save button within the sidebar on the left. The kinds might be utilized to your Twitter timeline instantly (no have to reload the web page).
  5. You may replace the kinds at any time. Click on the Stylus icon and the Pencil icon to open the editor once more.

Incapacity to override snapping

My implementation of scroll snapping for Twitter’s timeline has one main flaw. If a tweet is taller than the viewport, it’s unattainable to scroll the web page to disclose the underside a part of that tweet (e.g., if you wish to like or retweet that tweet) as a result of the browser forcefully snaps the web page to point out the highest of the tweet (or the highest of the next tweet).

The severity of this downside will depend on the consumer’s show. Viewing Twitter’s timeline on a big desktop monitor at a small web page zoom issue, it’s possible you’ll not encounter any tweets taller than the viewport.

I’ve requested the CSS Working Group if it could be doable so as to add a mechanism permitting the consumer to override the browser’s necessary scroll snapping. I ought to in all probability point out that this downside might, at the very least in concept, be resolved by switching from necessary to proximity snapping. I’ve examined proximity snapping in Chrome and Firefox, and I discovered it inconsistent and complicated. The browser would typically snap after I didn’t anticipate it to, and vice versa. Possibly Twitter’s code is interfering with the proximity algorithm, the browsers are nonetheless a bit buggy, or maybe I’m simply “scrolling it improper,” if that’s even doable. I don’t know.

However the principle cause why I went with necessary snapping is that I needed to keep away from conditions the place the topmost tweet is just partially on display screen after a scroll. The kind of fast-scrolling between units of tweets that I’ve proven within the video above is just doable with necessary snapping.

In case you, like me, choose necessary snapping, I can recommend the next two workarounds for the “tall tweet” downside:

  • You may open the tweet by itself web page and return to the timeline afterward.
  • In case you solely wish to click on the Like or Retweet buttons, you’ll be able to Shift-click the tweet to pick out it after which press L to love it, or T adopted by Enter to retweet it.

Points in Chrome and Safari

My scroll snap consumer kinds produce noticeably completely different scroll snapping behaviors in Chrome, Safari, and Firefox. These variations are partially because the actual implementation of the snapping mechanism is left as much as the browser:

The CSS Scroll Snap Module deliberately doesn’t specify nor mandate any exact animations or physics used to implement snap positions; that is left as much as the consumer agent.

The present model of Safari has a bug that forestalls scroll snapping from working appropriately on the Twitter timeline. I’ve reported this bug.

In Chrome, I’ve encountered the next issues:

  • The scrolling operations animate inconsistently. Generally the animation is gradual, generally it’s prompt, and generally it begins gradual however is then reduce quick. I discovered this irritating.
  • The scrolling operations animate too slowly basically. I carried out a check in Chrome and Firefox (20 House presses), and it took me 70% extra time to cowl the identical distance on my Twitter timeline in Chrome than in Firefox (18.5 seconds in Chrome vs. 11 seconds in Firefox).
  • After I scroll utilizing my laptop computer’s trackpad, the web page glints rather a lot. After I try to scroll quick by holding down the House key, the web page scrolls very slowly and oscillates. I believe that each points are brought on by the identical algorithm. Evidently Chrome re-snaps at a really excessive fee in these circumstances. I’ve reported this bug.

These browser bugs and variations between browsers could be a downside for web sites contemplating implementing scroll snapping. For instance, an online developer would possibly maintain again as a result of they don’t like how scroll snapping behaves in a single specific browser. Browsers can mitigate this downside by changing into extra interoperable. In truth, Scroll Snap is without doubt one of the areas of focus of the cross-browser Interop 2022 effort.

One other manner the state of affairs might be improved is by introducing new CSS properties that will make scroll snapping extra configurable. This might embrace the length of the snapping animation, the size of the proximity threshold for snapping, and a mechanism to override necessary snapping.

To snap or to not snap?

I’ve been utilizing my scroll snap consumer kinds on Twitter’s timeline for a few weeks, and I don’t wish to return. The power to shortly flip by my feed with solely the House key’s simply on one other stage.

Nevertheless, I contemplate this a complicated characteristic that in all probability isn’t for everybody. There’s a cause why I’ve enabled it solely on the timeline (/dwelling path) and nowhere else on Twitter’s web site. Snapping is a big change in how the web page scrolls, and it takes a while to get used to. It may well work nice for a particular use case, however it may well additionally get in the way in which and frustrate the consumer.

Web sites with feeds ought to subsequently contemplate providing scroll snapping solely as an elective characteristic, after cautious consideration and loads of testing in numerous browsers and with completely different enter strategies (mouse, keyboard, trackpad, contact display screen, and so on.).

Earlier than you go…

Lastly, I extremely advocate putting in and making an attempt out the Stylus browser extension. Net builders (or anybody who is aware of CSS) have the ability to fashion any web site of their browser. You may apply minor enhancements and fixes to your favourite web sites. I principally use it to cover web page components that I discover annoying, resembling sticky headers, video pop-ups, and vote counts.

However extra importantly, Stylus lets you shortly check new CSS options on any web site and report browser bugs, if essential. By doing this, you’ll be able to assist make the net platform a little bit higher.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments