单页网站上的复杂活动状态导航 [英] complex active state navigation on single page website

查看:83
本文介绍了单页网站上的复杂活动状态导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

<div class="logo-ribbon">
    <a href="#top"></a>
</div>

<nav id="nav">

    <ul>
        <li class="what">
            <a href="#what">what </a>
        </li>
        <li class="how">
            <a href="#how">how </a>
        </li>

        <li class="projects">
            <a href="#projects">projects</a>
        </li>
        <li class="faq">
            <a href="#faq">faq</a>
        </li>
        <li class="contact">
            <a href="#contact">contact</a>
        </li>
    </ul>

</nav>

JS:

$('nav li a').click(function(e) {
        e.preventDefault();
        $('a').removeClass('active-state-navigation');
        $(this).addClass('active-state-navigation');

        $('logo-ribbon a').click(function(){
        $('nav li').removeClass('active-state-navigation');
    });
});

我需要实现的目标如下:

what i need to achieve is the following:


  • 一旦你点击一个菜单项就是一个经典的活跃状态..而且这已经通过js的第一行实现了

  • a classic active state once you click on a menu item.. and this i already achieved with the first lines of js

我希望在页面滚动时也能触发活动状态。所以,如果我滚动,让我们说,#contact部分,活动状态更改为联系人菜单项

i would like the active state to be triggered also on page scroll. so that if i scroll, let's say, to the #contact section, the active state changed to the "contact" menu item

我也需要它,如果我点击.logo-ribbon a,有效状态导航将被移除到导航中的任何位置。

i also need that, if i click on ".logo-ribbon a", the "active-state-navigation" is removed anywhere it is in nav.

推荐答案

你的意思是卷轴?点击此处 http://jsfiddle.net/mekwall/up4nu/

You mean scrollspy? Check here http://jsfiddle.net/mekwall/up4nu/

// Cache selectors
var lastId,
    topMenu = $("#top-menu"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
  var href = $(this).attr("href"),
      offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
  $('html, body').stop().animate({ 
      scrollTop: offsetTop
  }, 300);
  e.preventDefault();
});

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";

   if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }                   
});

这篇关于单页网站上的复杂活动状态导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆