改变mousemove的背景
changing background on mousemove
我无法更改背景这是我的代码
const projectList = document.querySelector('.project-list');
projectList.addeventListener('mosemove', (e) => {
projectList.style.backgroundColor = "rgb(" + e.offsetX + "," + e.offsetY + "40)";
})
.project-list {
width: 100vw;
height: 100vh;
background: #f2f2f2;
}
<body>
<div class="project-list"></div>
</body>
您的活动名称有错字。它应该是 mousemove
而不是 mosemove
。您还忘记了 rgb
函数中 40
值前的逗号。
var projectList = document.querySelector('.project-list');
projectList.addEventListener('mousemove', e => {
projectList.style.backgroundColor = "rgb("+e.offsetX+","+e.offsetY+",40)";
});
对于快速测试,这应该有效。但是正如评论中已经提到的那样,这不会很好地工作,因为 offsetX
和 offsetY
将产生高于 255
的值。如果你想处理它,你可以使用余数运算符(例如,offsetX % 255
)。
我无法更改背景这是我的代码
const projectList = document.querySelector('.project-list');
projectList.addeventListener('mosemove', (e) => {
projectList.style.backgroundColor = "rgb(" + e.offsetX + "," + e.offsetY + "40)";
})
.project-list {
width: 100vw;
height: 100vh;
background: #f2f2f2;
}
<body>
<div class="project-list"></div>
</body>
您的活动名称有错字。它应该是 mousemove
而不是 mosemove
。您还忘记了 rgb
函数中 40
值前的逗号。
var projectList = document.querySelector('.project-list');
projectList.addEventListener('mousemove', e => {
projectList.style.backgroundColor = "rgb("+e.offsetX+","+e.offsetY+",40)";
});
对于快速测试,这应该有效。但是正如评论中已经提到的那样,这不会很好地工作,因为 offsetX
和 offsetY
将产生高于 255
的值。如果你想处理它,你可以使用余数运算符(例如,offsetX % 255
)。