Hacker News new | ask | show | jobs
by masklinn 2815 days ago
> I use it for linking to comments on my site. Users can copy a link to a particular comment, and the link directs people to the appropriate topic and location on the page.

That works out of the box, it's a native HTML/browser feature.

Why do you pass location.hash to jQuery?

2 comments

Sometimes, due to your in-page navigation or some other fixed position element, you need to actually scroll to a point before the actual element, so that it doesn’t get covered. The native behavior would not give it the lead space and you’d see some of the element’s content covered by a static element.

In scenarios where you can’t redesign the html structure or css to give the element more padding, you pretty much have to use javascript to get the window-x position of the element.

Oh, my mistake, it's been a while since I wrote the code. I use it to style the linked comment. So, the browser automatically scrolls to the appropriate location, and then jQuery adds a style to that comment so the user can easily locate it, or refind it if they scroll up and down the page.
In modern browsers, you can sometimes achieve this effect with `:target`. (https://developer.mozilla.org/en-US/docs/Web/CSS/:target)