七夏 发表于 2024-11-4 14:20:43

悬停星星动画的按钮

一个按钮悬停动画效果,当用户将鼠标悬停在按钮上时,会出现多个星星从按钮周围飞出的动画效果。

按钮的背景颜色和文本颜色也会在悬停时发生变化,增强了用户的交互体验。

![图片](https://www.3bbs.cn/index-diy/img.php?url=https://mmbiz.qpic.cn/sz_mmbiz_gif/g5MlHXG0iccjRGicatUZyhvdTUAxtnJHLEkCSLNEFDho9lnsxsZxqHGUrc3VCA1Hg56ibia4DIUP9JXibFia3a2FggEg/640?wx_fmt=gif&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1)

复制源代码到空白的**html**格式文件,在浏览中打开运行即可。

```
<!DOCTYPE html>
<html lang="en" >

<head>
<meta charset="UTF-8">
<title>STAR HOVER</title>
<style>
*{
box-sizing:border-box;
margin:0;
padding:0;
}
html,
body {
height:100vh;
display:flex;
align-items:center;
justify-content:center;
background-color: #212121;
}
button {
position: relative;
padding: 12px 35px;
background: #FFFFFF;
font-size: 17px;
font-weight: 500;
color: #000000;
border: 3px solid #0e3a3a;
border-radius: 8px;
box-shadow: 0 0 0 #fec1958c;
transition: all .3s ease-in-out;
}

.star-1 {
position: absolute;
top: 20%;
left: 20%;
width: 25px;
height: auto;
filter: drop-shadow(0 0 0 #fffdef);
z-index: -5;
transition: all 1s cubic-bezier(0.05, 0.83, 0.43, 0.96);
}

.star-2 {
position: absolute;
top: 45%;
left: 45%;
width: 15px;
height: auto;
filter: drop-shadow(0 0 0 #fffdef);
z-index: -5;
transition: all 1s cubic-bezier(0, 0.4, 0, 1.01);
}

.star-3 {
position: absolute;
top: 40%;
left: 40%;
width: 5px;
height: auto;
filter: drop-shadow(0 0 0 #fffdef);
z-index: -5;
transition: all 1s cubic-bezier(0, 0.4, 0, 1.01);
}

.star-4 {
position: absolute;
top: 20%;
left: 40%;
width: 8px;
height: auto;
filter: drop-shadow(0 0 0 #fffdef);
z-index: -5;
transition: all .8s cubic-bezier(0, 0.4, 0, 1.01);
}

.star-5 {
position: absolute;
top: 25%;
left: 45%;
width: 15px;
height: auto;
filter: drop-shadow(0 0 0 #fffdef);
z-index: -5;
transition: all .6s cubic-bezier(0, 0.4, 0, 1.01);
}

.star-6 {
position: absolute;
top: 5%;
left: 50%;
width: 5px;
height: auto;
filter: drop-shadow(0 0 0 #fffdef);
z-index: -5;
transition: all .8s ease;
}

button:hover {
background: #0e3a3a;
color: #FFFFFF;
box-shadow: 0 0 25px #0e3a3a;
    border: 3px solid #FFF;
}

button:hover .star-1 {
position: absolute;
top: -80%;
left: -30%;
width: 25px;
height: auto;
filter: drop-shadow(0 0 10px #fffdef);
z-index: 2;
}

button:hover .star-2 {
position: absolute;
top: -25%;
left: 10%;
width: 15px;
height: auto;
filter: drop-shadow(0 0 10px #fffdef);
z-index: 2;
}

button:hover .star-3 {
position: absolute;
top: 55%;
left: 25%;
width: 5px;
height: auto;
filter: drop-shadow(0 0 10px #fffdef);
z-index: 2;
}

button:hover .star-4 {
position: absolute;
top: 30%;
left: 80%;
width: 8px;
height: auto;
filter: drop-shadow(0 0 10px #fffdef);
z-index: 2;
}

button:hover .star-5 {
position: absolute;
top: 25%;
left: 115%;
width: 15px;
height: auto;
filter: drop-shadow(0 0 10px #fffdef);
z-index: 2;
}

button:hover .star-6 {
position: absolute;
top: 5%;
left: 60%;
width: 5px;
height: auto;
filter: drop-shadow(0 0 10px #fffdef);
z-index: 2;
}

.fil0 {
fill: #FFFDEF
}
</style>
</head>
<body translate="no">
<button>Button
<div class="star-1">
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 784.11 815.53" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><defs></defs><g id="Layer_x0020_1"><metadata id="CorelCorpID_0Corel-Layer"></metadata><path d="M392.05 0c-20.9,210.08 -184.06,378.41 -392.05,407.78 207.96,29.37 371.12,197.68 392.05,407.74 20.93,-210.06 184.09,-378.37 392.05,-407.74 -207.98,-29.38 -371.16,-197.69 -392.06,-407.78z" class="fil0"></path></g></svg>
</div>
<div class="star-2">
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 784.11 815.53" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><defs></defs><g id="Layer_x0020_1"><metadata id="CorelCorpID_0Corel-Layer"></metadata><path d="M392.05 0c-20.9,210.08 -184.06,378.41 -392.05,407.78 207.96,29.37 371.12,197.68 392.05,407.74 20.93,-210.06 184.09,-378.37 392.05,-407.74 -207.98,-29.38 -371.16,-197.69 -392.06,-407.78z" class="fil0"></path></g></svg>
</div>
<div class="star-3">
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 784.11 815.53" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><defs></defs><g id="Layer_x0020_1"><metadata id="CorelCorpID_0Corel-Layer"></metadata><path d="M392.05 0c-20.9,210.08 -184.06,378.41 -392.05,407.78 207.96,29.37 371.12,197.68 392.05,407.74 20.93,-210.06 184.09,-378.37 392.05,-407.74 -207.98,-29.38 -371.16,-197.69 -392.06,-407.78z" class="fil0"></path></g></svg>
</div>
<div class="star-4">
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 784.11 815.53" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><defs></defs><g id="Layer_x0020_1"><metadata id="CorelCorpID_0Corel-Layer"></metadata><path d="M392.05 0c-20.9,210.08 -184.06,378.41 -392.05,407.78 207.96,29.37 371.12,197.68 392.05,407.74 20.93,-210.06 184.09,-378.37 392.05,-407.74 -207.98,-29.38 -371.16,-197.69 -392.06,-407.78z" class="fil0"></path></g></svg>
</div>
<div class="star-5">
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 784.11 815.53" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><defs></defs><g id="Layer_x0020_1"><metadata id="CorelCorpID_0Corel-Layer"></metadata><path d="M392.05 0c-20.9,210.08 -184.06,378.41 -392.05,407.78 207.96,29.37 371.12,197.68 392.05,407.74 20.93,-210.06 184.09,-378.37 392.05,-407.74 -207.98,-29.38 -371.16,-197.69 -392.06,-407.78z" class="fil0"></path></g></svg>
</div>
<div class="star-6">
    <svg xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 784.11 815.53" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><defs></defs><g id="Layer_x0020_1"><metadata id="CorelCorpID_0Corel-Layer"></metadata><path d="M392.05 0c-20.9,210.08 -184.06,378.41 -392.05,407.78 207.96,29.37 371.12,197.68 392.05,407.74 20.93,-210.06 184.09,-378.37 392.05,-407.74 -207.98,-29.38 -371.16,-197.69 -392.06,-407.78z" class="fil0"></path></g></svg>
</div>
</button>
</body>

</html>
```

### 核心实现原理

1. **HTML 结构**:

   * 使用一个 `<button>` 元素作为主要的交互元素,内部包含多个星星的 SVG 图形。
2. **CSS 样式**:

   * 使用 CSS 进行布局和样式设置,包括按钮的外观、星星的位置、大小和过渡效果。
   * 使用 `position: absolute` 将星星定位在按钮的周围,并通过 `z-index` 控制它们的层级关系。
3. **悬停效果**:

   * 使用 `:hover` 伪类来定义按钮在鼠标悬停时的样式变化,包括背景颜色、文本颜色和阴影效果。
   * 星星的动画效果通过 CSS 的 `transition` 属性实现,使得星星在悬停时平滑地移动到新的位置。
4. **SVG 动画**:

   * 每个星星都是一个 SVG 图形,使用 `filter: drop-shadow` 创建阴影效果,使星星看起来更加立体。
   * 星星在悬停时的位置会发生变化,模拟出飞出的效果。

### 用途

1. **用户界面交互**:

   * 适用于网站或应用程序中的按钮,增强用户的交互体验,使得操作更加生动有趣。
2. **游戏界面**:

   * 可以用作游戏中的按钮或交互元素,增加游戏的趣味性和视觉吸引力。
3. **庆祝活动**:

   * 在特定的庆祝活动或节日主题的网页中使用,增加节日氛围。
4. **产品展示**:

   * 在电商网站中,可以用作产品的“喜欢”或“添加到购物车”按钮,吸引用户的注意。
5. **社交媒体分享**:

   * 在社交媒体平台上使用,鼓励用户分享内容,增加互动性。
页: [1]
查看完整版本: 悬停星星动画的按钮