javascript - jQuery 突出显示滚动上的导航链接不起作用

标签 javascript jquery html css

我对 JavaScript 非常陌生,所以我提前道歉。我正在尝试使用导航链接列表为学校元素创建一页 html 文档,当 anchor 滚动到该列表时,该链接列表会发生变化。我尝试了在 Jfiddle 上和通过 stackoverflow 找到的各种不同方法。这是我现在尝试的方法:http://jsfiddle.net/m2zQE/

 var topRange = 200, // measure from the top of the viewport to X pixels down
 edgeMargin = 20, // margin above the top or margin from the end of the page
 animationTime = 1200, // time in milliseconds
 contentTop = [];



$(document).ready(function () {

     // Stop animated scroll if the user does something
     $('html,body').bind('scroll mousedown DOMMouseScroll mousewheel keyup', function (e) {
         if (e.which > 0 || e.type == 'mousedown' || e.type == 'mousewheel') {
             $('html,body').stop();
         }
     });

     // Set up content an array of locations
     $('#nav').find('a').each(function () {
         contentTop.push($($(this).attr('href')).offset().top);
     });

     // Animate menu scroll to content
     $('#nav').find('a').click(function () {
         var sel = this,
             newTop = Math.min(contentTop[$('#nav a').index($(this))], $(document).height() - $(window).height()); // get content top or top position if at the document bottom
         $('html,body').stop().animate({
             'scrollTop': newTop
         }, animationTime, function () {
             window.location.hash = $(sel).attr('href');
         });
         return false;
     });

     // adjust side menu
     $(window).scroll(function () {
         var winTop = $(window).scrollTop(),
             bodyHt = $(document).height(),
             vpHt = $(window).height() + edgeMargin; // viewport height + margin
         $.each(contentTop, function (i, loc) {
             if ((loc > winTop - edgeMargin && (loc < winTop + topRange || (winTop + vpHt) >= bodyHt))) {
                 $('#nav li')
                     .removeClass('selected')
                     .eq(i).addClass('selected');
             }
         });
     });

     });

我仍然没有任何运气。我已经搜索过是否可以调试问题并尝试更改代码的顺序以及调用 jquery 的顺序。

这是该站点的链接:https://googledrive.com/host/0BwvPQbnPrz_LMlZDeGlFY2Yydmc/index.html

我使用 html5boilerplate 作为起点。提前致谢。

最佳答案

没有太多时间查看你的代码,但是当我输入行时

Math.min(contentTop[$('#nav a').index($(this))], $(document).height() - $(window).height())

进入开发者工具控制台,返回NaN。

所以我想问题是您没有正确设置 scrollTop。

我建议你给每个元素一个 id 并尝试:

$('html, body').animate({
    scrollTop: $("#elementID").offset().top
}, 2000);

或者如果你坚持不给id,

$('html, body').animate({
    scrollTop: $("#container-fulid:nth-child(2)").offset().top
}, 2000);

但请注意,这不适用于所有浏览器,因为第 n 个子选择器是 CSS3 选择器。

或者,如果您知道如何正确使用其他人的作品,您可以尝试使用 bootstrap 3.0,其中已经包含一个名为 scrollspy 的函数,它可以完成您正在做的事情。

http://getbootstrap.com/javascript/#scrollspy

关于javascript - jQuery 突出显示滚动上的导航链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20115636/

相关文章:

javascript - 即使必填字段值不正确或验证失败,为什么仍会提交此表单?

html - 3 个盒子正确堆叠所需的 CSS float 帮助

javascript - JQuery .on ('click' ) 在 DataTables 的第二页或超过 11 的行中不起作用

html - 如何隐藏jquery中最后一个div内容之后的下一个按钮

javascript - 弹出窗口不会关闭

javascript - 处理 docXTemplater 或 javascript 对象数组中的未定义值

javascript - 避免为动态内容制作一堆不同的弹出窗口

javascript - 如何居中图像,当我在当前窗口中打开它时

javascript - 需要在 HTML/JS 应用程序中灰显问题(以单选按钮作为答案)?

javascript - 为什么我的 next() 和 prev() 切换的文本输出不正确?