In this blog, we will learn how to use Intersection Observer API to create scrolling animation. For more detail about Intersection Observer API, please refer on this blog
Firstly, as usual, we need to define a raw layout with the content we need to show to the user.
Raw layout:
index.html
<div id="list"> </div>
index.js
for (var i = 0; i < 20; i++) { //Create an empty item var item = document.createElement('div'); item.classList.add("item"); //Create avatar with dynamic url var avatar = document.createElement('img'); avatar.classList.add("avatar"); let avatarUrl = i % 2 ? "https://www.w3schools.com/w3images/avatar2.png" : "https://www.w3schools.com/w3images/avatar5.png"; avatar.setAttribute("src", avatarUrl); //Create user title var user = document.createElement('span'); user.classList.add("user"); user.innerText = `User ${i}` //Append avatart and user into item item.appendChild(avatar); item.appendChild(user); //Append item to the list document.getElementById("list").appendChild(item); }
style.css
#list { height: 100vh; overflow: auto; } .item { padding: 10px 10px; border-bottom: 1px solid #323232; } .avatar { vertical-align: middle; width: 50px; height: 50px; border-radius: 50%; margin-right: 1em; }
As a result, after finish creating the raw layout, we have something like that:
See the Pen Scroll Animation part 1 by hieu (@phuchieuct93abc) on CodePen.
Adding intersecting class
Secondly, we need to create an observer to detect whether an item is in view or not. Whenever an item is scrolled into and display in the view, we will add a class intersecting for that visible item:
var options = { root: document.querySelector('#list'), rootMargin: '0px', threshold: [0] } var callback = function(entries, observer) { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('intersecting'); } }); }; var observer = new IntersectionObserver(callback, options); for (let element of document.getElementsByClassName("item")) { observer.observe(element); }
CSS for intersecting
Finally, we need to defined a class “intersecting” with animation css like this:
.intersecting .user { animation: opacity 1s linear; } @keyframes scale { from { transform: scale(0.1); } to { transform: scale(1); } } @keyframes opacity { from { opacity: 0; } to { opacity: 1; } }
The final result we have will look like:
See the Pen Scroll Animation part 2 by hieu (@phuchieuct93abc) on CodePen.
With Intersection Observer API, we can create some attractive animations for items inside the scrollable list
Write more, thats all I have to say. Literally, it
ѕeems aѕ thouɡh yоu relied onn tһe video tօ make yⲟur pоint.
You obvіously know ԝhat yourre talking about, wһy waste your intelligence оn just postingg videos to your site when уou
couⅼɗ be giving uss something informative to read?
football field
YOU NEED QUALITY VISITORS for your: cloudaccess.host
YOU GET HIGH-QUALITY VISITORS
– visitors from search engines
– visitors from social media
– visitors from any country you want
– very low bounce rate & long visit duration
CLAIM YOUR 24 HOURS FREE TEST => http://bit.ly/GetTrafficSmart
This post provides clear idea designed for the new visitors of blogging, that really how to do blogging.
Link exchange is nothing else except it is just placing the other person’s blog link on your page at
appropriate place and other person will also do same in favor
of you.
Link exchange is nothing else but it is simply placing the other person’s webpage link on your page at suitable place and other person will also do similar for you.
Do you have a spam issue on this site; I also am a blogger,
and I was wanting to know your situation; we have created some nice practices and we are looking to exchange strategies with others, why
not shoot me an email if interested.
Have you ever thought about including a little bit more than just your articles?
I mean, what you say is important and all. But just imagine if you added some great pictures or video clips to give
your posts more, “pop”! Your content is excellent but with
pics and clips, this website could certainly be one of the most beneficial in its niche.
Excellent blog!
These are in fact wonderful ideas in regarding blogging.
You have touched some good points here. Any way keep up wrinting.
WOW just what I was searching for. Came here by searching for videos
I got this web page from my friend who told me on the topic of this web site and at the moment this time I am visiting this site and reading
very informative posts at this place.
It’s truly a great and helpful piece of info. I’m satisfied that you just shared this helpful information with us.
Please keep us informed like this. Thank you for sharing.
Pretty! This has been an extremely wonderful article.
Thanks for providing these details.
I was curious iff you evrr considered changing the layout of yor blog?
Its very well written; I love what youve got to say. But maybe
you could a little more in the way of content so
people could connect with it better. Youve got an awful lott
of text for only having 1 or two pictures.
Maybe you could space iit out better?
Fantastic beat ! I would like to apprentice whilst you amend your web site, how could i subscribe for a blog website?
The account helped me a acceptable deal. I were tiny bit familiar of
this your broadcast offered brilliant clear idea
Simply want to say your article is as astounding.
The clarity for your put up is simply excellent and i can assume you
are a professional on this subject. Fine along with your permission allow me to snatch your feed
to stay up to date with impending post. Thank you one million and please carry on the
rewarding work.
Hi! Do you know if they make any plugins to safeguard against hackers?
I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
I am regular visitor, how are you everybody? This paragraph posted at this website is actually nice.
This piece of writing is genuinely a fastidious one it helps new the web viewers, who are wishing in favor of blogging.
Nice blog here! Also your site loads up fast!
What host are you using? Can I get your affiliate link to your
host? I wish my site loaded up as fast as yours lol by kickgoal.net
My homepage Consuelo Lombard (maps.google.co.id)
Hey! This is my 1st comment here so I just wanted to
give a quick shout out and tell you I truly enjoy reading through your posts.
Can you recommend any other blogs/websites/forums that deal with the same topics?
Thank you so much! by kickgoal.net
Feel free to visit my blog … Ken Judd [http://images.google.gy/]