How to click a href not scroll to top năm 2024

Sure thing, i'm using Chrome 100.0.4896.127 on MacOS Monterary 12.3.1 (21E258). It's very hit and miss and for the majority of the time it works great! No secrets to reproduce it really apart from clicking a bunch of times. Sometimes i've found that clicking on a link before the scroll back to top has completed then that can trigger the jump to fail and the animation of scrolling back to top to complete, when that happens follow up clicks on the same link then repeatedly cause a quick jump down of about 50px before going back to the top.

I was also getting the problem in Safari Version 15.4 (17613.1.17.1.13) and I also tried a private tab in Firefox Developer Edition 99.0b3 both of which I was able to reproduce the problem but within 5-10 clicks. I think Chrome took 20-30 but that feels a bit anecdotal!

https://streamable.com/t9arrg If you can check out the video i've linked then you should be able to see it in action. In the video you can sometimes see the whole page scroll up, that's just my mouse getting a little over excited and not related to what i'm seeing. The relevant time codes for you are at:

- 0:22 where the scroll fails for the first time

- 1:06-1:09 (it's a little hard to see here because the whole page scrolled up but you you can see two things happen: 1 - scroll fail, 2 - scroll success but to wrong location as the section is partly offset

- 1:30 and 1:45 this time in full screen codepen - repeated scroll failure

I'm really sorry if this is something isolated and unique my end, happy to provide any more information that might be helpful

In order to fix this issue, for now, I added this in my main layout.tsx file. I had to convert it to a client component:

const pathname = usePathname()
    useEffect(() => {
        window.scroll(0, 0)
    }, [pathname])

Describe the Bug

Clicking on a link that redirects to a page makes the scroll bar be scrolled instead of having it at the top of the page. Requirements of the bug are described above

Expected Behavior

Expecting the scroll bar to be at the top without removing the font instance from the 2nd page.

To note that, on my real app, I don't have this font instance. Si the bug is probably introduced by some other package.

To avoid page focus moving to top and page getting scrolled to top, we can return 0 when a anchor or hyperlink is clicked on a webpage.

How to click a href not scroll to top năm 2024
Javascript Avoid Scroll To Top On Link Click

So just change the value from “#” to “javascript:void(0)”

Sample Hyperlink

Now with above trick, click will happen and user will be at same place instead of scrolling to top.

You might be thinking whats the use of this ?

We can write onclick event and perform some operation or we can write javascript or jquery handler which will perform some operation.

But in any case our focus will be at the same position, instead of scrolling and getting lost.

JQuery Solution :

If you are using JQuery and want to achieve same functionality then in JQuery we can call “event.preventDefault()” method to block default behavior.

So whenever we write click event of any anchor tag / hyperlink then at the end of the function, if we write event.preventDefault() then it will do the work.

In order to fix this issue, for now, I added this in my main layout.tsx file. I had to convert it to a client component:

const pathname = usePathname()
    useEffect(() => {
        window.scroll(0, 0)
    }, [pathname])

Describe the Bug

Clicking on a link that redirects to a page makes the scroll bar be scrolled instead of having it at the top of the page. Requirements of the bug are described above

Expected Behavior

Expecting the scroll bar to be at the top without removing the font instance from the 2nd page.

To note that, on my real app, I don't have this font instance. Si the bug is probably introduced by some other package.

A sticky or fixed menu is a very popular UX solution that displays a navbar at the top of the page to provide access to menu items at all times, even while scrolling pages. This simple addition can make it much easier for users to jump between your site content, especially on long-form pages.

However, while this method works well in general use, if a page uses anchors in the menu to allow users to instantly jump to specific sections of the page, we run into an issue. When an anchor is clicked, The page will scroll to position the anchor at the very top of the viewport, meaning that the section title and perhaps even part of the content will be obscured by the fixed menu.

Sticky header covering an anchor section

So far, the standard solution has been to add top margin and padding to the anchor sections, but this has often resulted in a lack of control over the spaces, preventing fine-tuning of the page layout.

Fortunately, we have a new, simple, one-line CSS solution: scroll-margin-top and scroll-padding-top.

How to click a href not scroll to top năm 2024

The scroll-padding-top property is applied to the parent container and acts just like a CSS top padding, defining offsets from the top of the scrolling area.

scroll-padding-top: ; 

You can use any px, em, rem,




  
  
Your stuff here
Your stuff here
Your stuff here

0,




  
  
Your stuff here
Your stuff here
Your stuff here

1, etc. value, as well as




  
  
Your stuff here
Your stuff here
Your stuff here

2, where the user agent determines the offset as 0px.

Let see how it works

You could add the scroll-padding-top CSS property to an




  
  
Your stuff here
Your stuff here
Your stuff here

4 element with a value of




  
  
Your stuff here
Your stuff here
Your stuff here

5. Now when you click the anchor link, the browser jumps to the anchor section but leaves padding of




  
  
Your stuff here
Your stuff here
Your stuff here

5 at the top, rather than scrolling the anchor point all the way to the top. With this, when the height of the sticky menu is




  
  
Your stuff here
Your stuff here
Your stuff here

7, the section the anchor point scrolls to will be wholly visible, separated from the sticky menu by that extra




  
  
Your stuff here
Your stuff here
Your stuff here

8.




  
  
Your stuff here
Your stuff here
Your stuff here
html {
  scroll-padding-top: 4rem;
}
nav {
  height: 3rem;
  position: sticky;
  top: 0;
}

That's it.

The




  
  
Your stuff here
Your stuff here
Your stuff here

9 property, in simple terms, defines the top margin of the anchor sections (i.e. the container‘s children) that the browser will use when snapping the scrolled element into place.

scroll-margin-top: ; 

This property refers to the values defined with length units: px, em, rem,




  
  
Your stuff here
Your stuff here
Your stuff here

0, etc.

The end result of using the scroll-margin-top property will be basically the same as when using scroll-padding-top, in that the scrolled section will be visible and separated slightly from the top of the viewport to allow room for the menu, but it achieves this result via a different method.

Let see how it works

The




  
  
Your stuff here
Your stuff here
Your stuff here

9 property should be applied to each anchor

html {
  scroll-padding-top: 4rem;
}
nav {
  height: 3rem;
  position: sticky;
  top: 0;
}

5, and these sections will then leave a margin of




  
  
Your stuff here
Your stuff here
Your stuff here

5 at the top.




  
  
Your stuff here
Your stuff here
Your stuff here
nav {
  height: 3rem;
  position: sticky;
  top: 0;
}
section {
  scroll-margin-top: 4rem;
}

What's important to remember with these properties is that they both apply only to scroll-snapping, so they do not affect the actual padding of the HTML element or the defined margin between anchor sections.

Browser support for these solutions is great, as you can see here: scroll-margin-top, scroll-padding-top, so we can make use of them immediately with no ill-effects.

How do I stop a web page from scrolling to the top?

Scrolling of the webpage can also be disabled by using only the CSS using the overflow property. In this method, we set the height of the element for which the scroll is disabled to 100% such that it covers all the space of its parent container, and then we set the overflow property to hidden.nullHow to Disable Scrolling on a Webpage with HTML, CSS ... - Scalerwww.scaler.com › topics › css-disable-scrollnull

How do I turn off scroll to top in CSS?

To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.nullHow to Hide the Scrollbar in CSS - HubSpot Blogblog.hubspot.com › website › hide-scrollbar-cssnull

The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the tag will not be a hyperlink. Tip: You can use href="

top" or href="#" to link to the top of the current page!nullHTML a href Attribute - W3Schoolswww.w3schools.com › tags › att_a_hrefnull