You can use an offset, something like:
$(document).ready(function (){
var altura = $("#inicio").offset().top;
var altura2 = $("#section2").offset().top;
$(window).scroll(function(){
if($(window).scrollTop() >= altura + 400) {
$(".boton1").css({"font-weight": "600"});
}else{
$(".boton1").css({"font-weight": "400"});
}
if($(window).scrollTop() >= altura2) {
$(".boton2").css({"font-weight": "600"});
$(".boton1").css({"font-weight": "400"});
}else{
$(".boton2").css({"font-weight": "400"});
}
});
$(".boton1").click(function(){
$("html, body").animate({scrollTop: altura},500);
});
$(".boton2").click(function(){
$("html, body").animate({scrollTop: altura2},500);
});
});
* {
padding: 0;
margin: 0;
font-family: sans-serif;
}
html, body {
width: 100%;
height: 700px;
}
.section {
height: 700px;
width: 100%;
}
nav {
position: fixed;
width: 100%;
height: 50px;
text-align: center;
}
nav ul {
list-style: none;
}
nav ul li {
display: inline-block;
}
nav ul li a {
display: block;
padding: 16px 20px;
text-decoration: none;
text-transform: uppercase;
color: rgb(170, 92, 122);
}
nav ul li a:hover {
font-weight: 600;
color: rgb(148, 61, 95);
}
#section2 {
background-color: lightgreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul>
<li><a class="boton1" href="#inicio">Botón uno</a></li>
<li><a class="boton2" href="#section2">Botón dos</a></li>
</ul>
</nav>
<section class="section" id="inicio">
</section>
<section class="section" id="section2">
</section>
Short explanation: when the document is ready - > ( $(document).ready();
) I apply a variable with the offset function - > ( var altura = $(".section").offset().top
) to determine the height in top
of the label or class in terms of the window, and then within the function ready()
I apply a function scroll()
in the window $(window)
and in I add my if
so that if the window has a scroll at the top height and is greater than or equal to the previous variable - > ( if($(window).scrollTop() >= altura){ aqui las funciones}
) I apply the necessary functions to animate, change, etc. HTML tags you want, in this case, I changed the CSS values of the tags. If you want to know more about the subject or do not understand well, I recommend you go deeper into the JQuery and study the functions offset()
and scroll()
.
As an additional data, you can add as many variables with the function offset()
as sections and buttons of the menu you have, so that within the function scroll()
you can add more if/else
. Also outside the function scroll()
added other functions so that when you click on the menu buttons, move with an animation and not simply appear at once in the section.