如何在元素上使用滚动条事件

问题描述 投票:1回答:1

我想在滚动条位于元素旁边时更改其背景。我怎样才能做到这一点?我使用了'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'});
javascript html css dom events
1个回答
1
投票

我从您的问题中猜测是,您想在页面滚动时添加背景。

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>
© www.soinside.com 2019 - 2024. All rights reserved.