javascript - 滚动时事件菜单不会改变

标签 javascript jquery html css

我已经创建了一个右侧菜单,但是当我向下滚动时,它并没有将事件类更改为下一个菜单,我已经多次使用这段代码,但这次我没有得到结果,

  $(window).scroll(function() {
    var scrollbarLocation = $(this).scrollTop();

    scrollLink.each(function() {

      var sectionOffset = $(this.hash).offset().top - 70;

      if ( sectionOffset <= scrollbarLocation ) {
       $('.icons').removeClass('iconactive');
       $(this).children('.icons').addClass('iconactive'); 

      }
    });
  });

DEMO

最佳答案

你需要定义scrollLink,你可以通过添加这样的函数在点击 anchor 链接时提供一些动画效果

 $(document).on('click', 'a[href^="#"]', function (event) {
    event.preventDefault();
    $('html, body').animate({
        scrollTop: $($.attr(this, 'href')).offset().top
     }, 1000);
 });

演示:http://jsfiddle.net/3xmop98u/

关于javascript - 滚动时事件菜单不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47035661/

相关文章:

html - 当滚动条可见时,固定宽度的 div 会破坏布局

javascript - 在移动设备上使用 Google Web Speech API

javascript - 消失的谷歌地图

javascript - 使用 jquery 在表中进行多列搜索(需要算法)?

jquery - 在标题中格式化 h1

javascript - 我们如何将验证应用于 WordPress 插件的任何字段

javascript - 未捕获的类型错误 : info1 is not a function at HTMLInputElement. onkeyup

javascript - 如何从 stub 函数获取返回值?

jquery - 拖动并旋转容器内的图像

html - 元素在 Safari 中无缘无故不可见