Jump to content
Search Community

horizontal scroll with one large image not working

LSchneiderman test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I'd like my page to scroll normally until it gets to this one hugely wide image. Then I want it to scroll horizontally until it gets to the end of the image, and then resume scroll normally.

 

I was able to get it to work, kind of. It scrolls, but not to the end of the image, which is huge, and the image should be shrunken to fit within the height of one screen.

 

Can someone help me understand what I'm doing wrong please?

See the Pen qBJmJvq by lschneiderman (@lschneiderman) on CodePen

Edited by LSchneiderman
I made some progress, so changed my codepen
Link to comment
Share on other sites

  • Solution

Hi,

 

You're accessing innerWidth directly on your code. That's basically the width of the browser window including the scroll bar:

https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth

 

You should move the element it's own width minus the screen width. This seems to work the way you expect:

const image = document.querySelector(".massiveImage");

gsap.to(image, {
  x: () => - (image.clientWidth - window.innerWidth),
  ease: "none",
  scrollTrigger: {
    trigger: image,
    start: "top top",
    end: () => (image.clientWidth - window.innerWidth),
    scrub: true,
    pin: true,
    invalidateOnRefresh: true,
    anticipatePin: 1,
    markers: true,
  }
});

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...