Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2814

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T08:54:08+00:00 2024-11-26T08:54:08+00:00

JavaScript-30-Day-13

  • 61k

Slide in on Scroll

demo

ss.png

On Day-13 of JS-30 we made a Slide-in-on-Scroll, that is when you slide down, the image sort of slide themselves in form left or right. The images are hidden by default using CSS.

.slide-in {         opacity: 0;         transition: all 0.5s;       }        .align-left.slide-in {         transform: translateX(-30%) scale(0.95);       }        .align-right.slide-in {         transform: translateX(30%) scale(0.95);       }  
Enter fullscreen mode Exit fullscreen mode

Basically we have made their opacity to be 0 and using translate slightly pushed them off the window. We also do scale of 0.95 to get a fade-in effect.

During sliding in we add a active class to it which will make their opacity 1 and scale them back to normal size.

 .slide-in.active {         opacity: 1;         transform: translateX(0%) scale(1);       } 
Enter fullscreen mode Exit fullscreen mode

So let's get right into the JavaScript.

First thing we need to do is select all the images that we are going to be listening for by using slide-in class which we gave to all our images, for example:

      <img src="http://unsplash.it/400/400" class="align-left slide-in" />  <img src="http://unsplash.it/400/400" class="align-right slide-in" /> 
Enter fullscreen mode Exit fullscreen mode

const sliderImages = document.querySelectorAll(".slide-in"); 
Enter fullscreen mode Exit fullscreen mode

Now we'll create a function called checkSlide() that will run every time the person scrolls, so we'll add an eventListener for the scroll event.

window.addEventListener("scroll", debounce(checkSlide)); 
Enter fullscreen mode Exit fullscreen mode

Now you might be wondering what is this debounce thing we have wrapped around the checkSlide function.

Essentially the scroll event gets fired off hundreds of times in one scroll and this can cause a little bit of a performance issue.
We can check how many times the event gets fired off by using console.count(e).

function checkSlide(e) { console.count(e); } 
Enter fullscreen mode Exit fullscreen mode

So to avoid this performance issue we use a debouncer function. What this function essentially does is that whatever function is supplied to it and whatever wait interval is set in it, it makes sure that the passed function is run once every x seconds, where x is the wait interval in millisecond.

function debounce(func, wait = 20, immediate = true) {         var timeout;         return function () {           var context = this,             args = arguments;           var later = function () {             timeout = null;             if (!immediate) func.apply(context, args);           };           var callNow = immediate && !timeout;           clearTimeout(timeout);           timeout = setTimeout(later, wait);           if (callNow) func.apply(context, args);         };       } 
Enter fullscreen mode Exit fullscreen mode

Here func is the checkSlide function we have passed here debounce(checkSlide) and wait is 20 millisecond. So the debouncer function runs every time but the checkSlide function runs once every 20ms now, which can be verified using the console.count.

Note: It's always a good idea to decbounce scroll event.

Now inside the checkSlide function we'll do the math for when the Images should slide in and when should they slide out.

First of all we'll loop every single image and figure out when each image need to be shown.

What we want is while scrolling when we reach half of the image height on our window the image should slide-in and when we have scrolled past the bottom of the image we'll slide it out, so that we can have the same slide in effect when we scroll back up.

This calculates the distance in pixel of the image half way through

const slideInAt =             window.scrollY + window.innerHeight - sliderImage.height / 2; 
Enter fullscreen mode Exit fullscreen mode

Here window.scrollY gives how far we have scrolled down from the top of the browser window, but it gives the distance only upto the the top of the viewport and we want to know the distance upto the bottom of our window so we add window.innerHeight to it. Now since we want the animation to happen when we are exactly halfway through the image so we subtract that much distance using (sliderImage.height / 2).

Now slideInAt contains the exact pixel when the image should slide in.

Similarly we calculate when we reach the bottom of the image by using

const imageBottom = sliderImage.offsetTop + sliderImage.height; 
Enter fullscreen mode Exit fullscreen mode

Where sliderImage.offsetTop gives the distance in pixel between the top of he image and the top of the browser window and hence by adding sliderImage.height we get the bottom of the image.

Now we need to determine 2 things:

First is the image half shown which we do by:

const isHalfShown = slideInAt > sliderImage.offsetTop; 
Enter fullscreen mode Exit fullscreen mode

Secondly if we have not scrolled past the image:

const isNotScrolledPast = window.scrollY < imageBottom; 
Enter fullscreen mode Exit fullscreen mode

and only if both of them are true we add the active class to the slider images else we remove it.

if (isHalfShown && isNotScrolledPast) {             sliderImage.classList.add("active");           } else {             sliderImage.classList.remove("active");           } 
Enter fullscreen mode Exit fullscreen mode

Here is the complete JavaScript code:

function debounce(func, wait = 20, immediate = true) {         var timeout;         return function () {           var context = this,             args = arguments;           var later = function () {             timeout = null;             if (!immediate) func.apply(context, args);           };           var callNow = immediate && !timeout;           clearTimeout(timeout);           timeout = setTimeout(later, wait);           if (callNow) func.apply(context, args);         };       }        const sliderImages = document.querySelectorAll(".slide-in");       function checkSlide(e) {         sliderImages.forEach((sliderImage) => {           //halfway through the image           const slideInAt =             window.scrollY + window.innerHeight - sliderImage.height / 2;           //bottom of the iamge           const imageBottom = sliderImage.offsetTop + sliderImage.height;            const isHalfShown = slideInAt > sliderImage.offsetTop;           const isNotScrolledPast = window.scrollY < imageBottom;            if (isHalfShown && isNotScrolledPast) {             sliderImage.classList.add("active");           } else {             sliderImage.classList.remove("active");           }         });       }       window.addEventListener("scroll", debounce(checkSlide)); 
Enter fullscreen mode Exit fullscreen mode

and with this our project for the day was completed.

GitHub repo:

GitHub logo cenacrharsh / JS-30-DAY-13

Blog on Day-12 of javascript30

cenacr007_harsh

JavaScript-30-Day-12

KUMAR HARSH ・ Jun 12 '21

#javascript #github #webdev

Blog on Day-11 of javascript30

cenacr007_harsh

JavaScript-30-Day-11

KUMAR HARSH ・ Jun 11 '21

#javascript #html #webdev #css

Blog on Day-10 of javascript30

cenacr007_harsh

JavaScript-30-Day-10

KUMAR HARSH ・ Jun 10 '21

#javascript #html #webdev #github

Follow me on Twitter
Follow me on Linkedin

DEV Profile

You can also do the challenge at javascript30

Thanks @wesbos , WesBos to share this with us! 😊💖

Please comment and let me know your views

Thank You!

cssgithubjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.