写一个按钮,按钮点击之后会消失,会出现一颗粉红色的爱心,3秒之后爱心裂开,2秒后消失,按钮重新显现
时间: 2023-10-07 08:06:52 浏览: 105
可以使用JavaScript和CSS来实现这个按钮的效果。
HTML代码:
```html
<button id="myButton">点击我</button>
<div id="heart"></div>
```
CSS代码:
```css
#heart {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
background: url("https://i.imgur.com/H9GjzjN.png") no-repeat;
background-size: contain;
animation: heart 3s ease-in-out;
}
@keyframes heart {
0% {
transform: translate(-50%, -50%) scale(0.8);
opacity: 0;
}
50% {
transform: translate(-50%, -50%) scale(1.2);
opacity: 1;
}
100% {
transform: translate(-50%, -50%) scale(1);
opacity: 0;
}
}
```
JavaScript代码:
```javascript
const button = document.getElementById("myButton");
const heart = document.getElementById("heart");
button.addEventListener("click", () => {
button.style.display = "none";
heart.style.display = "block";
setTimeout(() => {
heart.style.background = "url('https://i.imgur.com/7JbjJ4l.png') no-repeat";
heart.style.backgroundSize = "contain";
}, 3000);
setTimeout(() => {
heart.style.display = "none";
button.style.display = "block";
}, 5000);
});
```
当用户点击按钮时,按钮会隐藏,爱心图案会出现,并且开始一个3秒的动画效果,动画结束后,爱心图案会裂开,2秒后消失,然后按钮重新出现。
阅读全文