<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vidit Shah -  tech-savvy -  web technologist on Medium]]></title>
        <description><![CDATA[Stories by Vidit Shah -  tech-savvy -  web technologist on Medium]]></description>
        <link>https://medium.com/@ViditIsOnline?source=rss-380d0e8e825f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*YdNFVfvyo5vdR_-K.jpg</url>
            <title>Stories by Vidit Shah -  tech-savvy -  web technologist on Medium</title>
            <link>https://medium.com/@ViditIsOnline?source=rss-380d0e8e825f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:59:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ViditIsOnline/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What’s more to IntersectionObserver — lazy load and beyond!]]></title>
            <link>https://medium.com/@ViditIsOnline/whats-more-to-intersectionobserver-lazy-load-and-beyond-201f487a8221?source=rss-380d0e8e825f------2</link>
            <guid isPermaLink="false">https://medium.com/p/201f487a8221</guid>
            <category><![CDATA[web-design]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Vidit Shah -  tech-savvy -  web technologist]]></dc:creator>
            <pubDate>Sat, 07 Jul 2018 16:09:30 GMT</pubDate>
            <atom:updated>2018-07-07T16:09:30.205Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*w-JX0MI-T49DfiQC" /><figcaption>“A red sports car sputtering a flame in its exhaust in a night time setting beside a parking lot” by <a href="https://unsplash.com/@o5ky?utm_source=medium&amp;utm_medium=referral">Oscar Sutton</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A few months ago, I came across a situation in one of my projects. We had to implement a so called <em>Infinite Scroll </em>on a product listing page. Being an e-commerce application, this page was a very critical part of the project and hence had to be performant.</p><p>A typical approach was to use the <em>scroll </em>event on the <em>window </em>object. It involved starting with a limited set of entities in the product list and then adding more to the list when we hit bottom of the page. A simple demonstration of this approach is in the following codepen.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fviditisonline%2Fembed%2Fpreview%2FZRgOPZ%3Fheight%3D600%26slug-hash%3DZRgOPZ%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fviditisonline%2Fpen%2FZRgOPZ&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/39864b184654627dfcb2082b0f86747c/href">https://medium.com/media/39864b184654627dfcb2082b0f86747c/href</a></iframe><p>The above approach can be optimized in a lot of different ways such as:</p><ul><li>throttling the <em>onscroll</em> callback function</li><li>not waiting for the page to be scrolled till the bottom but loading resources just before a predefined threshold range</li><li>removing elements from the DOM which are way out of the viewport</li></ul><p>Despite accepting variety of ways to optimise this approach, the drawbacks are the usual ones associated with any <em>window.onscroll</em>,<em> </em>specifically the heavy occupancy of the main js thread.</p><p>Basically what that means to a user is a laggy interface!!</p><p>But then I came across a few articles on web which talked about a new hero in town called <em>IntersectionObserver. </em>To my luck, most of the examples I found across articles, were related to my use case someway or the other. Now, my code got a little changed and it’s simple demonstration is in the following codepen.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fviditisonline%2Fembed%2Fpreview%2FbKXwLz%3Fheight%3D600%26slug-hash%3DbKXwLz%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fviditisonline%2Fpen%2FbKXwLz&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F1002478.bKXwLz.small.d9c1e776-da05-4419-86f0-7164e5c5e3f6.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/0755ed3b0a12a6f5a185fa94582c6958/href">https://medium.com/media/0755ed3b0a12a6f5a185fa94582c6958/href</a></iframe><p>As I saw <em>IntersectionObserver</em> functioning for the first time, my reaction was</p><blockquote>Wow!</blockquote><p>There was a drastic difference between the two approaches to achieve the same functionality. As can be seen, the number of times the callback for <em>scroll </em>event called is way way way more than that of <em>IntersectionObserver </em>(Basically, the <em>scroll</em> callback is called as many times more as the content is present on page till I reach the end of the page whereas the <em>IntersectionObserver </em>callback is called just once!).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BVBj4uh0bp03Wjdm0VHH3g.gif" /><figcaption>Infinite scroll using window.onscroll (left) and IntersectionObserver (right)</figcaption></figure><p>This difference alone was enough to scrap any and all <em>onscroll </em>events and replace them with <em>IntersectionObserver.</em></p><p>While this use case is the most common use of <em>IntersectionObserver </em>and can also been seen from the number of examples available on the web<em>, </em>the possibilities are way many. I demonstarte a few others below.</p><h3>Animate when a section appears completely in the viewport</h3><p>There are times when you want to run an animation or for that matter even a video, only when the section appears completely in the viewport. This is a very common use case and can be easily achieved with <em>IntersectionObserver.</em></p><p>The <em>IntersectionObserver </em>constructor takes a callback as it’s first argument but also takes an <em>options </em>object<em> </em>as a second argument. To read more about the options available, one can check following articles:</p><ul><li><a href="https://developers.google.com/web/updates/2016/04/intersectionobserver">https://developers.google.com/web/updates/2016/04/intersectionobserver</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API</a></li></ul><p>In the codepens I shared earlier, we didn’t use the options arguments which meant that by default the elements which we will observe would be observed with respect to document viewport. Also importantly, the threshold would be 0. By that what I mean is as soon as the element to be observed intersects our root (document viewport here) by 1px, our callback is fired. <em>IntersectionObserver </em>takes<em> </em>threshold values as an array with values ranging from 0 to 1. So for example, if I give threshold as [0, 0.5, 1], the callback will get fired 3 times. First, as soon as the first pixel crossed my viewport. Second when 50% of the element appears in viewport. And finally third, when all of the element appears in the viewport. I call such convenience <strong>Heavenly!</strong></p><p>The following gif shows this affect and also attached is a codepen of the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*I2F0JEX0FeOhb4nQtFDEIg.gif" /><figcaption>Animate when in viewport using IntersectionObserver</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fviditisonline%2Fembed%2Fpreview%2FbKXBRd%3Fheight%3D600%26slug-hash%3DbKXBRd%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fviditisonline%2Fpen%2FbKXBRd&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F1002478.bKXBRd.small.b14ae87a-17c2-482c-82e8-83cb893e1c67.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/c144c817f50a85b7f20606807d951399/href">https://medium.com/media/c144c817f50a85b7f20606807d951399/href</a></iframe><h3>Determine which part of page user spends most time on</h3><p>We can use <em>IntersectionObserver </em>to determine parts of pages where user spends most time on (Analytics teams be like 🤑). Now, there could be a lot of deciding factors such as the type of users, the content itself and lots others. I just wanted to give a simple example demonstrating the power of <em>IntersectionObserver.</em></p><p>Below is a gif demonstrating this use case (<a href="https://codepen.io/viditisonline/pen/aKepbQ">link to codepen</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*4EBQi7AL2IAbasJExdMZ7A.gif" /><figcaption>Measure time spent on content using IntersectionObserver</figcaption></figure><h3>Is IntersectionObserver the holy grail? Is window.onscroll dead?</h3><p>While <em>IntersectionObserver </em>does solve most of the problems over <em>window.onscroll</em>, there are still places where you would need your scroll events such as a progress bar showing amount of scrollable content left on a page or similar actions which need continuous scroll data.</p><p>The usage is still low for <em>IntersectionObserver </em>compared to what it deserves. Browser compatibility is also an issue for browsers such as IE11 😩😭 and as a fact even for Safari 😳!! Although a <a href="https://github.com/w3c/IntersectionObserver/tree/master/polyfill">polyfill</a> is available for them and even for browsers as old as IE7 (performance is an issue for these browsers though).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vy9vPaZY0PdV3zbP7PiEuQ.png" /><figcaption>‘Can I use’ support chart fort <em>IntersectionObserver</em></figcaption></figure><p>Hopefully more and more projects make notice of this gem and adopt it sooner. Also, it’s upto the browsers to make it widely available (Apple I am staring at you😒). The implementation by browsers also needs to remain as close to spec as possible for wider acceptability (Edge you are the culprit here😑).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=201f487a8221" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>