问题
I am having issue with external anchor links with Chrome. On my website, I want to navigate back from a subpage to a specific section of the main page when clicking a link. I used a simple # anchor to achieve this like I always did in the past...It works fine in all browsers except for chrome. When clicking the link in chrome, I am redirected to the top of the main page. (But I can see the #anchor showing in the url bar) With all other browser this action works fine. Is this due to a bug with chrome? If so, can someone suggest a work around? Here are the codes I got. On main page: <div id="page-anchor"></div>
On subpage: <div id="go_back"><p ><a id="back" href="../index.html#page-anchor" target="_self">back to portfolio</a></p></div>
回答1:
I have the same issue. Try this workaround
var hash = window.location.hash;
if ($(hash).length) {
$('html, body').stop().animate({
scrollTop: $(hash).offset().top
}, 1000);
}
来源:https://stackoverflow.com/questions/42467363/external-anchor-link-doesnt-work-in-chrome