粘性导航上的活动类

时间:2014-04-07 10:43:57

标签: javascript jquery

我的粘性导航:

<nav id="page-nav" class="page-nav">
    <ul>    
        <li><a href="#top" class="linky active">Overview</a></li>
        <li><a class="linky" href="#sub-our-drainage-solutions">Our drainage solutions</a></li>
        <li><a class="linky" href="#sub-cctv-drain-survey">CCTV drain survey</a></li>
        <li><a class="linky" href="#sub-wet-waste-disposal">Wet waste disposal</a></li>
        <li><a class="linky" href="#sub-blocked-drains">Blocked drains</a></li>
        <li><a class="linky" href="#sub-cess-pit-emptying">Cess pit emptying</a></li>
    </ul>
</nav>

<div id="sub-our-drainage-solutions">

</div>

<div id="sub-cctv-drain-survey">

</div>

等...

$(function() {
    //sticky scroll to nav
    //page nav
    var s = $("#page-nav"),
        //sectorContent = $("#sector-content"),
        pos = s.position(), 
        linkClicked = false,
        w = $(window).width();

      if(w > 800) {

        $(window).scroll(function() {
            var windowpos = $(window).scrollTop();
            if (windowpos >= pos.top) {
                s.addClass("stick");
            } else {
                s.removeClass("stick"); 
            }
        });

        $(window).scroll(function() {
            var y = $(this).scrollTop();
            $('.linky').each(function(event) {
                id = $(this).attr('href');
                if (y >= $(id).offset().top -30) {
                    if (!linkClicked) { 
                        $('.linky').not(this).removeClass('active');
                        $(this).addClass('active');
                    }
                }
            });

        });

        $("#page-nav li a").click(function(e) { 
            e.preventDefault();
            $("#page-nav li a").removeClass('active');
            $(this).addClass('active');
            linkClicked = true; 
            goToByScroll($(this).attr("href").replace("#", ""));           
        });

        function goToByScroll(id){
            $('html,body').animate({
                scrollTop: $("#"+id).offset().top -30},
                 'slow', function() {
                     linkClicked = false;
                });
        }

    }
});

导航在滚动时粘到窗口顶部,我还需要它在传递相关div id时更改锚点上的活动类。

滚动时出现以下错误:

Uncaught TypeError: Cannot read property 'top' of null

由于这部​​分代码:

$(window).scroll(function() {
            var y = $(this).scrollTop();
            $('.linky').each(function(event) {
                id = $(this).attr('href');
                if (y >= $(id).offset().top -30) {
                    if (!linkClicked) { 
                        $('.linky').not(this).removeClass('active');
                        $(this).addClass('active');
                    }
                }
            });

        });

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

不确定您的代码发生了什么,但是这个代码段对我来说非常适合在滚动和平滑滚动中更改活动类。

&#13;
&#13;
$(document).ready(function () {
    $(document).on("scroll", onScroll);
    
    //smoothscroll
    $('#nav-minor a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        $(document).off("scroll");
        
        $('#nav-minor a').each(function () {
            $(this).removeClass('active');
        })
        $(this).addClass('active');
      
        var target = this.hash,
            menu = target;
        var $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top+2
        }, 500, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});

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

    });
}
&#13;
&#13;
&#13;

然后只需要一个类=&#34;活跃&#34;

的基本CSS样式