在Chrome和Safari中,点击指向命名锚点(<a href="#like-this">Hash Link<a>
)的链接仅在第一次有效。后续单击不会捕捉到引用的元素。这在Firefox中可以正常工作。我找到了一个看似相关的chromium bug,但没有提供解决方法,似乎没有修复。你知道这个问题的解决方法吗?
答案 0 :(得分:1)
另一种方法是捕获锚点击次数,找到目标坐标并使用window.scrollTo(x,y)
。
有些代码out there会为您执行此操作,如下所示:
HTML:
<a href="#services">Jump to services</a>
<div id="services">
</div>
JS:
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
答案 1 :(得分:0)
如果这个问题有非js解决方案会很好,但这似乎不太可能。
我正准备使用javascript:
location.hash = '#actual-destination-invalid-to-fix-webkit'
location.hash = '#actual-destination'
但我认为Diodeus提供的答案不那么简单,而且不太复杂。