【问题标题】:Change Navigation Active when Scrolling滚动时更改导航活动
【发布时间】:2017-04-24 03:44:53
【问题描述】:

我希望导航链接具有“活动”类并在滚动时进行更改。我仅在用户单击链接时才工作的代码。如果用户手动滚动页面,则活动类不会改变。有什么建议吗?

    $(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
        }, 700, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});


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



           <!-- navigation -->
  <nav>
   <ul>
     <li><a href="#bmi">text</a></li>
     <li><a href="#health">text</a></li>
     <li><a href="#home" class="active">text</a></li>
   </ul>
  </nav>
</header>


<section id="home">
</section>

<section id="health">
</section> 

<section id="bmi">
</section>

【问题讨论】:

  • 你能分享你的html吗?
  • 我添加了导航和部分 html

标签: jquery html css scroll


【解决方案1】:

我也用过这个功能。试试这个

var selector = 'nav a';
    $(window).scroll(function() {
        onScrollt(selector);
    });
    function onScrollt(selector) {
        var scrollPos = $(document).scrollTop();
        $(selector).each(function() {
            var currLink = $(this);
            var refElement = $(currLink.attr("href"));
            if (parseInt(refElement.position().top) <= scrollPos && parseInt(refElement.position().top) + parseInt(refElement.height()) > scrollPos) {
                $(selector).removeClass("active");
                currLink.addClass("active");
            } else {
                currLink.removeClass("active");
            }
        });
    }

【讨论】:

    【解决方案2】:

    我在这里创建了一个示例,试试这个,可能对你有帮助

    $(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
            }, 500, 'swing', function () {
                window.location.hash = target;
                $(document).on("scroll", onScroll);
            });
        });
    });
    
    function onScroll(event){
        var scrollPos = $(document).scrollTop();
        $('#menu-center a').each(function () {
            var currLink = $(this);
            var refElement = $(currLink.attr("href"));
            if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
                $('#menu-center ul li a').removeClass("active");
                currLink.addClass("active");
            }
            else{
                currLink.removeClass("active");
            }
        });
    }
    body, html {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
    }
    .menu {
        width: 100%;
        height: 75px;
        background-color: rgba(0, 0, 0, 1);
        position: fixed;
        background-color:rgba(4, 180, 49, 0.6);
        -webkit-transition: all 0.3s ease;
        -moz-transition: all 0.3s ease;
        -o-transition: all 0.3s ease;
        transition: all 0.3s ease;
    }
    .light-menu {
        width: 100%;
        height: 75px;
        background-color: rgba(255, 255, 255, 1);
        position: fixed;
        background-color:rgba(4, 180, 49, 0.6);
        -webkit-transition: all 0.3s ease;
        -moz-transition: all 0.3s ease;
        -o-transition: all 0.3s ease;
        transition: all 0.3s ease;
    }
    #menu-center {
        width: 980px;
        height: 75px;
        margin: 0 auto;
    }
    #menu-center ul {
        margin: 15px 0 0 0;
    }
    #menu-center ul li {
        list-style: none;
        margin: 0 30px 0 0;
        display: inline;
    }
    .active {
        font-family:'Droid Sans', serif;
        font-size: 14px;
        color: #fff;
        text-decoration: none;
        line-height: 50px;
    }
    a {
        font-family:'Droid Sans', serif;
        font-size: 14px;
        color: black;
        text-decoration: none;
        line-height: 50px;
    }
    #home {
        background-color: grey;
        height: 100%;
        width: 100%;
        overflow: hidden;
        background-image: url(images/home-bg2.png);
    }
    #portfolio {
        background-image: url(images/portfolio-bg.png);
        height: 100%;
        width: 100%;
    }
    #about {
        background-color: blue;
        height: 100%;
        width: 100%;
    }
    #contact {
        background-color: red;
        height: 100%;
        width: 100%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="m1 menu">
        <div id="menu-center">
            <ul>
                <li><a class="active" href="#home">Home</a>
    
                </li>
                <li><a href="#portfolio">Portfolio</a>
    
                </li>
                <li><a href="#about">About</a>
    
                </li>
                <li><a href="#contact">Contact</a>
    
                </li>
            </ul>
        </div>
    </div>
    <div id="home"></div>
    <div id="portfolio"></div>
    <div id="about"></div>
    <div id="contact"></div>

    【讨论】:

    • 我尝试使用您的 JS 代码,但我仍然无法在滚动时更改它。只有当我单击其中一个导航链接时,它才会发生变化。这应该与CSS无关吧?
    【解决方案3】:

    以下代码适用于滚动和点击。我已经使用你的代码来描述场景。

    下面是工作示例..

    $(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
        }, 500, 'swing', function() {
          window.location.hash = target;
          $(document).on("scroll", onScroll);
        });
      });
    });
    
    function onScroll(event) {
      var scrollPos = $(document).scrollTop();
      $('nav a').each(function() {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
          $('nav ul li a').removeClass("active");
          currLink.addClass("active");
        } else {
          currLink.removeClass("active");
        }
      });
    }
    body,
    html {
      height: 100%;
      width: 100%:
    }
    
    * {
      margin: 0px;
      padding: 0px;
      box-sizing: border-boz;
    }
    
    section {
      height: 100%;
      width: 100%;
    }
    
    nav ul {
      width: 100%;
      background: yellow;
      position: fixed;
    }
    
    nav ul li {
      display: inline-block;
      padding: 20px 0;
      margin-left: 20px;
      list-style: none;
    }
    
    nav ul li a {
      text-decoration: none;
      color: black;
    }
    
    nav ul li a.active {
      color: white
    }
    
    section {
      background: red;
    }
    
    section+section {
      background: green;
    }
    
    section+section+section {
      background: blue;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <header>
      <nav>
        <ul>
          <li><a href="#bmi">text</a></li>
          <li><a href="#health">text</a></li>
          <li><a href="#home" class="active">text</a></li>
        </ul>
      </nav>
    </header>
    
    
    <section id="home">
    </section>
    
    <section id="health">
    </section>
    
    <section id="bmi">
    </section>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2012-12-19
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多