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));
}
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.
– Emerson Matheus
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– hugocsl
I checked several times and did not find the error, I took some prints of the code: https://imgur.com/a/3EDgdIv
– Emerson Matheus
@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
– hugocsl
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.
– Emerson Matheus
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
– hugocsl
Upload full code, html, css and js from the animation. https://jsfiddle.net/kuxf7mep/
– Emerson Matheus