我怎样才能修改它,让它滚动到类(class)而不是 ID?
JS:
// Scroll To # Links
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash;
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 700, 'swing', function () {
window.location.hash = target;
});
});
HTML:
<a href="#test">test link</a>
<div class="test" style="margin-top: 1000px;">
testing content
</div>
JSFIDDLE .
最佳答案
您可以使用获取的值获取类名,然后按类定位元素:
var target = this.hash;
var $target = $('.'+target.replace('#',''));
关于javascript - jQuery ScrollTo anchor 链接但类不是 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29770701/