external anchor link doesn't work in Chrome

↘锁芯ラ 提交于 2019-12-14 03:19:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!