I want to add a class to an HTML element when the user scrolls away from the top of the page and then remove that class when the user returns to the top of the page. I presume JavaScript is the best way of achieving this, but don't know how.
For example in the demo, the text class to be "red" when scrolled to the top and "blue" when anywhere but.
CSS
.red {
color:red;
}
.blue {
color:blue;
}
HTML
<br>
<br>
<br>
<br>
<br>
<p id="changeme" class="red">Text</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>