When i click any of the fixed buttons on the left pane (also fixed) the page scrolls to a certain section (each button corresponds to a section).How can I achieve the contrary:whenever I scroll to a section the corresponding button is highlighted?
Here is code:
HTML:
`<aside class="sidebar"><p id="sidebar-navigation-title" class="sidebar-section-title">Welcome</p><br>
<div id="sidebar-navigation-buttons" class="sidebar-navigation-buttons">
<button id="sidebar-navigation-to-contact" class="sidebar-section-button"
onclick="ScrollToContact()">Contact</button>
<button id="sidebar-navigation-to-projects" class="sidebar-section-button"
onclick="ScrollToProjects()">Projects</button>
<button id="sidebar-navigation-to-top" style="font-weight:bolder;"
class="sidebar-section-button-selected" onclick="ScrollToTop()">About</button>
</div><br></aside>
<div id="about-content-container">
</div>
<div id="project-content-container">
</div>
<div id="contact-content-container">
</div>`
CSS:
.sidebar
{
width:140px;
height:100%;
font-family:Rajdhani;
background-image: url("./sidebar_background.jpg");
color:white;
float:left;
position:fixed;
display: block;
}
#about-content-container
{
height:100%;
width:100%;
padding:1px;
background-color:#FCB9B5;
}
#projects-content-container
{
height:100%;
width:100%;
padding:1px;
background-color:#B5EECB;
}
#contact-content-container
{
height:100%;
width:100%;
padding:1px;
background-color:#1F3541;
}
/*sidebar text on top left*/
.sidebar-section-title
{
color:#A1DBF1;
font-size:25px;
left:25px;
position:absolute;
font-weight: bold;
}
/*-----------------------*/
/*sidebar 3 buttons container*/
.sidebar-navigation-buttons
{
height:100px;
width:100px;
position:absolute;
top:33.3%;
left:10px;
margin-left:12px;
rotate:90deg;
}
/*-----------------------*/
/*sidebar generic button*/
.sidebar-section-button
{
color:whitesmoke;
background:none;
border:none;
font-family:Rajdhani;
font-size:22.5px;
width:100px;
height:33px;
margin-bottom:3px;
display: inline-block;
transition: 0.3s;
}
.sidebar-section-button:hover
{
background-color: rgba(225, 239, 250, 0.6);
color:white;
font-weight: 200;
}
/*-----------------------*/
/*sidebar button corresponding to the section scrolled to*/
.sidebar-section-button-selected
{
color:whitesmoke;
background:none;
border:1px solid transparent;
font-family:Rajdhani;
font-size:22.5px;
width:100px;
height:33px;
margin-bottom:3px;
display: inline-block;
transition: 0.3s;
}
.sidebar-section-button-selected:hover
{
border:1px solid rgba(225, 239, 250, 0.6);
color:white;
margin-bottom:2.5px;
}
I’ve tried with scrollTop and IntersectionObserver but nothing works.From what I’ve tried it seems like window scroll event is not even happening.
2
Answers
You can use
getBoundingClientRect()
method and then calculate the distance from the top.P.S: I added 2 second time out so you can see that the
.highlighted
is being added only when the link is on the screenI have written all the necessary codes for those functionalities with proper comments. If you want to change the style of the link on scroll just write it in the
active
class and it will be applied. Feel free to ask questions.