Skip to content
Advertisement

Zoom on Scroll Down & Scroll up the zoomed element when scale is more than full screen

I tried to make zoom in Zoom out on scroll. The element is zooming on scrolling but I want to make it(the zoomed element) go up once it reached the full width of the screen.

Here’s my code:

JavaScript

Fiddle: https://jsfiddle.net/mayureshpitale/6etpn0vs/3/

I am trying to make something like this: http://truegossiper.com/vilson17/

Advertisement

Answer

A bit of a different approach using the scroll event and window.scrollY for positioning together with requestAnimationFrame (RAF).

This also works, if the image (or other content) is not squared.

The problem with the wheel event is, that it does not trigger when the user uses the scrollbar or arrow keys to scroll the page.

The problem with the scroll event is, that it executes a lot… that’s why RAF is used, to only execute necessary zoom changes.


Keep in mind, this exact code works with the document width when the page is loaded. If the user resizes the window or changes the browsers zoom, the element will resize to the same scale as before. You could fix this with some extra code by using a ResizeObserver. (Remove the existing event listener, resize the Image and execute parts of the code again, every time the document is resized…)

You will notice this, when you try to view the below snippet as full page.

JavaScript
JavaScript
JavaScript

I got a bit carried away during coding and added some fading-in and -out to the blue background. Not necessary, but looks nice. You can remove the Fade ----- Fade parts of the code, without affecting functionality.

To alter zoom and fade speed, simply change the ZOOM_SPEED and FADE_SPEED variables.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement