Reputation: 21
I want to change the background of an element when the scroll bar is beside that element. How can I do that? I used 'scroll' event, but it didn't work.
<div id="skill">This is Skill</div>
var about = document.getElementById('about');
about.addEventListener('scroll', function() {
about.style.background='green';
});
like this, when I click on that section the background changes:
about.addEventListener('click', function() {
about.style.background='green'});
Upvotes: 2
Views: 1908
Reputation: 3502
What I guess from your question is, you want to add background when page scrolled.
var about = document.getElementById('about');
window.addEventListener('scroll', function() {
about.style.background = 'green';
});
body{
height:800px;
}
<html>
<body>
<div id="about">This is Skill</div>
</body>
<html>
Upvotes: 1