简体   繁体   English

如何在水平网页的导航栏中突出显示?

[英]How can I make a highlight in the navigation bar of a horizontal webpage?

I can't seem to find any asked questions on this topic. 我似乎找不到关于此主题的任何询问的问题。 I made an horizontal webpage with a smoothscroll system. 我使用了平滑滚动系统制作了一个水平网页。 The only thing I'm missing is highlighting in the navigation bar so you can see where you are on the website. 我唯一缺少的是在导航栏中突出显示,以便您可以看到自己在网站上的位置。 Here is the largest part of the website and the thing I am trying to make work 这是网站的最大部分,也是我正在努力做的事情

http://jsfiddle.net/JZt3b/ http://jsfiddle.net/JZt3b/

$(function(){
    var sections = {},
        _width  = $(window).width(),
        i        = 0;

    // Grab positions of our sections 
    $('.section').each(function(){
        sections[this.name] = $(this).offset().left;
    });

    $(document).scroll(function(){
        var $this = $(this),
        pos   = $this.scrollLeft();

        for(i in sections){
            if(sections[i] >= pos && sections[i] <= pos + _width){
                $('a').removeClass('active');
                $('#nav_' + i).addClass('active');
            }  
        }
    });
});

I've got no idea how to make the menu highlight so you can see where you are. 我不知道如何突出显示菜单,以便您可以看到自己所在的位置。 Please help me with this problem, I've tried several demo's but I can't figure it out. 请帮助我解决这个问题,我已经尝试了几个演示,但是我无法弄清楚。

The script I am using in the head is: 我在头中使用的脚本是:

<script src=" http://s3.sitepoint.com/examples/sidescroll/jquery-1.5.1.min.js" type="text/javascript" charset="utf-8"></script> 
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
           $(".menubar a").bind("click",function(event){
               event.preventDefault();
               var target = $(this).attr("href");
               $("html, body").stop().animate({
                   scrollLeft: $(target).offset().left,
                   scrollTop: $(target).offset().top
               }, 1200);
           });
        });
    </script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

if you change sections to be array instead of object you can do something like this 如果将节更改为数组而不是对象,则可以执行以下操作

$(function(){
    var sections = [],//change to array []
        _width  = $(window).width();
    $('.section').each(function(){
        sections.push($(this).offset().left);//you can use push 
    });
    var $document=$(document);//you can create a variable outside the scroll event
    $document.scroll(function(){
        var pos   = $document.scrollLeft();
        $.each(sections,function(i,n){// loop for each section
            if(n >= pos && n <= pos + _width){
                $('a').removeClass('active');
                //change #nav_ for #nav_section 
                //add 1 to i as you start with nav_section1
                $('#nav_section' +(i+1)).addClass('active');
            }  
        });        

    });
});    

http://jsfiddle.net/JZt3b/2/ http://jsfiddle.net/JZt3b/2/

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

相关问题 如何使我的导航栏(具有透明背景)在视差滚动网页中浮动/重叠背景? - How can I make my navigation bar (which has transparent background) to float/overlap my background in a parallax-scrolling webpage? 如何使用水平滚动导航制作魔术线动画? - How can I make magicline animation with horizontal scrollable navigation? 如何制作仅用于水平滚动的粘性导航栏? - How to make sticky navigation bar only for horizontal scrolling? 如何使边框跟随网页顶部导航栏中的cursor - How to make border follow the cursor in the top navigation bar on a webpage 如何在RNRF中使自定义导航栏透明 - How can I make the custom navigation bar transparent in RNRF 我可以在网页上劫持垂直滚动动作并将其水平移动吗? - On a webpage can I hijack the vertical scrolling action and make it horizontal? 单击网页中的其他内容时如何关闭导航栏 - How to close navigation bar when I click on other contents in the webpage 如何隐藏水平栏,并使左右按钮水平滚动? - How can I make the horizontal bar hidden and make left and right buttons to scroll horizontally? 如何在水平窗口调整大小时缩小网页的边距? - How do I make the margins of a webpage shrink on horizontal window resize? 如何使用Selenium-Python突出显示网页上的元素? - How can I highlight element on a webpage using Selenium-Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM