我正在尝试在es6类中使用jquery平滑滚动,但我在实现它时遇到了一些麻烦。我有一个导航菜单,我只是想点击时滚动到指定的位置。我相信问题是我的.each()循环,但我不确定。它有点工作,它似乎抓住了我设置的属性,但只有第一个并且不滚动。这是我的代码:
<nav class="primary-nav primary-nav--pull-right">
<ul>
<li><a href="#about">About Me</a></li>
<li><a href="#projects">Projects</a></li>
<li><a href="#github">Github</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
JS
import $ from 'jquery';
import smoothScroll from 'jquery-smooth-scroll';
class StickyHeader {
constructor(){
this.headerLinks = $("nav.primary-nav a");
this.events();
$.smoothScroll();
}
events() {
var that = this;
this.headerLinks.click(function() {
var currentLink = that.headerLinks;
currentLink.each(function() {
$.smoothScroll({
scrollElement: currentLink,
speed: 1000,
offset: -100,
scrollTarget: currentLink.attr('href')
});
console.log(that.headerLinks.attr('href'));
return false;
});
});
}
}
export default StickyHeader;