在滚动条上突出显示活动菜单项

2023-12-20

我正在使用 jQuery 进行单页导航。我想要的是当用户滚动时,突出显示的菜单将发生变化。我想出了以下代码:

HTML

<body class="index m-scene" id="main">

<!-- // Start HTML \\ -->
<header id="header" class="fixed">
    <div class="container_12">
        <h1 class="grid_4">
            <a href="#" class="logo backtotop">
                <img src="images/logo.png" alt="">
                <span>Maurice Draait</span>
            </a>
        </h1><!-- End h1.grid_4 -->

        <div class="grid_8">
            <a href="#" class="toggle-menu"><span>Menu</span></a>
            <nav id="primary-navwrapper" class="pushy-right">
                <ul id="listnav">
                    <li><a href="#" class="current">Home</a></li>
                    <li><a href="#aboutus">About us</a></li>
                    <li><a href="#events">Agenda</a></li>
                    <li><a href="#gallery">Galerij</a></li>
                    <li><a href="#organisation">Organisatie</a></li>
                    <li><a href="#contact">Contact</a></li>
                </ul><!-- End ul#listnav -->

            </nav><!-- End nav#primary-navwrapper .pushy-right -->

        </div><!-- End div.grid_8 -->

    <div class="clear"></div><!-- End div.clear -->
    </div><!-- End div.container_12 -->
</header><!-- End header#header .fixed -->

<div class="m-page scene_element scene_element--fadeinup">
<main class="container">
    <section id="hero">
        <figure>
            <figcaption>
                <div class="container_12">
                    <hgroup class="grid_12">
                        <h1>
                            Headline 1
                        </h1>

                        <h2>
Headline 2
                        </h2>

                        <h3>
                            Donec sed odio dui. Nulla vitae elit libero, a pharetra augue. 
                            <br>
                            Nullam id dolor id nibh ultricies vehicula ut id elit. 
                        </h3>
                    </hgroup><!-- End hgroup.grid_12 -->

                <div class="clear"></div><!-- End div.clear -->
                </div><!-- End div.container_12 center -->
            </figcaption>
        </figure><!-- End figure -->
    </section><!-- End section#hero -->


    <section id="aboutus" class="e-waypoint">
        <div class="container_12 center">
            <header class="grid_12 center">
                <h3>Maurice Draait</h3>
                <p>
                    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
                    <br>
                    eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, 
                    <br>
                    sunt in culpa qui officia deserunt mollit anim id est laborum. 
                    <br>
                    Sed ut perspiciatis unde omnis iste natus error sit. 
                </p>
            </header><!-- End header.grid_12 center -->

            <footer class="grid_12 center">
                <a href="#" title="Meer weten" class="button primary">Learn more about us &rarr;</a>
            </footer><!-- End footer.grid_12 center -->

        <div class="clear"></div><!-- End div.clear -->
        </div><!-- End div.container_12 center -->
    </section><!-- End section#aboutus .e-waypoint -->


    <section id="events" class="alt">
        <div class="container_12 center">
            <header class="grid_12 center">
                <h3>Agenda</h3>
                <p>
                    Donec quis semper magna. Vivamus pellentesque tempor tincidunt.
                    <br> 
                    Proin quis eros dolor. Donec sed venenatis enim. 
                </p>
            </header><!-- End header.grid_12 center -->

            <article class="grid_6 post-entry event">
                <a href="#" title="">
                <figure class="post-thumbnail">
                    <img src="images/event.png" alt="Placehold">

                    <figcaption>
                        <h4>22 november</h4>
                        <p>Maecenas sed enim iaculis.</p>
                    </figcaption><!-- End figcaption -->
                </figure><!-- End figure.post-thumbnail -->
                </a>
            </article><!-- End article.grid_6 post-entry event -->

            <article class="grid_6 post-entry event">
                <a href="#" title="">
                <figure class="post-thumbnail">
                    <img src="images/event.png" alt="Placehold">

                    <figcaption>
                        <h4>8 december</h4>
                        <p>Quisque hendrerit.</p>
                    </figcaption><!-- End figcaption -->
                </figure><!-- End figure.post-thumbnail -->
                </a>
            </article><!-- End article.grid_6 post-entry event -->

        <div class="clear"></div><!-- End div.clear -->
        </div><!-- End div.container_12 center -->
    </section><!-- End section#events .alt -->


    <section id="gallery" class="even">
        <div class="container_12">
            <header class="grid_12 center">
                <h3>Galerij</h3>
                <p>
                    Donec quis semper magna. Vivamus pellentesque tempor tincidunt.
                    <br> 
                    Proin quis eros dolor. Donec sed venenatis enim. 
                </p>
            </header><!-- End header.grid_12 center -->

            <div class="entry">
                <article class="grid_4 post-entry">
                    <a href="#" title="">
                    <figure class="post-thumbnail">
                        <img src="images/placehold.png" alt="Placehold">
                    </figure><!-- End figure.post-thumbnail -->

                    <header>
                        <h4>Maurice Draait Weer</h4>
                        <p>Donec quis semper magna. Vivamus pellentesque tempor tincidunt.</p>
                    </header><!-- End header -->

                    </a>
                </article><!-- End article.grid_4 post-entry -->

                <article class="grid_4 post-entry">
                    <a href="#" title="">
                    <figure class="post-thumbnail">
                        <img src="images/placehold.png" alt="Placehold">
                    </figure><!-- End figure.post-thumbnail -->

                    <header>
                        <h4>Maurice Draait Weer</h4>
                        <p>Donec quis semper magna. Vivamus pellentesque tempor tincidunt.</p>
                    </header><!-- End header -->

                    </a>
                </article><!-- End article.grid_4 post-entry -->

                <article class="grid_4 post-entry">
                    <a href="#" title="">
                    <figure class="post-thumbnail">
                        <img src="images/placehold.png" alt="Placehold">
                    </figure><!-- End figure.post-thumbnail -->

                    <header>
                        <h4>Maurice Draait Weer</h4>
                        <p>Donec quis semper magna. Vivamus pellentesque tempor tincidunt.</p>
                    </header><!-- End header -->

                    </a>
                </article><!-- End article.grid_4 post-entry -->

                <article class="grid_4 post-entry">
                    <a href="#" title="">
                    <figure class="post-thumbnail">
                        <img src="images/placehold.png" alt="Placehold">
                    </figure><!-- End figure.post-thumbnail -->

                    <header>
                        <h4>Maurice Draait Weer</h4>
                        <p>Donec quis semper magna. Vivamus pellentesque tempor tincidunt.</p>
                    </header><!-- End header -->

                    </a>
                </article><!-- End article.grid_4 post-entry -->

                <article class="grid_4 post-entry">
                    <a href="#" title="">
                    <figure class="post-thumbnail">
                        <img src="images/placehold.png" alt="Placehold">
                    </figure><!-- End figure.post-thumbnail -->

                    <header>
                        <h4>Maurice Draait Weer</h4>
                        <p>Donec quis semper magna. Vivamus pellentesque tempor tincidunt.</p>
                    </header><!-- End header -->

                    </a>
                </article><!-- End article.grid_4 post-entry -->

                <article class="grid_4 post-entry">
                    <a href="#" title="">
                    <figure class="post-thumbnail">
                        <img src="images/placehold.png" alt="Placehold">
                    </figure><!-- End figure.post-thumbnail -->

                    <header>
                        <h4>Maurice Draait Weer</h4>
                        <p>Donec quis semper magna. Vivamus pellentesque tempor tincidunt.</p>
                    </header><!-- End header -->

                    </a>
                </article><!-- End article.grid_4 post-entry -->
            </div><!-- End div.entry -->

        <div class="clear"></div><!-- End div.clear -->
        </div><!-- End div.container_12 -->
    </section><!-- End section#gallery .even -->


    <section id="organisation" class="alt">
        <div class="container_12 center">
            <header class="grid_12 center">
                <h3>Organisatie</h3>
                <p>
                    Donec quis semper magna. Vivamus pellentesque tempor tincidunt.
                    <br> 
                    Proin quis eros dolor. Donec sed venenatis enim. 
                </p>
            </header><!-- End header.grid_12 center -->

            <article class="grid_3 post-entry">
                <figure class="post-thumbnail avatar">
                    <img src="images/casperbiemans.png" alt="Placehold">

                    <figcaption>
                        <h4>Headline 4</h4>
                        <p>Donec quis semper magna.</p>
                    </figcaption><!-- End figcaption -->
                </figure><!-- End figure.post-thumbnail -->
            </article><!-- End article.grid_3 post-entry -->

            <article class="grid_3 post-entry">
                <figure class="post-thumbnail avatar">
                    <img src="images/casperbiemans.png" alt="Placehold">

                    <figcaption>
                        <h4>Headline 4</h4>
                        <p>Donec quis semper magna.</p>
                    </figcaption><!-- End figcaption -->
                </figure><!-- End figure.post-thumbnail -->
            </article><!-- End article.grid_3 post-entry -->

            <article class="grid_3 post-entry">
                <figure class="post-thumbnail avatar">
                    <img src="images/casperbiemans.png" alt="Placehold">

                    <figcaption>
                        <h4>Headline 4</h4>
                        <p>Donec quis semper magna.</p>
                    </figcaption><!-- End figcaption -->
                </figure><!-- End figure.post-thumbnail -->
            </article><!-- End article.grid_3 post-entry -->

            <article class="grid_3 post-entry">
                <figure class="post-thumbnailnail avatar">
                    <img src="images/casperbiemans.png" alt="Placehold">

                    <figcaption>
                        <h4>Headline 4</h4>
                        <p>Donec quis semper magna.</p>
                    </figcaption><!-- End figcaption -->
                </figure><!-- End figure.post-thumbnail -->
            </article><!-- End article.grid_3 post-entry -->

        <div class="clear"></div><!-- End div.clear -->
        </div><!-- End div.container_12 center -->
    </section><!-- End section#organisation .alt -->


    <figure class="parallax socialmedia">
        <figcaption>
            <div class="container_12">
                <header class="grid_12 center">
                    <h3>Like ons op Facebook</h3>
                    <p>
                        Maecenas sed enim iaculis, elementum enim quis, 
                        <br>
                        dignissim lectus. Quisque at pellentesque. 
                    </p>
                </header><!-- End header.grid_12 center -->

                <footer class="grid_12 center">
                    <a href="#" title="Volg ons online" class="button primary" id="buttonContact">Volg ons online &rarr;</a>
                </footer><!-- End footer.grid_12 center -->
            <div class="clear"></div><!-- End div.clear -->
            </div><!-- End div.container_12 -->
        </figcaption>
    </figure><!-- End figure.parallax social -->


    <section id="contact" class="even">
        <div class="container_12 center">
            <header class="grid_12 center">
                <h3>Contact</h3>

                <a href="contact.html" title="Meer weten" class="button primary">Get in touch &rarr;</a>
            </header><!-- End header.grid_12 center -->

        <div class="clear"></div><!-- End div.clear -->
        </div><!-- End div.container_12 center -->
    </section><!-- End section#contact .even -->
</main><!-- End main.container -->

<footer id="footer">
    <div class="container_12">
        <div class="grid_12 center">
            <ul class="list-of-links">
                <li><a href="#aboutus">About us</a></li>
                <li><a href="#galerij">Galerij</a></li>
                <li><a href="#organisatie">Organisatie</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul><!-- End ul#listnav -->
        </div><!-- End div.grid_12 center -->

    <div class="clear"></div><!-- End div.clear -->
    </div><!-- End div.container_12 -->

</footer><!-- End footer#footer -->
</div>
<!-- JavaScript -->
<script src="js/retina.js" type="text/javascript"></script>


</body>
</html>

JS

// Update current item class
function setActiveListElements(event){
    var windowPos = $(window).scrollTop();

    $('#primary-navwrapper li a[href^="#"]').each(function() { 
        var currentLink = $(this);
        var refElement = $(currentLink.attr("href"));

        if (refElement.position() <= windowPos && refElement.position().top + refElement.height() > windowPos) {
            $('#primary-navwrapper li a').removeClass("current");
            currentLink.addClass("current");
        }
        else{
            currentLink.removeClass("current");
        }
    });
}



// Update menu item on scroll
$(window).scroll(function() {
       // Call function
    setActiveListElements();
});

http://jsfiddle.net/8n06pvy9/6/ http://jsfiddle.net/8n06pvy9/6/

这段代码的问题是我收到了错误:Uncaught TypeError: Cannot read property 'top' of undefined.

我还没有找到解决办法。


答案如下:

    // The id of the section we want to go to.
    var anchorId = $(this).attr("href");

    // Our scroll target : the top position of the
    // section that has the id referenced by our href.
    if (anchorId.length > 1 && $(anchorId))
    {
     var target = $(anchorId).offset().top - offset;
    }
    else
    {
     var target = 0;
    }

当一个链接只有#没有锚。它指的是页面的顶部,但没有附加锚元素。所以达到 undefined 的顶部是不可能的。此代码现在检查是否href长度大于 1(例如:#test)并且页面上有一个具有实际 id 的元素。如果是则计算偏移量,如果没有则将偏移量设置为0。这意味着,返回到页面顶部。

http://jsfiddle.net/8n06pvy9/8/ http://jsfiddle.net/8n06pvy9/8/

实际上,同样类型的答案也适用于突出显示问题。它选择了带有home导航按钮,因为它未链接到部分元素。 jQuery 不返回有效的选择并且position()将失败。

function setActiveListElements(event){
    var windowPos = $(window).scrollTop();
    $('#primary-navwrapper li a[href^="#"]').each(function() { 

        var currentLink = $(this);
        if ($(currentLink.attr("href")).length > 0)
            {
            var refElement = $(currentLink.attr("href"));
            if (refElement.position().top <= windowPos && (refElement.position().top + refElement.height() + $("#primary-navwrapper").height() ) > windowPos) {
                $('#primary-navwrapper li a').removeClass("current");
                currentLink.addClass("current");
            }
            else{
                currentLink.removeClass("current");
            }
        }
    });
}

再次检查所选元素是否具有链接的部分元素,如果有则继续。

http://jsfiddle.net/8n06pvy9/9/ http://jsfiddle.net/8n06pvy9/9/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在滚动条上突出显示活动菜单项 的相关文章

随机推荐

  • 删除mysql表中的所有记录[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在尝试从表中删除所有记录 我的查
  • Django:测试页面是否已重定向到所需的网址

    在我的 Django 应用程序中 我有一个身份验证系统 因此 如果我不登录并尝试访问某些个人资料的个人信息 我会被重定向到登录页面 现在 我需要为此编写一个测试用例 我得到的浏览器响应是 GET myprofile data some id
  • 如何在 MySQL 数据库中存储 UTC ISO8601 日期?

    我有数千个以下格式的日期 2011 10 02T23 25 42Z 又名 UTC 格式的 ISO 8601 我应该使用什么 MySQL 数据类型在 MySQL 数据库中存储这样的 ISO8601 日期 例如 Datetime timesta
  • 如何在 Keycloak 中指定刷新令牌的生命周期

    Keycloak 刷新令牌的生命周期为 1800 秒 refresh expires in 1800 如何指定不同的过期时间 在 Keycloak 管理 UI 中 只能指定访问令牌的生命周期 正如评论中指出的 库巴西蒙诺夫斯基 https
  • 为什么要在mvvm中的viewmodel和view上实现一个接口

    我对 MVVM 模式还很陌生 所以请耐心等待 我见过 wpf mvvm prism 中的实现 其中所有视图都倾向于将 IView 作为最顶层的界面 然后 各个模块中的视图都有一个特定于视图的接口 如 IViewA IViewB 等 它们实现
  • 移动 CALayer(添加动画)

    好吧 我有一个 CALayerlayer我想用 CADisplaylink 移动它 喜欢 layer center CGPointMake layer center x 10 layer center y 10 但我不能用center or
  • 为任何方法创建 Func 或 Action(在 C# 中使用反射)

    我的应用程序根据设置动态加载 dll 来自数据库 文件 类和方法名称 为了方便 加速和减少反射的使用 我想要一个缓存 遵循以下想法 MethodInfo Invoke 没有什么表现性的 反射性能 创建委托 C 属性 https stacko
  • ASP.Net MVC:在现有项目上从“无身份验证”更改为“个人用户帐户”

    我正在学习有关 ASP Net MVC 的课程 并且达到了该课程依赖于标准 注册 和 登录 功能的程度 当您在创建项目时选择 个人用户帐户 时 Visual Studio 会自动构建这些功能 显然 较新版本的 Visual Studio 现
  • 如何在异步任务中实现此图像视图?

    我有一个 url 传递给一个活动 我试图全屏显示 url 中的图像 但是它会引发主网络线程异常 据我所知 我相信我必须将该方法放入异步任务中 但我似乎根本无法理解它 那么我该如何将此方法放入异步任务中呢 全屏图像视图 java public
  • PS1 环境变量在 mac 上不起作用

    我有一个脚本 不是我自己写的 它在我的命令提示符中显示 git 分支 svn 分支 有谁知道为什么这在 mac 上不起作用 它在linux下完美运行 From https github com xumingming dotfiles blo
  • Observable .catch 不是一个函数

    从 Unix 环境运行代码时 我遇到了这个非常烦人的错误 当我通过本地运行代码时 这工作正常ng serve 但是当我将代码部署到我的服务器时 此错误会停止所有程序执行 ERROR TypeError this http get catch
  • 如何防止logback/slf4j解析换行符

    我使用 logback 和 SLF4j 来登录我的应用程序 我有一个包含换行符的字符串 它是字符串值的一部分 但并不表示新行 当我打印字符串时 logback 会在新行中打印它 如何防止这种情况发生 Code String str geor
  • Cygwin/bash 别名中的空格?

    我正在尝试为 Cygwin bash shell 设置别名 以运行标准 Windows C Program Files 子目录中包含的程序 不幸的是 alias 命令确实不喜欢目录名称中的空格 而且我尝试过的任何方法似乎都不起作用 我正在尝
  • pymysql fetchall() 结果作为字典?

    有没有办法使用 pymysql 将 fetchall 的结果作为字典获取 PyMySQL 包括一个DictCursor 它做我认为你想要的 使用方法如下 import pymysql import pymysql cursors conne
  • 跨线程的 HTTPContext

    我需要为每个网络请求实例化一个单例对象 以便数据处理一次并在整个请求期间有效 我正在使用HttpContext Current Items在 HTTP 请求期间共享数据 一切都很好 直到我们需要单例对象实例跨多个线程 我想到的第一件事是将
  • C++,在堆上创建的对象与本地创建的对象 - 返回指针时

    这是来自的后续问题在 C 中安全 在 C 中不安全 简单返回指针 引用 https stackoverflow com questions 8456335 safe in c sharp not in c simple return of
  • 模拟继承的受保护方法

    我这里有我的问题的简化版本 A类有一个受保护的方法 B类继承了这个方法 public class A protected String getString some Code public class B extends A public
  • 是否有被认为“安全”的 GHC 扩展列表?

    有时 如果没有至少一种语言扩展 我想编写的一段代码是不合法的 当试图在研究论文中实现想法时尤其如此 这些论文往往使用论文撰写时可用的任何漂亮的 超级扩展的 GHC 版本 而没有明确实际需要哪些扩展 结果是我的 hs 文件顶部经常出现这样的内
  • 如何使用货币格式化散景 xaxis 刻度

    我正在使用 jupyter 笔记本制作子弹图来比较两个值 该图已渲染 但我想格式化 x 轴刻度以显示货币 这就是我到目前为止所拥有的 project Rocket targetspend 15000 spend2date 16600 dat
  • 在滚动条上突出显示活动菜单项

    我正在使用 jQuery 进行单页导航 我想要的是当用户滚动时 突出显示的菜单将发生变化 我想出了以下代码 HTML