I like these a lot. Saves myself time and frustration.
@markday31457 жыл бұрын
Thanks for the series! Having real-world homework helps me hone my skills better. Once I knew what you were going for, I coded it up a little differently. I went for variables to represent the important offsets in the window and image, then did simple comparisons between them. Seemed more readable to me. // Check all of the images and slide them in or out as needed function slideImages(e) { // What part of the content is visible in the window? const windowTop = window.scrollY; const windowBottom = window.scrollY + window.innerHeight; images.forEach(function(image) { const imageTop = image.offsetTop; const imageBottom = image.offsetTop + image.height; const imageMiddle = (imageTop + imageBottom) / 2; // The image should slide in if its middle is visible in the window if (imageMiddle >= windowTop && imageMiddle windowBottom) { image.classList.remove('active'); } }); }
@danbuild9776 жыл бұрын
Yeah I like that a little better too. Thank you Mark!
@davlewis20116 жыл бұрын
Glad I came to KZbin for this vs his site, so I can read people's comments. I definitely like your implementation a lot better. It makes sense what is happening. I love wes but his was a little confusing. Over a year ago and your comment is still providing value. Thank you for this!
@flamethrower8832 жыл бұрын
Great exercise. I personally don't like the feature of removing the active class when the user scrolled past the image, so I did not include it in my solution. But, to each their own.
@swastikrana26702 жыл бұрын
Hi Paulo , Did you easily understand the calculation part??
@nomdplume61666 жыл бұрын
Thanks for making this! Super useful and well done.
@thomasprior57135 жыл бұрын
Awesome video. What would I do if I wanted to slide in paragraphs or whole divs?
@andrewvathanakamsang64045 жыл бұрын
You can apply the .slide-in class to the div but instead of sliderImage.height you would want to do sliderImage.offsetHeight. Perhaps rename sliderImage to sliderDiv for clarity.
@horoman7 жыл бұрын
Hey Wes Bos the quality of the video looks too much compressed
@slicerabbit61666 жыл бұрын
that's what youtube does when the video contains a lot of information, which this does. all the text on the left accounts for a lot of information. find a video with noise in it, and you'll see the same thing.
@scriptskillz5 жыл бұрын
Any reason not to use intersection observer instead?
@aya22223 жыл бұрын
I'm using intersection observer for my project but still I think we need to understand how it works without Intersection Observer API .
@san_tient6 жыл бұрын
its hard to imagine numbers.. you have to console log to understand values of variables if dealing with numbers
@ИванКурочкин-с9м5 жыл бұрын
Now Wes Bos, finally tell us what the music is playing at the beginning.
@eh-lo2do5 жыл бұрын
what about getBoundingClientRect() ?
@danbuild9776 жыл бұрын
Hi Brilliant Tutorials as always.I noticed that you use the .'sliderImage' variable quite heavily. Is there a way to explicitly state that variable and still have it make contextual sense in the current code? I find it a little confusing as you appear to have implied its use from the forEach. Rather than stating it explicitly like you did with its collective superior variable - (.sliderImages).
@sateeshmodukuru23 жыл бұрын
Is there a draw back to writing the function like this: function checkSlide() { sliderImages.forEach((sliderImage) => { const slideInAt = window.scrollY + window.innerHeight > sliderImage.offsetTop + sliderImage.height / 2; const scrolledPast = window.scrollY > sliderImage.offsetTop + sliderImage.height; if (slideInAt) sliderImage.classList.add("active"); else sliderImage.classList.remove("active"); if (scrolledPast) sliderImage.classList.remove("active"); }); }
@shubhamdas94894 жыл бұрын
What's the text editor that you are using????
@uzair0044 жыл бұрын
VS code
@bluex2173 жыл бұрын
stuck getting this to work as offsetTop is undefined
@DogmaKrone6 жыл бұрын
It's not working for me. The only thing i know is that the const slideInAt is always NaN. It became a number only when i delete the " - sliderImage.Height / 2".
@woutii6 жыл бұрын
I got the same problem :(
@XxBobynoobxX5 жыл бұрын
Replace the height by offsetHeight and that's it
@nathancornwell14554 жыл бұрын
its lowercase h on height ...JS is case sensitive
@vergilkelley9015 жыл бұрын
also can you show the html? Im not sure I have all of the classes attached to the correct elements because its not working.
I can console log the scrollY but I cant get any console logs to show after sliderImages.forEach(sliderImage => {
@kamransiddique93116 жыл бұрын
I also want to add effects to text, so how I have to code.
@lardosian7 жыл бұрын
How many years experience would be required to be expected to be able to write that debounce function, thanks.
@shashankmadan17116 жыл бұрын
a whole lot. its functional js...
@NikolaZagorac5 жыл бұрын
Thanks!
@NikoBellicGTAIVPL Жыл бұрын
USING INTERSECTION OBSERVER API (images both appear and hide on 50% of their height): const sliderImages = document.querySelectorAll('.slide-in'); const createObserver = () => { const options = { threshold: 0.5 } const observer = new IntersectionObserver(entries => { entries.forEach(entry => entry.target.classList.toggle('active', entry.isIntersecting)) }, options); sliderImages.forEach(sliderImage => observer.observe(sliderImage)) } window.addEventListener('load', createObserver);
@markocatela96066 жыл бұрын
Hello Wes and everybody else... awesome tutorial! I managed to do all that but my problem is that my content needs to slide in not only when i SCROLL to specific point but when i click on anchor in my navbar which leads me to the position in html where my sliding is aswell... When i click on navbar it brings me to this point and event is not triggering untill i scroll up or down a bit (even 1px will trigger it)... anyone here knows the solution to this issue? thank you
@droodthedude5 жыл бұрын
did you find a fix for it? I tried to simply add an event listener on the nav links and add the class .active when clicked, but it does not seem to work constantly. Kinda weird :D
@karlomoonblade5 жыл бұрын
get the id of the area then id.pageoffestY
@nathancornwell14554 жыл бұрын
you could honestly do that with nothing but CSS and the :target pseudo class
@Suneriins2344 жыл бұрын
Just without debounce code look like this.Only 10 lines..!I love it! const allimages = document.querySelectorAll('.slide-in'); window.addEventListener('scroll',slidein); function slidein() { allimages.forEach(images => { const sliderimages = (window.scrollY + window.innerHeight) - images.height / 2; const sroldown = sliderimages > images.offsetTop; if (sroldown) {images.classList.add('active') } else { images.classList.remove('active'); } }) }
@kirtperez80483 жыл бұрын
But you need debounce for performance issues...
@Gentarozzo6 жыл бұрын
Not working in IE... also IE 11
@vadimrozhansky98436 жыл бұрын
Commonly I agree with you, but specifically IE doesn't support default parameters in functions (and you can see them in debounce function's definition). And it doesn't support forEach method. And window.scrollY too... But all this easy to fix.
@vitfl25806 жыл бұрын
Well, use BABEL in your workflow and let him transpile your ES6+ code to standards supported by browsers that you need.
@ИванКурочкин-с9м5 жыл бұрын
@@vadimrozhansky9843 IE 11 supports forEach.
@abdelrahman50945 жыл бұрын
convert the node list to an array first becuase forEach can't work with node list directly
@nathancornwell14554 жыл бұрын
@@ИванКурочкин-с9м I.e. doesn't support forEach on nodelists though
@khotambakhromov5 жыл бұрын
can someone give a bit description to the debounce() function? it is interesting to know ) and what should I learn to understand these kinds of functions?
@nathancornwell14554 жыл бұрын
So, theres a lot going on in that particular debounce function. But, essentially what is happening is that debounce function is running immediately on page load. But , it returns a function and that function is calling a setTimeout . When you assign a setTimeout to a variable, it returns an ID. So, each time that return function runs, it checks to see if there is a timeout and if there is , it clears the timeout then starts the timeout all over . And if enough time passes(in this case 20ms) without that return function being called, the function inside the setTimeout will finally run. So you wanna learn about first class functions, function expressions, and then call ( ) , apply ( ), bind ( ) and setTimeout.
@anokhps30882 жыл бұрын
@@nathancornwell1455 can you explain what immediat argument is doing in the debounce function and why later function is set.