Ich habe einige Schwierigkeiten damit, meinen Sticky-Menü-Objekten ihre eigene Hintergrundfarbe zuzuweisen, wenn ihr jeweiliger Anker gescrollt wird das Ansichtsfenster.jQuery - Eine andere Hintergrundfarbe für jedes Objekt, wenn es in einem sticky-Menü aktiv ist
JSfiedle hier: https://jsfiddle.net/ym88etuk/6/ - das funktioniert, wie ich es jetzt will, aber ich könnte immer noch Hilfe verwenden, um diese DRY freundlich zu machen.
HTML:
<ul class="sticky">
<a href=#part1 class="scroll">
<li>part1</li>
</a>
<a href=#part2 class="scroll">
<li>part2</li>
</a>
<a href=#part3 class="scroll">
<li>part3</li>
</a>
<a href=#part4 class="scroll">
<li>part4</li>
</a>
</ul>
<div class=content>
<div id=part1 class="section gray">
</div>
<div id=part2 class="section green">
</div>
<div id=part3 class="section gray">
</div>
<div id=part4 class="section green">
</div>
<div class=extraspace>
</div>
</div>
</div>
CSS:
.sticky{
position: fixed;
top:0px;
width:100%;
z-index:999;
background: white;
margin:0;
}
.content{
position:relative;
top: 28px;
width:100%;
}
ul{
list-style-type:none;
}
li{
float: left;
width 20%;
padding:2.5%;
}
.section{
height:300px;
}
.gray{
background-color:gray;
}
.green{
background-color:green;
}
.clearfix:after {
content: " ";
display: table;
clear: both;
}
.extraspace{
height: 400px;
}
jQuery/Javascript:
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('html, body').animate({
scrollTop:($(this.hash).offset().top) - 32}, 500);
});
});
$(window).scroll(function(){
// I've removed my work here, because it was very broken... and not pertinent to solving the issue.
}