比较简单,就直接上代码了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
html,
.chest {
width: 80px;
height: 80px;
cursor: pointer;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.text {
position: absolute;
bottom: -20px;
color: #ffd3d3;
}
.heart {
position: absolute;
z-index: 2;
background: linear-gradient(-90deg, #f50a45 0%, #d5093c 10%);
animation: beat 0.7s ease infinite;
}
.heart.center {
background: linear-gradient(-45deg, #b80734 0%, #d5093c 10%);
}
.heart.top {
z-index: 3;
}
.side {
width: 35.2px;
height: 35.2px;
border-radius: 50%;
top: 16px;
}
.center {
width: 33.6px;
height: 33.6px;
bottom: 16px;
left: 23.2px;
}
.left {
left: 9.92px;
}
.right {
right: 9.92px;
}
@keyframes beat {
0% {
transform: scale(1) rotate(225deg);
box-shadow: 0 0 10px #d5093c;
}
50% {
transform: scale(1.1) rotate(225deg);
box-shadow: 0 0 10px #d5093c;
}
100% {
transform: scale(1) rotate(225deg);
box-shadow: 0 0 10px #d5093c;
}
}
</style>
</head>
<body>
<div class="chest" οnclick="moveDiv()">
<div class="heart left side top"></div>
<div class="heart center"></div>
<div class="heart right side"></div>
<div class="text">关注我们</div>
</div>
</body>
</html>
<script>
function moveDiv() {
var widthNumber = randomNum(10, document.documentElement.clientWidth);
var heightNumber = randomNum(
10,
document.documentElement.clientHeight - 200
);
// 获取 <div> 元素
var movingDiv = document.querySelector(".chest");
// 隐藏 <div> 元素
movingDiv.style.display = "none";
setTimeout(function () {
// 更新 <div> 元素的样式
movingDiv.style.display = "flex";
movingDiv.style.position = "absolute";
movingDiv.style.left = widthNumber + "px";
movingDiv.style.top = heightNumber + "px";
}, 200); // 模拟延时效果
}
//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}
</script>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.