Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic topSpacing ? #3

Open
Stibo opened this issue Mar 17, 2021 · 3 comments
Open

Dynamic topSpacing ? #3

Stibo opened this issue Mar 17, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@Stibo
Copy link

Stibo commented Mar 17, 2021

Hi

Is it possible to change the topSpacing value "on-the-fly"? We have a responsive sticky header that changes height during scroll or resize (and at some point disappears completely) and a sidebar that has always the same distance to the header (or the top document border if the header is not visible). If we scroll, the sidebar pans with a spacing (like 50px) from the header that is 100px in height. So the sidebar should be positioned 150px from the top document border. If the header shrinks to 50px, the sidebar should be 100px from the top document. And if the header dissapears completely, the sidebar distance between document top and sidebar should be 50px. Is this possible with your sticky plugin? Can I overwrite the topSpacing value while the script is already running? Or does topSpacing support a dynamic function?

@vladdancer
Copy link

vladdancer commented May 23, 2022

We had also this issue but in the context of dynamic main content region with top sticky header.
Here is workaround:

// File with content observer callback.
const resizedEvent = new CustomEvent("b5Theme.resizeObserver.observeContentRegion", {
  bubbles: true,
  detail: {
    width: rect.width,
    height: rect.height
  }
});
element.target.dispatchEvent(resizedEvent);
// File with sticksy handler.
const defaultBlockPadding = 20;

let stickyEl = new Sticksy('.block-fixed-on-scroll', {
  topSpacing: defaultBlockPadding
});

window.addEventListener(
  "b5Theme.resizeObserver.observeContentRegion",
  e => {
    const contentHeight = e.detail.height;

    if (sidebar) {
      sidebar.style.height = `${contentHeight}px`;
    }

    // In sticksy's minified version there is no "_props" property.
    const props = stickyEl?._props ?? stickyEl?.t ?? false;

    if (props) {
      // Fix also sticky block padding when admin toolbar is on the page.
      const bodyPaddingTop = parseInt(body.style.paddingTop || 0);
      props.topSpacing = bodyPaddingTop + defaultBlockPadding;
    }
    stickyEl.hardRefresh();
  }
);

The key here: stickyEl._props.topSpacing = value;

@kovart kovart added the enhancement New feature or request label May 29, 2022
@Stibo
Copy link
Author

Stibo commented Jul 15, 2022

That sounds great, thanks for sharing! We'll try that out in our next project.

Still, would be nice if this feature would be supported by the plugin itself, without work around.

@vladdancer
Copy link

I'm glad if this could help you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants