In this code below, I can make the item clicked change background-color
, but I would like it to change without clicking, ie when I was passing through the corresponding <section>
.
I know how to make it change with a certain event
scrollTop
, however I do not want to set a fixed size of each roll, but in a way that regardless of the content and size of the<section>
change thebackground-color
of the menu item.
How to do it?
$('.menu li').click(function(){
$('.menu li').css('background-color', 'green');
$(this).css('background-color', 'white');
})
* { margin: 0; padding: 0; }
.menu { text-align: center; position: fixed; width: 100%; }
.menu ul { list-style: none; display: inline; }
.menu li { display: inline-block; }
.menu a { text-decoration: none; color: #000; padding: 10px; display: inline-block; }
div { height: 300px; }
.blue { background-color: blue; }
.red { background-color: red; }
.orange { background-color: orange; }
.grey { background-color: grey; }
.green { background-color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><navclass="menu green">
<ul>
<li><a href="#home">HOME</a></li>
<li><a href="#work">WORK</a></li>
<li><a href="#contact">CONTACT</a></li>
</ul>
</nav>
<section id="home">
<div class="orange"></div>
<div class="blue"></div>
<div class="grey"></div>
</section>
<section id="work">
<div class="blue"></div>
<div class="orange"></div>
<div class="grey"></div>
<div class="orange"></div>
<div class="grey"></div>
</section>
<section id="contact">
<div class="grey"></div>
<div class="orange"></div>
<div class="blue"></div>
<div class="blue"></div>
<div class="orange"></div>
<div class="grey"></div>
</section>