DEV Community

Cover image for Using Smart CSS to Time Your Wonderful Newsletter Popup

Using Smart CSS to Time Your Wonderful Newsletter Popup

Rik Schennink on January 29, 2019

In the previous article on Smart CSS we used the user's scroll position to trigger a floating navigation header. This article was originally publ...
Collapse
 
link2twenty profile image
Andrew Bone

You could use the newish, Intersection Observer API. You'd need to make an element to mark the halfway point. But you could make something like this:

Collapse
 
rikschennink profile image
Rik Schennink

Thanks, Andrew!

While IntersectionObserver is a super nice API, it's not really in line with the purpose behind the article, which is doing this by feeding info to CSS using JavaScript and not controlling the hiding and showing of elements with JavaScript. Thereby completely separating CSS from JavaScript.

Collapse
 
bhaibel profile image
Betsy Haibel

The two approaches seem compatible to me! If the intersection observer updated the body class with a data-last-landmark attribute, instead of a raw %, you could still reference off of that with CSS.

Thread Thread
 
rikschennink profile image
Rik Schennink

Interesting approach! That would certainly work I think.

Collapse
 
link2twenty profile image
Andrew Bone

I see, my apologies 🙂

Generally, I try to avoid listening on scroll because it can cause jank when scrolling (especially on mobile).

That being said, "premature optimization is the root of all evil" 😅

xkcd

Thread Thread
 
rikschennink profile image
Rik Schennink

It’s something to surely take into account, that said, the previous article outlines some of the performance improvements applied in the code.

Collapse
 
jvanbruegge profile image
Jan van Brügge

Or you could just provide an RSS feed sigh
What happened to the good, old and reliable tech...