I am posting this because I did not find any solution to this question so far.
I have already implemented my own codes as the solution, but I want to know if there are any better way of doing this.
Scenario: There is a list of div tags that are enclosed in a parent div tag, which presents you with a scrollbar to scroll through the list of divs.
Objective: The objective is to highlight the div tag that is next/closest to the scrollbar at any point in time while scrolling.
Here is the HTML code for the scenario:
<div id="main">
<div id="container">
<div>Text 1</div>
<br/>
<div>Text 2</div>
<br/>
<div>Text 3</div>
<br/>
<div>Text 4</div>
<br/>
<div>Text 5</div>
<br/>
<div>Text 6</div>
<br/>
<div>Text 7</div>
<br/>
<div>Text 8</div>
<br/>
<div>Text 9</div>
<br/>
<div>Text 10</div>
<br/>
</div>
</div>
Ths CSS code:
#main {
width:100px;
height:100px;
overflow-y:auto;
}
Note that:
- when the scrollbar is at the top, the first div should be highlighted.
- when the scrollbar is in the middle, the div in the middle should be highlighted.
- when the scrollbar is at the bottom, the last div should be highlighted