2012-11-10 144 views
0

我有3个内容框,并且所有菜单都有锚链接到下面的内容框! (链接包含和动态)找到锚点并设置jquery活动

昨天我received help with the script ..它的工作非常好,当我有一个大的内容框。

在这里看到:http://jsfiddle.net/wv9EQ/7/

现在我有一个问题,当我有一个小的内容盒

在这里看到:http://jsfiddle.net/wv9EQ/8/

当我如箱2和3之间两个锚越来越活性。 任何想法来解决这个问题?我只是想只因为我是盒子的积极锚目前内部

我使用这个脚本:

$(function(){ 
    var sections = {}, 
     _height = $(window).height(), 
     i  = 0; 

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

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

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

    $('.head-nav-button').click(function() 
    { 
     $('a').removeClass('active'); 
     $('.nav_' + $(this).attr('href').replace('#', '')).addClass('active'); 
    }); 
}); 

回答

1

离开此功能设置部分活跃后:http://fiddle.jshell.net/doktormolle/nCasy/

+0

喜Dr.Molle ..有同样的问题时headline2和3两2-SP锚活跃..和你scrript问题之间的IM ..当我例如在head3上按下1-SP它跳到上面但设置2-SP活动 – bernte

+1

设置这个条件:sections [i]> pos'为:'sections [i]> = pos',因为相关的。当您使用链接时,节等于pos:http://fiddle.jshell.net/doktormolle/nCasy/9/ –

+0

您修复了错误!酷..但我有一个其他问题..我试图解释:当访问该网站的第一个锚点设置,因为该网站加载ontop(everthing罚款)当我开始滚动或按锚点3例如所有3个盒子活跃在锚点3.我只是想要在活动框(而不是全部3)的锚点滚动或点击..对不起,错误的 – bernte

1

使用偏移和break后的第一个启动菜单似乎工作:

琐碎的部分:

offset = 50; // Should be at least the height of your nav bar 

if(sections[i] - pos + offset > 0){ 
    $('a').removeClass('active'); 
    $('.nav_' + i).addClass('active'); 
    break;    
} 

在拨弄全部代码在这里:http://jsfiddle.net/wv9EQ/10/