davidjbradshaw / iframe-resizer

Keep iFrames sized to their content.
https://iframe-resizer.com
Other
6.63k stars 978 forks source link

Scrolling may make the page jump to the left #1197

Closed pluspol closed 2 months ago

pluspol commented 8 months ago

Describe the bug In a special case scrolling the parent page leads to it jumping to the left.

This happens when:

In this case iframe-resizer´ calculates a scroll position like300,400` which means it wants to scroll horizontally to 300px.

In case of only a) and b) iframe-resizer too tries to scroll horizontally to 300px but nothing happens as the page isn't scrollable at all. Only when adding c) it's possible to scroll programmatically because of the wider element.

This happens in Chrome and Firefox.

Workaround Luckily it's rather easy to override the scroll behaviour and disable horizontal scrolling to fix this issue:

const [iframe] = iframeResizer({
    scrollCallback: (position) => {
        window.scrollTo({top: position.y});
        return false;
    },
}, rawIframe);

To Reproduce Steps to reproduce the behavior: Is there a template for a Codesandbox that can easily be used to provide an example?

Here's the css of the parent page:

    .bg {
        position: relative;
        display: flex;
        flex-direction: row;
        padding: 45px 0;
        width: 100%;
        max-width: 1180px;

        box-sizing: border-box;
    }

    .bg:before {
        position: absolute;
        left: -100vw;
        top: 0;
        width: 200vw;
        height: 100%;
        background-color: red;
        content: " ";
        display: block;
    }

    main {
        width: 100%;
        max-width: 1180px;
        margin: 0 auto;
    }

And the relevant markup for the parent page:

<main>
    <div class="bg">aaaa</div>
    <div class="iframe-container"></div>
</main>

And the iframe is simply calling something like that:

window.parentIFrame.scrollToOffset(0, 200);

Expected behavior The page shouldn't jump to the left but stay where it is.

Possible solutions

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

davidjbradshaw commented 5 months ago

Looks like your using a very old version of this library

pluspol commented 3 months ago

No, unfortunately not. We're using the latest version 4.3.9.

davidjbradshaw commented 2 months ago

Just looking at this again and I think your workaround, is the solution. I've added an example into the new version 5 API documentation and the troubleshooting guide.

https://iframe-resizer.com/api/parent/#onscroll https://iframe-resizer.com/troubleshooting/#srcolling-does-not-scroll-to-where-i-want-it

davidjbradshaw commented 2 months ago

btw in v5 it is now slightly cleaner, as onScroll() now also passes top and left

const [iframe] = iframeResizer({
  scrollCallback: ({ top }) => {
    window.scrollTo({ top });
    return false;
  },
}, rawIframe);