2017-05-26 48 views
-2

我想要更改菜單元素的顏色,同時滾動頁面以查看以下模板。我遵循了一些方法,但它不是動態的。 如何將其更改爲動態?如何在滾動時更改菜單元素的顏色?

$(document).ready(function(){ 
 

 
//scrolling animation 
 

 
\t $('.navigation a').on('click',function(){ 
 

 
\t \t var target = $(this); 
 
\t \t var element = target.attr('href'); 
 
\t \t 
 
\t \t $('.navigation a').removeClass('active'); 
 
\t \t target.addClass('active'); 
 

 
\t \t navHeight = $(".navigation").height(); 
 

 
    \t $("body, html").animate({ 
 
     \t scrollTop: $(element).offset().top + navHeight - 120 
 
    \t }, 800); 
 
\t }); 
 

 
\t //changing color of menu elements when scrolling 
 

 
\t $(window).scroll(function() { 
 

 
\t \t var scrollTop = $(document).scrollTop(); 
 

 
\t \t if(scrollTop < 1000) { 
 
\t \t \t $(".navigation a").removeClass('active'); 
 
\t \t \t $("a[title='About']").addClass('active'); 
 
\t \t } 
 
\t \t else if(scrollTop > 1001 && scrollTop < 2000) { 
 
\t \t \t $(".navigation a").removeClass('active'); 
 
\t \t \t $("a[title='Work']").addClass('active'); 
 
\t \t } 
 
\t \t else if(scrollTop > 2001 && scrollTop < 3000) { 
 
\t \t \t $(".navigation a").removeClass('active'); 
 
\t \t \t $("a[title='Clients']").addClass('active'); 
 
\t \t } 
 
\t \t else if(scrollTop > 3001 && scrollTop < 4200) { 
 
\t \t \t $(".navigation a").removeClass('active'); 
 
\t \t \t $("a[title='Blogs']").addClass('active'); 
 
\t \t } 
 
\t \t else if(scrollTop > 4200 && scrollTop < 4600) { 
 
\t \t \t $(".navigation a").removeClass('active'); 
 
\t \t \t $("a[title='Contact']").addClass('active'); 
 
\t \t } 
 
    }); 
 

 

 
});
* { 
 
\t list-style-type: none; 
 
\t padding: 0; 
 
\t margin: 0; 
 
} 
 

 
body { 
 
\t font-size: 16px; \t \t 
 
\t background: #eee; 
 
\t padding-top: 90px; 
 
\t font-family: 'Roboto',Arial, Helvetica, Sans-serif; 
 
\t overflow-x: hidden; 
 
} 
 

 
.navigation { 
 
\t top: 0; 
 
\t left: 0; 
 
\t padding: 0 10%; 
 
\t width: 100%; 
 
\t position: fixed; 
 
\t color: #fff; 
 
\t box-sizing: border-box; 
 
\t background: #363636; 
 
\t text-align: center; 
 
} 
 
.navigation a { 
 
\t color: inherit; 
 
\t margin: 35px 5px; 
 
\t line-height: 150%; \t 
 
\t padding: 0 5px 0 20px; 
 
\t display: inline-block; 
 
\t text-decoration: none; 
 
\t border-left: 1px solid #fff; 
 
} 
 

 
.navigation a.active { color: yellow;} 
 
.navigation a:first-child { border: 0; } 
 
.box { 
 
\t width: 100vw; 
 
\t height: 100vh; 
 
\t font-size: 14vw; 
 
\t padding-top: 100px; 
 
\t text-align: center; \t 
 
\t background: #4CD2DA; 
 
} 
 
.box:nth-child(2) { background: #59DAE2; } 
 
.box:nth-child(3) { background: #4372A6; } 
 
.box:nth-child(4) { background: #D8E0E3; } 
 
.box:nth-child(5) { background: #4B565A; }
<html> 
 
<head> 
 
\t <title>ScrollSpy</title> 
 
\t <link rel="stylesheet" type="text/css" href="scrollspy.css"> 
 
</head> 
 
<body> 
 
\t <nav class="navigation"> 
 
\t \t <a class="active" href="#about" title="About">About</a> 
 
\t \t <a href="#work" title="Work">Work</a> 
 
\t \t <a href="#clients" title="Clients">Clients</a> 
 
\t \t <a href="#blogs" title="Blogs">Blogs</a> 
 
\t \t <a href="#contact" title="Contact">Contact</a> 
 
\t </nav> 
 
\t 
 
\t <div id="container"> 
 
\t \t <div id="about" class="box"> 
 
\t \t \t About 
 

 
\t \t </div> 
 
\t \t <div id="work" class="box"> 
 
\t \t \t Work \t 
 
\t \t </div> 
 
\t \t <div id="clients" class="box"> 
 
\t \t \t Clients 
 
\t \t </div> 
 
\t \t <div id="blogs" class="box"> 
 
\t \t \t Blogs 
 
\t \t </div> 
 
\t \t <div id="contact" class="box"> 
 
\t \t \t Contact 
 
\t \t </div> 
 
\t </div> 
 

 
\t <script src="https://code.jquery.com/jquery-3.2.0.min.js"></script> 
 
\t <script type="text/javascript" src="scrollspy.js"></script> 
 
\t 
 

 
</body> 
 
</html>

copepen example here

回答

0

你只需要當滾動到達箱.box之一來計算,然後添加類主動到相應的菜單項。

見下面片段或jsFiddle

$(document).ready(function() { 
 

 
    //scrolling animation 
 

 
    $('.navigation a').on('click', function() { 
 

 
    var target = $(this); 
 
    var element = target.attr('href'); 
 

 
    $('.navigation a').removeClass('active'); 
 
    target.addClass('active'); 
 

 
    navHeight = $(".navigation").height(); 
 

 
    $("body, html").animate({ 
 
     scrollTop: $(element).offset().top + navHeight - 120 
 
    }, 800); 
 
    }); 
 

 
    //changing color of menu elements when scrolling 
 

 
    $(window).scroll(function() { 
 

 
    var scrollTop = $(document).scrollTop(); 
 

 
    $(".box").each(function() { 
 
     var id = $(this).attr("id"); 
 
     var height = $(this).outerHeight(); 
 
     var oTop = $(this).offset().top - 120; 
 
     if (scrollTop > oTop && scrollTop < oTop + height) { 
 
     $(".navigation a[href='#" + id + "']").addClass("active"); 
 

 
     } else { 
 
     $(".navigation a[href='#" + id + "']").removeClass("active"); 
 
     } 
 
    }); 
 

 

 
    }); 
 

 

 
});
* { 
 
    list-style-type: none; 
 
    padding: 0; 
 
    margin: 0; 
 
} 
 

 
body { 
 
    font-size: 16px; 
 
    background: #eee; 
 
    padding-top: 90px; 
 
    font-family: 'Roboto', Arial, Helvetica, Sans-serif; 
 
    overflow-x: hidden; 
 
} 
 

 
.navigation { 
 
    top: 0; 
 
    left: 0; 
 
    padding: 0 10%; 
 
    width: 100%; 
 
    position: fixed; 
 
    color: #fff; 
 
    box-sizing: border-box; 
 
    background: #363636; 
 
    text-align: center; 
 
} 
 

 
.navigation a { 
 
    color: inherit; 
 
    margin: 35px 5px; 
 
    line-height: 150%; 
 
    padding: 0 5px 0 20px; 
 
    display: inline-block; 
 
    text-decoration: none; 
 
    border-left: 1px solid #fff; 
 
} 
 

 
.navigation a.active { 
 
    color: yellow; 
 
} 
 

 
.navigation a:first-child { 
 
    border: 0; 
 
} 
 

 
.box { 
 
    width: 100vw; 
 
    height: 100vh; 
 
    font-size: 14vw; 
 
    padding-top: 100px; 
 
    text-align: center; 
 
    background: #4CD2DA; 
 
} 
 

 
.box:nth-child(2) { 
 
    background: #59DAE2; 
 
} 
 

 
.box:nth-child(3) { 
 
    background: #4372A6; 
 
} 
 

 
.box:nth-child(4) { 
 
    background: #D8E0E3; 
 
} 
 

 
.box:nth-child(5) { 
 
    background: #4B565A; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<nav class="navigation"> 
 
    <a class="active" href="#about" title="About">About</a> 
 
    <a href="#work" title="Work">Work</a> 
 
    <a href="#clients" title="Clients">Clients</a> 
 
    <a href="#blogs" title="Blogs">Blogs</a> 
 
    <a href="#contact" title="Contact">Contact</a> 
 
</nav> 
 

 
<div id="container"> 
 
    <div id="about" class="box"> 
 
    About 
 

 
    </div> 
 
    <div id="work" class="box"> 
 
    Work 
 
    </div> 
 
    <div id="clients" class="box"> 
 
    Clients 
 
    </div> 
 
    <div id="blogs" class="box"> 
 
    Blogs 
 
    </div> 
 
    <div id="contact" class="box"> 
 
    Contact 
 
    </div> 
 
</div>

相關問題