Hey Guys, I have an image on my website that I want to scale down when the user scrolls, but I can’t figure out how to make it work.
Basically, on the 4th section of my website, There’s an image that I want to have initially be very large, but as the user scrolls down it becomes smaller and moves a bit to the right to not block any content.
I’ve been trying to figure out the code for this, but I haven’t found anything remotely similar to work with. All I can see is making background scrolling images and more info on scroll effects (sticky, etc).
I’ve tried a lot of code, but here’s what I’ve got at the moment:
import wixWindow from ‘wix-window’ ;
$w . onReady ( function () {
const image = $$ ( “#astronautMan” );
const initialWidth = 2002 ;
const initialHeight = 2270 ;
image . style ( ‘width’ , ${ initialWidth } px
);
image . style ( ‘height’ , ${ initialHeight } px
);
image . onViewportEnter (() => {
const scrollPosition = wixWindow . scrollY ;
const newWidth = Math . max ( 0 , initialWidth - scrollPosition * 0.5 );
const newHeight = Math . max ( 0 , initialHeight - scrollPosition * 0.5 );
image . style ( 'width' , ` ${ newWidth } px` );
image . style ( 'height' , ` ${ newHeight } px` );
});
});
Unfortunately, it does nothing. Would really appreciate some help / being pointed in the right direction.
EDIT: While messing with the code, I’ve noticed that I don’t think there’s any sort of Velo code that can actually change the size of an image. I’ve tried a couple of different syntaxes, but nothing works. So, if I can’t scale image with Velo, would there be another way to achieve this?
Thanks!