Following "infinite horizontal scroll animation" tutorial from Kevin on Youtube; not working (help!)

So, I was following everything in this video: https://www.youtube.com/watch?v=iLmBy-HKIAw&t=530s&ab_channel=KevinPowell And everything was working right until the javascript part came in, where for some reason the "data-animated" attribute isn't being added. Here's screenshots of my code as I followed it from the tutorial:
Kevin Powell
YouTube
Create an infinite horizontal scroll animation
🎓 Did you know I have courses? Both free and premium ones: https://kevinpowell.co/courses?utm_campaign=general&utm_source=youtube&utm_medium=infinitescroll Infinite scroll animations for things like logos are relatively common these days, but there is a lot that we can do incorrectly when making one, so I decided to try and make one that respec...
No description
25 Replies
Kivory
Kivory3w ago
P.S, for a timestamp: it's around 5:30 that the javascript part starts.
Chris Bolson
Chris Bolson3w ago
If it isn't working you will be probably be getting an error message in the console. Have you checked that?
Kivory
Kivory3w ago
doesn't look like it. Console is empty
Chris Bolson
Chris Bolson3w ago
Do you have elements with the "scroller" class in your HTML? Are you sure that you don't have prefers-reduced-motion turned on?
Kivory
Kivory3w ago
here's the html as followed from the tutorial (yes I closed the li tags I just cropped it out of the screenshot because some of it is private)
No description
Kivory
Kivory3w ago
As for prefers-reduced-motion, using Chrome I turned it both on and off and nothing happened for either
Chris Bolson
Chris Bolson3w ago
You could add some console logs to check firstly if you are calling the addAnimation() function and then to check that you have any scrollers.
const scrollers = document.querySelectorAll(".scroller");
function addAnimation(){
console.log("animation function being called");
console.log(scrollers)
scrollers.forEach((scroller) => {
scroller.setAttribute("data-animated",true)
})

}
addAnimation();
const scrollers = document.querySelectorAll(".scroller");
function addAnimation(){
console.log("animation function being called");
console.log(scrollers)
scrollers.forEach((scroller) => {
scroller.setAttribute("data-animated",true)
})

}
addAnimation();
Kivory
Kivory3w ago
here's what my code and the console looks like after this
No description
No description
Chris Bolson
Chris Bolson3w ago
It looks like the node list is empty
Kivory
Kivory3w ago
im quite new, so do forgive me does "length: 0" when i expand the nodelist in the console mean that it's empty?
Chris Bolson
Chris Bolson3w ago
you should see something like this
No description
Chris Bolson
Chris Bolson3w ago
From your image capture it clearly shows that there is an element with the scroller class so there must be something else going in. How are you including the JavaScript file? If the JavaScript is being loaded before the HTML has finished loading the JS won't be able to find the elements as it has already run
Kivory
Kivory3w ago
"<script src="main.js"></script>" inside the <body> tag
Chris Bolson
Chris Bolson3w ago
right before the closing body tag or at the beginning?
Kivory
Kivory3w ago
at the beginning
Chris Bolson
Chris Bolson3w ago
OK, so that is probably the issue.
Kivory
Kivory3w ago
Ah, let me try putting it at the end it worked. Thank you so much!
Chris Bolson
Chris Bolson3w ago
Great. You can add the JS file at the beginning within the <head> tags but you need to add the "defer" attribute. <script src="main.js" defer></script> This instructs the browser to not load the file until the html has fully loaded Otherwise, as you have seen, the JS parses the DOM before it actually has any content so, in your case, "scrollers" is empty as they didn't exist when the JS checked the DOM.
ἔρως
ἔρως3w ago
this is not quite accurate. the html is parsed while the script is downloaded, and the script is executed only after everything has loaded or failed loading it is also important to specify that defer will execute the scripts in the order that they show in the html async doesnt guarantee that order and works closer to what you described
Chris Bolson
Chris Bolson3w ago
OK, but the key thing is that the JS is trying to find the elements before that they exist and that is what is was causing the issue.
ἔρως
ἔρως3w ago
i fully agree with that also, i dont see 'use strict'; on the file you should use it, as it prevents dumb mistakes
Chris Bolson
Chris Bolson3w ago
An alternative is to use type="module" on the script tag as that both defers it and automatically uses strict mode.
ἔρως
ἔρως3w ago
and dont you have to export stuff for it to work at all?
Chris Bolson
Chris Bolson3w ago
I don't think so. I just did a test and the only contents I put in the js file was a simple alert.
ἔρως
ἔρως3w ago
that is interesting if it works, then it works
Want results from more Discord servers?
Add your server