Jump to content
Search Community

Working with ScrollTrigger while in css zoom on body

Tyco Interactive test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

In our website we set zoom out on our body, calculated by js on every resize, so that we maintain the layout of our desktop (1920) on smaller resolutions (from 1200 up).

We then calculate the actual vw/vh by multiplying the clientWidth and clientHeight with the zoom factor.

The problem is that ScrollTrigger, of course, uses regular clientWidth/clientHeight and vw/vh and our method throws all of its calculations off.

I think that the solution would be using Gsap's scrollerProxy, but I did not succeed in that. What I would like is to override ScrollTriggers clientWidth and clientHeight with my calculated values.

 

Here is the actual code I use for setting the body zoom, so that my question is better understood:

 

<style>
    :root {
        --desktop-breakpoint: 1919.98px;
        --zoom-breakpoint: 1200.02px;
        --browser-zoom: 1;
        --browser-height: 100vh;
        --browser-width: 100vw;
    }

    @media (min-width: 1200.02px) and (max-width: 1919.98px) {
        body {
            zoom: var(--browser-zoom);
        }
    }
</style>

<script>
    function setZoom() {
        const doc = document.documentElement;

        // set the zoom value for matching laptop view to desktop
        let desktopBreakpoint = parseInt( getComputedStyle( document.documentElement ).getPropertyValue( '--desktop-breakpoint' ) ) ?? 1920;
        let zoomBreakpoint = parseInt( getComputedStyle( document.documentElement ).getPropertyValue( '--zoom-breakpoint' ) ) ?? 1200.02;
        doc.style.setProperty( '--browser-zoom', `${parseFloat(window.innerWidth / desktopBreakpoint).toFixed(2)}` );

        // set the css-variable value for --browser-height - take in account zoom out and add it to maintain actual 100vh
        // check if current window width is less than the zoom breakpoint, if so, set the zoom to 1
        let zoom = desktopBreakpoint / window.innerWidth;
        if ( window.innerWidth < zoomBreakpoint || zoom < 1 ) {
            doc.style.setProperty( '--browser-height', `${window.innerHeight}px` );
            doc.style.setProperty( '--browser-width', `${window.innerWidth}px` );
        } else {
            doc.style.setProperty( '--browser-height', `${window.innerHeight * zoom}px` );
            doc.style.setProperty( '--browser-width', `${window.innerWidth * zoom}px` );
        }
    }

    setZoom();
</script>
Link to comment
Share on other sites

It's very difficult to troubleshoot without a minimal demo; the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

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...