鼠标悬浮后高亮是一种常见的用户界面交互效果,通常用于提升用户体验,使用户能够直观地看到鼠标当前所在的位置或元素。
以下是一个简单的JavaScript和CSS结合使用的示例,实现鼠标悬浮后高亮效果:
<div class="highlightable">悬停我试试</div>
.highlightable {
padding: 10px;
border: 1px solid #ccc;
transition: background-color 0.3s ease;
}
.highlightable:hover {
background-color: #f0f0f0;
}
如果你需要通过JavaScript来控制高亮效果,可以这样做:
document.querySelectorAll('.highlightable').forEach(item => {
item.addEventListener('mouseover', function() {
this.style.backgroundColor = '#f0f0f0';
});
item.addEventListener('mouseout', function() {
this.style.backgroundColor = '';
});
});
通过以上方法,你可以有效地实现并优化鼠标悬浮后的高亮效果,提升用户界面的交互体验。
领取专属 10元无门槛券
手把手带您无忧上云