2017-03-03 73 views
2

你好我正在创建一个页面网站,所以我需要的,例如,如果section2出现在视口添加类活动链接与href =“section2”。
当滚动时将类添加到导航栏项目

我需要什么one page website

$(document).ready(function() { 
 
    $(".links a").click(function (e) { 
 
     if (this.getAttribute("href").charAt(0) == "#") { 
 
      e.preventDefault(); 
 
      $(this).addClass("active").siblings().removeClass("active"); 
 
      $("html, body").stop(); 
 
      $("html, body").animate({ 
 
       scrollTop: $($(this).attr("href")).offset().top 
 
      }, 1400) 
 
     } 
 
     else { 
 
      $($(this)).attr("target", "_blank") 
 
     } 
 
    }) 
 
})
.links{ 
 
    width:600px; 
 
    position:fixed; 
 
    top:0; 
 
    padding:20px; 
 
} 
 
.links a{ 
 
    display:inline-block; 
 
    padding:10px 20px; 
 
    border:1px solid #02e62a; 
 
    color:#02e62a; 
 
    text-decoration:none; 
 
} 
 
.links a:hover, .links a.active{ 
 
    color:#fe0101; 
 
    border-color:#fe0101; 
 
} 
 
.section{ 
 
    width:400px; 
 
    height:200px; 
 
    margin:300px auto 600px; 
 
    background-color:#0094ff; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="links"> 
 
     <a href="#home">Section 1</a> 
 
     <a href="#about">Section 2</a> 
 
     <a href="http://google.com" target="_blank">External Link</a> 
 
     <a href="#services">Section 3</a> 
 
     <a href="#contact">Section 4</a> 
 
    </div> 
 
    <div id="home" class="section"></div> 
 
    <div id="about" class="section"></div> 
 
    <div id="services" class="section"></div> 
 
    <div id="contact" class="section"></div>

注:请不要推荐我使用任何插件。

+0

旁注:你的外部链接错误:) – niceman

+0

为什么它是错误的:) –

+0

当你说“当section2出现在视口”你的意思是什么时候通过JavaScript动态添加的东西?因为'section2'是一个div,一个空的div,它已经出现,它的外观什么都没有:) – niceman

回答

3

试试这个,我认为它提供了你想要的解决方案。

$(document).on("scroll", function() { 
    $('div[id^="section"]').each(function() { 
    var id = $(this).attr("id"); 
    if (isScrolledIntoView("#" + id)) { 
     $('a[href="#'+id+'"]').addClass("active").siblings().removeClass("active"); 
    } 
    }) 
}) 

注意似乎错误一点,当你用鼠标滚轮在这里使用它,所以在正确的拉动滚动条测试。不知道为什么,但现在我试图解决它。

更新这个问题似乎是,片段窗口是如此之小,如果你运行在full page的例子,那么它工作得很好

$(document).ready(function() { 
 
    $(".links a").click(function(e) { 
 
    if (this.getAttribute("href").charAt(0) == "#") { 
 
     e.preventDefault(); 
 
     $(this).addClass("active").siblings().removeClass("active"); 
 
     $("html, body").stop(); 
 
     $("html, body").animate({ 
 
     scrollTop: $($(this).attr("href")).offset().top 
 
     }, 1400) 
 
    } else { 
 
     $($(this)).attr("target", "_blank") 
 
    } 
 
    }) 
 
}) 
 

 
$(document).on("scroll", function() { 
 
    $('div.section').each(function() { 
 
    var id = $(this).attr("id"); 
 
    if (isScrolledIntoView("#" + id)) { 
 
     $('a[href="#'+id+'"]').addClass("active").siblings().removeClass("active"); 
 
    } 
 
    }) 
 
}) 
 

 
function isScrolledIntoView(elem) { 
 
    var docViewTop = $(window).scrollTop(); 
 
    var docViewBottom = docViewTop + $(window).height(); 
 

 
    var elemTop = $(elem).offset().top; 
 
    var elemBottom = elemTop + $(elem).height(); 
 

 
    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); 
 
}
.links { 
 
    width: 600px; 
 
    position: fixed; 
 
    top: 0; 
 
    padding: 20px; 
 
} 
 

 
.links a { 
 
    display: inline-block; 
 
    padding: 10px 20px; 
 
    border: 1px solid #02e62a; 
 
    color: #02e62a; 
 
    text-decoration: none; 
 
} 
 

 
.links a:hover, 
 
.links a.active { 
 
    color: #fe0101; 
 
    border-color: #fe0101; 
 
} 
 

 
.section { 
 
    width: 400px; 
 
    height: 200px; 
 
    margin: 300px auto 600px; 
 
    background-color: #0094ff; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="links"> 
 
    <a href="#Home">Home</a> 
 
    <a href="#About">About</a> 
 
    <a href="http://google.com" target="_blank">External Link</a> 
 
    <a href="#Contact">Contact</a> 
 
    <a href="#Blog">Blog</a> 
 
</div> 
 
<div id="Home" class="section"></div> 
 
<div id="About" class="section"></div> 
 
<div id="Contact" class="section"></div> 
 
<div id="Blog" class="section"></div>

+0

谢谢,但请你能解释我的代码:) –

+1

它工作正常,当我输入代码片段编辑器,所以在你自己的代码中尝试一下,然后它可能会像它应该做的那样工作。我会马上尝试解决问题 –

+0

@MichaelNeas查看我的更新声明,点击“运行代码片段”,然后点击右上角的“整页”,应该可以找到 –