【问题标题】:jQuery page scroll not changing navbar href to active classjQuery页面滚动不会将导航栏href更改为活动类
【发布时间】:2017-02-27 08:05:20
【问题描述】:

我有一个单页网站。当我向下滚动时,我希望我的(固定)导航栏链接在到达该特定 div 的位置时将状态更改为活动。

我使用了jQuery,但它不起作用。这是我的代码:

// SMOOTH SCROLLING PAGES

$(document).ready(function () {
$(document).on("scroll", onScroll);

//smoothscroll
$('a[href^="#"]').on('click', function (e) {
    e.preventDefault();
    $(document).off("scroll");

    $('a').each(function () {
        $(this).removeClass('active');
    })
    $(this).addClass('active');

    var target = this.hash,
        menu = target;
    $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top+2
    }, 800, 'swing', function () {
        window.location.hash = target;
        $(document).on("scroll", onScroll);
    });
  });
});

function onScroll(event){
var scrollPos = $(document).scrollTop();
$('main-navigation a').each(function () {
    var currLink = $(this);
    var refElement = $(currLink.attr("href"));
    if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
        $('main-navigation ul li a').removeClass("active");
        currLink.addClass("active");
    }
    else{
        currLink.removeClass("active");
    }
  });
};

这是我的 HTML:

<nav id="main-navigation">
  <ul>
    <li class="active"><a href="#site-main">Home</a></li>
    <li><a href="#a">A</a></li>
    <li><a href="#b">B</a></li>
    <li><a href="#c">C</a></li>
    <li><a href="#d">D</a></li>
  </ul>
</nav>
<div id="a">DIV Alpha</div>
<div id="b">DIV Bravo</div>
<div id="c">DIV Charlie</div>
<div id="d">DIV Delta</div>

平滑滚动效果很好,但是当我从 div #d 向上滚动时,导航栏 li 的活动状态不会改变。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    在这里,我为你准备了小提琴,我希望它是你想要的 - http://jsfiddle.net/skyr9999/dpuvcj5w

    <div class="content">
        <div id="site-main">DIV Home</div>
        <div id="a">DIV Alpha</div>
        <div id="b">DIV Bravo</div>
        <div id="c">DIV Charlie</div>
        <div id="d">DIV Delta</div>
    </div>
    
    <nav id="main-navigation">
        <ul>
            <li><a class="menuitem" href="#site-main">Home</a></li>
            <li><a class="menuitem" href="#a">A</a></li>
            <li><a class="menuitem" href="#b">B</a></li>
            <li><a class="menuitem" href="#c">C</a></li>
            <li><a class="menuitem" href="#d">D</a></li>
        </ul>
    </nav>
    

    js

    $(document).ready(function () {
    
        $('a[href^="#site-main"]').addClass('active');
    
    //smoothscroll
        $('.menuitem').on('click', function (e) {
            e.preventDefault();
            //  $(document).off("scroll");
            var athis = this;
            var target = this.hash,
                    menu = target;
            $target = $(target);
            $('html, body').stop().animate({
                'scrollTop': $target.offset().top + 2
            }, 800, 'swing', function () {
                window.location.hash = target;
                $('.menuitem').removeClass('active');
                $(athis).addClass('active');
    
            });    
        });    
    
        $(window).scroll(function (event) {
            var scrollPos = $(document).scrollTop();
            if (scrollPos === 0)
            {
                $('a[href^="#site-main"]').addClass('active');
                return;
            }    
            $('.menuitem').each(function () {
                var currLink = $(this);
                var refElement = $(currLink.attr("href"));
                if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
                    $('.menuitem').removeClass("active");
                    currLink.addClass("active");
                } else {
                    currLink.removeClass("active");
                }
            });    
        })    
    });
    

    因为你没有包含任何 css,所以我即兴创作了一点,但看起来效果很好。

    css

    #a,#b,#c,#d,#site-main { height: 400px;}
    #main-navigation {
        position: fixed;
        top: 0px;
        right:10px;        
    }
    
    #main-navigation ul li {
      display: inline;
    }
    
    .active {
      background: #f00;
    }
    

    更新 1 我更新代码以匹配小提琴现在它会在您滚动到项目时更改菜单项选择

    【讨论】:

    • 感谢您的努力。我工作正常,但我缺少的(也在你的小提琴中)是当我(鼠标)向下滚动说 div D 导航栏链接不是类活动时。我需要两个:D
    • 我更新了我的小提琴jsfiddle.net/skyr9999/dpuvcj5w 不是它更新了 scrool 上的菜单项
    • 谢谢。会尽快检查的!!
    • 它工作得几乎完美。非常感谢瓦西里!!!我在我的网站上注意到的一件事是课堂活动状态来得有点晚。它需要提前大约 100px 更改。我在哪里可以解决这个问题?
    • 你能给我链接到你的网站,这样我就可以测试它,看看我能做什么?
    【解决方案2】:

    您必须使用列表项进行循环,并使用 # 表示法更正 id 选择器:

    $('#main-navigation li').each(function () { //<---correct id selector
        var currLink = $(this);
        var refElement = $(currLink.find('a').attr("href")); //<---find the anchor here
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('#main-navigation ul li').removeClass("active"); //<---and here
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
    

    【讨论】:

    • 你能把更正的代码给我吗?不确定您对 cme​​ts 的含义。提前致谢!
    • $('main-navigation a') 你有这个选择器,它是 nav 元素的 ID 属性。所以,必须在前面加上#
    【解决方案3】:

    你可以使用这个很酷的小插件来实现这个 https://github.com/davist11/jQuery-One-Page-Nav

    当您在特定的 div 或部分时,它将添加当前类 希望这个帮助:)

    【讨论】:

    • 不知何故它对我不起作用。当鼠标滚动到 DIV 时,导航栏链接不会激活“点击”该 DIV。
    【解决方案4】:

    替换

    $('a').each(function () { $(this).removeClass('active'); }) $(this).addClass('active');

    $('a').each(function () {
        $(this).parent().removeClass('active');
    })
    $(this).parent().addClass('active');
    

    完整代码:

    $('a[href^="#"]').on('click', function (e) {
    e.preventDefault();
    $(document).off("scroll");
    
    $('a').each(function () {
        $(this).parent().removeClass('active');
    })
    $(this).parent().addClass('active');
    
    var target = this.hash,
        menu = target;
    $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top+2
    }, 800, 'swing', function () {
        window.location.hash = target;
    });
    

    });

    不需要onScroll函数

    【讨论】:

    • 试过了,但不幸的是它不起作用。与其他人提出的答案相同的问题。鼠标滚动导航栏链接不会变为活动状态。
    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多