五个星星鼠标移入星星变亮
时间: 2023-09-12 19:08:19 浏览: 96
鼠标周围的星星
非常感谢您的提问!根据您的要求,以下是一个简单的五星鼠标移入变亮的代码示例:
HTML 代码:
```html
<div class="rating">
<span class="star" onmouseover="setRating(1)" onmouseout="resetRating()">★</span>
<span class="star" onmouseover="setRating(2)" onmouseout="resetRating()">★</span>
<span class="star" onmouseover="setRating(3)" onmouseout="resetRating()">★</span>
<span class="star" onmouseover="setRating(4)" onmouseout="resetRating()">★</span>
<span class="star" onmouseover="setRating(5)" onmouseout="resetRating()">★</span>
</div>
```
CSS 代码:
```css
.rating .star {
color: gray;
font-size: 24px;
cursor: pointer;
}
.rating .star.active,
.rating .star:hover {
color: gold;
}
```
JavaScript 代码:
```javascript
function setRating(rating) {
for (let i = 1; i <= rating; i++) {
document.querySelector(".rating .star:nth-child(" + i + ")").classList.add("active");
}
}
function resetRating() {
let stars = document.querySelectorAll(".rating .star");
for (let i = 0; i < stars.length; i++) {
stars[i].classList.remove("active");
}
}
```
此代码使用HTML、CSS和JavaScript实现了一个五星评分的功能。当鼠标移入星星时,星星颜色变为金色,表示该星星被选中,同时所有前面的星星也变为金色。当鼠标移出星星时,所有星星恢复为灰色。
阅读全文