我想在滚动条位于元素旁边时更改其背景。我怎样才能做到这一点?我使用了'scroll'事件,但是没有用。
<div id="skill">This is Skill</div>
var about = document.getElementById('about');
about.addEventListener('scroll', function() {
about.style.background='green';
});
像这样,当我单击该部分时,背景会发生变化:
about.addEventListener('click', function() {
about.style.background='green'});
我从您的问题中猜测是,您想在页面滚动时添加背景。
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>