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

How to use offset #25

Closed
mpetricek opened this issue Apr 25, 2018 · 16 comments
Closed

How to use offset #25

mpetricek opened this issue Apr 25, 2018 · 16 comments
Labels

Comments

@mpetricek
Copy link

Hi,

I'm trying to find out how to use offset function. Can you please write me a better explanation or perhaps better documentation?

Thank you!

@zadremal
Copy link

zadremal commented Apr 27, 2018

Agree with @mpetricek. Would be nice to have an example of using scroll with offset in documentation. For now, You can use custom scroll function:

const scrollWithOffset = (el, offset) => {
  const elementPosition = el.offsetTop - offset;
  window.scroll({
    top: elementPosition,
    left: 0,
    behavior: "smooth"
  });    

   <NavLink
        to="/#link"`
        scroll={el => scrollWithOffset(el, 150)}
    >
     link
    </NavLink>




@readeral
Copy link

readeral commented Oct 5, 2018

Agree with @mpetricek. Would be nice to have an example of using scroll with offset in documentation. For now, You can use custom scroll function:

const scrollWithOffset = (el, offset) => {
  const elementPosition = el.offsetTop - offset;
  window.scroll({
    top: elementPosition,
    left: 0,
    behavior: "smooth"
  });    

   <NavLink
        to="/#link"`
        scroll={el => scrollWithOffset(el, 150)}
    >
     link
    </NavLink>

You're missing a close brace after scrollWithOffset

@TheStu
Copy link

TheStu commented Feb 15, 2019

scroll={el => { el.scrollIntoView(true); window.scrollBy(0, -64) }}

bit hacky but it works as a one-liner. (64 being the height of my header / the amount I want offset).

@pedropcruz
Copy link

scroll={el => { el.scrollIntoView(true); window.scrollBy(0, -64) }}

bit hacky but it works as a one-liner. (64 being the height of my header / the amount I want offset).

Yeap this actually works, but if you want to put some animation on scrolling, you dont have it

@Vidit92
Copy link

Vidit92 commented Sep 30, 2019

This functions is working for me:

const scrollWithOffset = (el) => {
    const yCoordinate = el.getBoundingClientRect().top + window.pageYOffset;
    const yOffset = -80; 
    window.scrollTo({ top: yCoordinate + yOffset, behavior: 'smooth' }); 
}

<NavHashLink smooth to='/#link' scroll={el => scrollWithOffset(el)}>
     Link
</NavHashLink>

@SandMoshi
Copy link

SandMoshi commented Oct 25, 2019

I prefer @Vidit92's solution because it preserves the smooth scrolling.

Just a note, you can simply use scroll={scrollWithOffset} without explicitly stating the el input to make it more compact.

@crowlKats
Copy link

This functions is working for me:

const scrollWidthOffset = (el) => {
    const yCoordinate = el.getBoundingClientRect().top + window.pageYOffset;
    const yOffset = -80; 
    window.scrollTo({ top: yCoordinate + yOffset, behavior: 'smooth' }); 
}

<NavHashLink smooth to='/#link' scroll={el => scrollWithOffset(el)}>
     Link
</NavHashLink>

this does not work for me

@ghost
Copy link

ghost commented Aug 31, 2020

thank you all I was very stressed trying to figure this out for a client was losing my mind. much appreciated.

@sabago
Copy link

sabago commented Sep 30, 2020

This functions is working for me:

const scrollWidthOffset = (el) => {
    const yCoordinate = el.getBoundingClientRect().top + window.pageYOffset;
    const yOffset = -80; 
    window.scrollTo({ top: yCoordinate + yOffset, behavior: 'smooth' }); 
}

<NavHashLink smooth to='/#link' scroll={el => scrollWithOffset(el)}>
     Link
</NavHashLink>

this does not work for me

I was in the same boat until I ran "npm run build" and restarted localhost.
Hope this helps others having the same issue.
Works for me! Thanks for posting!

@rafgraph
Copy link
Owner

rafgraph commented Oct 9, 2020

Added a section to the readme with a link to this thread. This seems to be a common question, so if anyone would like to create a formal doc that explains how to scroll with offset, PRs are welcome.

@rafgraph rafgraph closed this as completed Oct 9, 2020
@matude
Copy link

matude commented May 5, 2021

This functions is working for me:

const scrollWidthOffset = (el) => {
    const yCoordinate = el.getBoundingClientRect().top + window.pageYOffset;
    const yOffset = -80; 
    window.scrollTo({ top: yCoordinate + yOffset, behavior: 'smooth' }); 
}

<NavHashLink smooth to='/#link' scroll={el => scrollWithOffset(el)}>
     Link
</NavHashLink>

this does not work for me

There's a typo: scrollWidthOffset vs scrollWithOffset . Works if you fix that.

@yuya-tani
Copy link

yuya-tani commented Dec 21, 2021

I'm using scrolling of in-page links with this offset.

The question is, if you jump to the link destination of the in-page link with a hash
If there is an image on that page, I think that the height of that image cannot be taken into consideration (because the image will be loaded later?).
Is there a workaround?

Page A

  • Sentence
  • Link PageB#section2 * Click

Page B

  • Section1
  • Sentence
  • Sentence
  • Image * It will fly to this position
  • Sentence
  • image
  • Section2 * I want to fly here
  • Sentence
  • Sentence

Best Regard.

@rafgraph
Copy link
Owner

@yuya-tani the best way is to account for the size of the image on the page before the image is loaded (use css to specify the space that the image will take up). Comments in #84 may help.

@yuya-tani
Copy link

yuya-tani commented Dec 21, 2021

Thank you for your quick reply.

Certainly, if the image is fixed, it is better to secure the height first in the component where the image is displayed. It's a good idea.

I saw #84 for reference, but if the height of the image changes dynamically
Show loading? There is an answer,
Is it possible to start smooth after transitioning and loading?

@ayoubkhan558
Copy link

ayoubkhan558 commented Dec 7, 2023

 const scrollWithOffset = (el) => window.scrollTo({ top: el.getBoundingClientRect().top + window.pageYOffset - 100, behavior: 'smooth' });
 <HashLink smooth scroll={el => scrollWithOffset(el)} to="/new/legal-compliance#TermsConditionsSec" className="nav-link active" aria-current="page" >
      Terms & Conditions
 </HashLink>

@kayleriegerpatton
Copy link

kayleriegerpatton commented Feb 24, 2024

The above solutions work, but when I replace pageYOffset with the scrollY alias (pageYOffset is deprecated), oddly it doesn't work.

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

No branches or pull requests