sitepositive Posted December 9, 2021 Share Posted December 9, 2021 I created a white background for a title on a block by custom css. I would like to create a fade in effect when the user scrolls and the block becomes visible. I want to add classes dynamically and do the rest in custom css. My first step worked: With code injection I can add a class .preFade to my block: window.onload = function myFunction() { var element = document.getElementById("block-85f05db57d57001f69ee"); element.classList.add("preFade"); } My second step failed: I wanted user Intersection Observer to add another class .fadeIn when the block becomes visible: const appear = document.querySelector('.preFade'); const cb = function(entries){ entries.forEach(entry => { if(entry.isIntersecting){ entry.target.classList.add('fadeIn'); }else{ entry.target.classList.remove('fadeIn'); } }); } const io = new IntersectionObserver(cb); io.observe(appear); My JS skills are pretty poor, so any help is greatly appreciated. Link to comment
Beyondspace Posted December 9, 2021 Share Posted December 9, 2021 (edited) 21 hours ago, sitepositive said: I created a white background for a title on a block by custom css. I would like to create a fade in effect when the user scrolls and the block becomes visible. I want to add classes dynamically and do the rest in custom css. My first step worked: With code injection I can add a class .preFade to my block: window.onload = function myFunction() { var element = document.getElementById("block-85f05db57d57001f69ee"); element.classList.add("preFade"); } My second step failed: I wanted user Intersection Observer to add another class .fadeIn when the block becomes visible: const appear = document.querySelector('.preFade'); const cb = function(entries){ entries.forEach(entry => { if(entry.isIntersecting){ entry.target.classList.add('fadeIn'); }else{ entry.target.classList.remove('fadeIn'); } }); } const io = new IntersectionObserver(cb); io.observe(appear); My JS skills are pretty poor, so any help is greatly appreciated. Try the following one <script> (function(){ document.addEventListener('DOMContentLoaded',()=>{ const element = document.getElementById("block-85f05db57d57001f69ee"); element.classList.add("preFade"); let observer = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.isIntersecting) { entry.target.classList.add("fadeIn"); } else entry.target.classList.remove("fadeIn"); }); }, { threshold: 1 }); observer.observe(element); }); })() </script> If it can not be implemented, kindly share your site with the protected password so we can take a look more detail Hope it can help you Edited December 10, 2021 by bangank36 after checking BeyondSpace - Squarespace Website Developer 🖼️ Lightbox Studio (Enable Pinch/Zoom on lightbox - Lightbox captions only mode) 🗓️ Delivery Date Picker (Squarespace Date picker form field) 💫 Gallery block 7.1 workaround 🥳 No-code customisations for Squarespace 🚀 Learn how to rank new pages on Google in 48 hours! If you find my answer useful, let's leave a like or upvote so others with the same issue can find their solution. Thank you! Link to comment
sitepositive Posted December 10, 2021 Author Share Posted December 10, 2021 Many thanks for your suggestion. Unfortunately, there is still a problem with adding the second class. Here is the url: https://clavichord-garlic-a66t.squarespace.com password: sitepositive Link to comment
Beyondspace Posted December 10, 2021 Share Posted December 10, 2021 44 minutes ago, sitepositive said: Many thanks for your suggestion. Unfortunately, there is still a problem with adding the second class. Here is the url: https://clavichord-garlic-a66t.squarespace.com password: sitepositive I've just updated the previous code. Try it again and check if it works properly on your site. My testing result 2021-12-10_20-38-46.mp4 BeyondSpace - Squarespace Website Developer 🖼️ Lightbox Studio (Enable Pinch/Zoom on lightbox - Lightbox captions only mode) 🗓️ Delivery Date Picker (Squarespace Date picker form field) 💫 Gallery block 7.1 workaround 🥳 No-code customisations for Squarespace 🚀 Learn how to rank new pages on Google in 48 hours! If you find my answer useful, let's leave a like or upvote so others with the same issue can find their solution. Thank you! Link to comment
sitepositive Posted December 11, 2021 Author Share Posted December 11, 2021 Thanks Ba Ngan! This works really well. I made a tiny change in the else clause because I wanted the block to remain visible when the user scrolls past. Now it does exactly what I intended, many thanks for your help! Beyondspace 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment