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

ScrollTo - anchor link correct position #830

Open
Matteo-pd opened this issue Aug 31, 2023 · 2 comments
Open

ScrollTo - anchor link correct position #830

Matteo-pd opened this issue Aug 31, 2023 · 2 comments

Comments

@Matteo-pd
Copy link

Matteo-pd commented Aug 31, 2023

Problem:

I have a menu with anchor links to the sections (#item-1, #item-2, ...).
When the targeted section is not visible yet, then the offset top position of the section is wrong.

How can I read the correct section position after clicking on the anchor link?

Code:

https://codepen.io/matteokrr/pen/VwqKxbL

$(document).on('click', 'a[href^="#"]', function (e) {
  e.preventDefault();
  var target = $(this.hash);

  if (target.length) {
    let headerHeight = $('.header').outerHeight();
    $('html, body').stop().animate({
        scrollTop: target.offset().top - headerHeight - 20
    }, 2000, 'swing');
  }
});

I'm using AOS animation:

[data-aos='zoom-in-down'] {
  transform: translate3d(0, -200px, 0) scale(.6);
}
@Matteo-pd Matteo-pd changed the title Anchor scroll Anchor scroll position Aug 31, 2023
@Matteo-pd
Copy link
Author

Any idea?

@Matteo-pd Matteo-pd changed the title Anchor scroll position ScrollTo - anchor link correct position Sep 11, 2023
@ericdsd
Copy link

ericdsd commented Jul 3, 2024

Hi have the same issue with fade-up animation on an element that is just before my anchor alement and it break the scrolled position. The scroll is using only native css, no js for the scroll itself.

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

No branches or pull requests

2 participants