Site logo

Archived topic

fade-in animation on scroll

5 replies · Started by chris on May 14, 2021

Viewing posts 1–6 of 6

Hi, im trying to make each element fade in when the user scrolls with it in view.
I have added the css just not sure why the js isnt working as it should, im trying to impliment it via snippets plugin:

(function() {
var elements;
var windowHeight;

function init() {
elements = document.querySelectorAll('.hidden');
windowHeight = window.innerHeight;
}

function checkPosition() {
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var positionFromTop = elements[i].getBoundingClientRect().top;

if (positionFromTop - windowHeight <= 0) {
element.classList.add('fade-in-element');
element.classList.remove('hidden');
}
}
}

window.addEventListener('scroll', checkPosition);
window.addEventListener('resize', init);

init();
checkPosition();
})();

Coming back with error on line 2. Not sure why?

Hi there,

i am not sure how you're adding it in code snippets - but that is Javascript and needs to be hooked into the page.
You can create a new Hook Element, add the script like so:

<script>
// Your Javascript goes here
</script>

Then set the Hook to wp_footer
And set the Display Rule Location(s) to where you want the code to fire.

Ok so i have done that now the code is active but the element which has classes hidden & fade-in-element fades in on page reload and then hides? i thought the js is telling it to behave the opposite and fade in when in view?

We are not able to help with custom code that's not a part of GP in this support forum.

I would recommend checking with the code author.

Thanks for your understanding.

So i am still struggling with this one. (I know Leo said its not within the support but i saw another user get support and achieve what they wished to do)
- Only asking again here because of this users post

I am trying to follow the post & the generate press support replies to achieve the same animation on scroll effect.

So i create hook with this JS.

<script>
$(window).on(“load”,function() {
$(window).scroll(function() {
var windowBottom = $(this).scrollTop() + $(this).innerHeight();
$(“.fade-in”).each(function() {
/* Check the location of each desired element */
var objectBottom = $(this).offset().top + $(this).outerHeight();

/* If the element is completely within bounds of the window, fade it in */
if (objectBottom < windowBottom) { //object comes into view (scrolling down)
if ($(this).css(“opacity”)==0) {$(this).fadeTo(500,1);}
} else { //object goes out of view (scrolling up)
if ($(this).css(“opacity”)==1) {$(this).fadeTo(500,0);}
}
});
}).scroll(); //invoke scroll-handler on page-load
});
</script>

Then make css class like this
@keyframes fade-in {
from {opacity: 0; transform: scale(.7,.7)}
to {opacity: 1;}
}

.fade-in {
animation: fade-in 1.4s;
}

But when I apply the class "fade-in" to the container I wish to be animated when scrolled past, it just doesn't, the container appears and uses the animation on page refresh ONLY. Absolutely scratching my head over what I thought to be a simple task, surely cant be the only one looking to achieve this...

Getting error with this JS running ( Uncaught SyntaxError: Invalid or unexpected token ) Not sure if that has anything to do with it.
I Feel like I'm missing something simple.

This archived topic is closed to new replies.