简体   繁体   English

向下滚动页面时突出显示导航链接

[英]Highlight navigation link as i scroll down the page

I have a simple page like the code below 我有一个简单的页面,如下面的代码

<nav>
    <a  href="#menu1">menu1</a>
    <a  href="#menu2">menu2</a>
    <a  href="#menu3">menu3</a>
    <a  href="#menu4">menu4 </a>
</nav>


<section id="menu1"></section>      
<section id="menu2"></section>          
<section id="menu3"></section>          
<section id="menu4"></section>

How can I highlight each navigation link when I change the section, as I scroll down the page? 向下滚动页面时,如何更改部分内容,以突出显示每个导航链接?

Is it possible to be done using css? 可以使用CSS完成吗?

Here is an example in fiddle 这是一个小提琴的例子

I need the grey background color to change when i change section... 更改部分时,我需要更改灰色背景颜色...

Concept broken down: 概念分解:

Get at what height each div is and assign it to a variable if first.y > current -> show first if first.y < current < second -> show second etc etc 取得每个div的高度并将其分配给变量,如果first.y> current-> first首先显示y <current <second-> show second等

here is some code to illustrate the example 这是一些代码来说明示例

$(document).scroll(function() {
    var scroll_top = $(document).scrollTop();
    var div_one_top = $('#one').position().top;
    var div_two_top = $('#two').position().top;

    if(scroll_top > div_one_top && scroll_top < div_two_top) {
        //You are now past div one
        $('#sidebar').text('One');
    } else if( scroll_top > div_two_top) {
        //You are now past div two
       $('#sidebar').text('Two');
    }
});

in your case a switch(y pos) might be better 在您的情况下,切换(y pos)可能更好

Not on scroll but solid solution, detects mouseenter and by that assigning active class to links. 不是滚动显示,而是可靠的解决方案,它检测到mouseenter并通过将活动类分配给链接来进行检测。

$('section').mouseenter(function() {
  $('nav a[href="#'+$(this).attr('id')+'"]').addClass('active').siblings('nav a').removeClass('active');
});

FIDDLE 小提琴

Update: Here's on scroll solution 更新:这是滚动解决方案

$(document).scroll(function() {
  $('nav a[href="#'+$('section:hover').attr('id')+'"]').addClass('active').siblings('nav a').removeClass('active');
});

A little javascript can do this : 一些JavaScript可以做到这一点:

<script>
$(function(){
    $('a').click(function(){
        $('a').removeClass('active');
        $(this).addClass('active');
    });
})
</script>

CHECK DEMO 检查演示

[UPDATED] [更新]

Just add the ids in every links: 只需在每个链接中添加ID:

    <nav>
        <a id="a" href="#menu1" class="active">menu1</a>
        <a id="b" href="#menu2">menu2</a>
        <a id="c" href="#menu3">menu3</a> 
        <a id="d" href="#menu4">menu4 </a>
    </nav>

How about that if you store the clicked element a#id and then add the class when page is being scroll. 如果存储单击元素a#id然后在滚动页面时添加类,该怎么办。 Check this - DEMO 检查一下- 演示

Here is the updated code - 这是更新的代码-

<script>
$(function(){
    var id;
    $(document).on('click','a',function(){
        id = $(this).attr('id');
    }).scroll(function(){
        $('a').removeClass('active');
        $("#"+id).addClass('active');        
    });
})
</script>

Partly working solution to get you started: http://jsfiddle.net/nYw35/4/ 使您入门的部分可行的解决方案: http : //jsfiddle.net/nYw35/4/

$(document).scroll(function () {

    var scroll_top = $(document).scrollTop();
    var one_top = $('#menu1').position().top;
    var two_top = $('#menu2').position().top;

    if (scroll_top > one_top && scroll_top < two_top) {
        $('a[href="#menu1"]').removeClass('active');
        $('a[href="#menu2"]').addClass('active');
    }

});

Give unique id to all menu link. 为所有菜单链接提供唯一的ID。 Call one function on click event of all that link and pass that id using this attribute to that function and add active class by using for link of that respective link using that id. 在所有链接的click事件上调用一个函数,并使用该属性将该ID传递给该函数,并通过使用该ID将相应链接的链接用于添加活动类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM