Animations in the elements happen as soon as the page loads

Asked

Viewed 164 times

0

Hello, in my project I have an animation in Javascript with the page scroll, which adds the Animate class, so I approach the element, but only some elements of the page are working perfectly, other elements are adding this class as soon as the page is loaded, without me even being near the item, and even when I move away from the element the class is not removed which should happen.

html:

<div data-anime="left"></div>

Css:

    [data-anime] {
        opacity: 0;
        -webkit-transition: all 0.3s;
        -moz-transition: all 0.3s;
        -ms-transition: all 0.3s;
        -o-transition: all 0.3s;
        transition: all 0.3s;
    }

    [data-anime = "left"] {
        transform: translate3d(-50px, 0, 0);
    }

[data-anime].animate {
    opacity: 1;
    transform: translate3d(0, 0, 0);
}

Javascript:

const debounce = function(func, wait, immediate) {
  let timeout;
  return function(...args) {
    const context = this;
    const later = function () {
      timeout = null;
      if (!immediate) func.apply(context, args);
    };
    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
  };
};

const target = document.querySelectorAll('[data-anime]');
const animationClass = 'animate';

function animeScroll() {
  const windowTop = window.pageYOffset + ((window.innerHeight * 3) / 4);
  target.forEach(function(element) {
    if((windowTop) > element.offsetTop) {
      element.classList.add(animationClass);
    } else {
      element.classList.remove(animationClass);
    }
  })
}

animeScroll();

if(target.length) {
  window.addEventListener('scroll', debounce(function() {
    animeScroll();
  }, 200));
}

1 answer

1

Most likely the problem is because of the team’s debounce, It is very high 200, if you decrease to 20 or 10 vc you will see that it will start to pick the animations better, because it will be checking the event in shorter times, and not in a large interval with 200. For that kind of animation sometimes it’s good to keep that in mind.

inserir a descrição da imagem aqui

Another thing you can adjust is this account, window.pageYOffset + ((window.innerHeight * 6) / 7) you were using 3/4, but with 6/7 you can make the element animate a little earlier, so even if the user scrollar very fast you have an advantage by adding the class as soon as the element enters the screen, 3/4 the animation only applies when the element has already climbed too...

Follow the code with the adjustments I made for you to test.

const debounce = function(func, wait, immediate) {
  let timeout;
  return function(args) {
    const context = this;
    const later = function () {
      timeout = null;
      if (!immediate) func.apply(context, args);
    };
    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
  };
};

const target = document.querySelectorAll('[data-anime]');
const animationClass = 'animate';

function animeScroll() {
  const windowTop = window.pageYOffset + ((window.innerHeight * 6) / 7);
  target.forEach(function(element) {
    if((windowTop) > element.offsetTop) {
      element.classList.add(animationClass);
    } else {
      element.classList.remove(animationClass);
    }
  })
}

animeScroll();

if(target.length) {
  window.addEventListener('scroll', debounce(function() {
    animeScroll();
  }, 5));
}
body {
  height: 200vh;
}

.tt {
  margin-top: 120vh;
}

[data-anime] {
    opacity: 0;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    -ms-transition: all 0.3s;
    -o-transition: all 0.3s;
    transition: all 0.3s;
}

[data-anime = "left"] {
    transform: translate3d(-50px, 0, 0);
}

[data-anime].animate {
    opacity: 1;
    transform: translate3d(100px, 0, 0);
    background-color: red;
}
<div class="tt" data-anime="left">123</div>

  • Good morning Hugo, I made the changes and continued with the same problem, 3 of the animated elements work normally, others below have no effect, so reload the page is already automatically added to the Animate class.

  • I did a test here putting more elements, and everything works normally.... https://imgur.com/xMP4XPh I get the impression that you may have written some wrong parameter around as a class name or something like that, or data-anime in the CSS you left out some property etc... Take a look at Devtools to see if JS is actually adding the class to the element and if it’s the right class... see ai https://imgur.com/tTAflTH

  • I checked several times and did not find the error, I took some prints of the code: https://imgur.com/a/3EDgdIv

  • @Emersonmatheus I made this code pen with the code... start by removing what is not part of the animation of the code to see if it is something there that is interfering with things... Here is the example https://codepen.io/hugocsl/pen/rgPRmd

  • It seems that the problem happens when I put the animated element inside the Section, outside works normal, the engracado is that in the first section of the page works normal. I honestly don’t know what the problem is.

  • Then go up your most complete code for a Jsfidelli or pro Codepen then I try to take a look if I have time here

  • Upload full code, html, css and js from the animation. https://jsfiddle.net/kuxf7mep/

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.