简体   繁体   English

jQuery AutoScroll不起作用

[英]Jquery AutoScroll Not Working

I have a one page design and want to have an auto scroll effect when a nav link is clicked. 我具有一页设计,并且想要在单击导航链接时具有自动滚动效果。 Not sure why the scrolling effect is not working. 不知道为什么滚动效果不起作用。 I added the alert function to see if $(sectionID).offset().top is returning a value and it is. 我添加了警报功能,以查看$(sectionID).offset().top是否正在返回一个值,并且它确实是。 So if someone can explain the problem it would be greatly appreciated. 因此,如果有人可以解释问题,将不胜感激。

jsfiddle: https://jsfiddle.net/8t881403/ jsfiddle: https ://jsfiddle.net/8t881403/

html: HTML:

<div class="sub-nav">
  <nav>
    <ul>
      <li><a href="#mission">Mission</a></li>
      <li><a href="#why-cs">Why CS</a></li>
      <li><a href="#learning-experience">Learning Experience</a></li>
      <li><a href="#spartan-success">Spartan Success</a></li>
    </ul>
  </nav>
</div>

jquery: jQuery的:

$(function() {

  autoScroll();

});

function autoScroll() {
    $('.sub-nav a').click(function (e) {
        e.preventDefault();
        var sectionID = $(this).attr('href');
        alert($(sectionID).offset().top);
        $('html body').animate({ scrollTop: $(sectionID).offset().top
        }, 1000)
    })
}
var div = $('.sub-nav');
setInterval(function(){
    var pos = div.scrollTop();
    div.scrollTop(pos + 2);
}, 200)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM