本帖最后由 马黑黑 于 2024-11-11 23:23 编辑
梳理一下你的代码:
<style>
#papa{
position: relative;
width: 1286px;
height: 720px;
margin-left:-300px;
margin-top:140px;
border: 0px solid rgba(36, 201, 219,.95);
border-radius: 0px;
background:#000 url(https://pic.imgdb.cn/item/67162d12d29ded1a8c2f45cf.jpg)no-repeat center/cover;
overflow: hidden;
--state: running;
}
#mdiv {
top:12%;
left:85%;
cursor: pointer;
width:120px;
height: 120px;
animation:rot 10s linear infinite var(--state);
position: absolute;
filter:drop-shadow(0 0 1px #000);
z-index: 40;
}
#tp{
position: relative;
width: 1286px;
height: 720px;
overflow: hidden;
background:#1A192D;
margin-left:0px;
margin-top:0px;
animation: light 10s ease-in infinite;
}
#rain {
width: 3px;
height: 6px;
background: #8db0b4;
position: absolute;
top: 5px;
left: 0px;
border-radius: 3px;
animation: rain 8s linear infinite var(--state);
filter: blur(5px);
}
@keyframes rot {
to { transform: rotate(2turn);}
}
@keyframes light {
0% { opacity: 1; }
2% { opacity: 0; }
3% { opacity: 0.6; }
4% { opacity: 0.2; }
6% { opacity: 0.9; }
100% { opacity: 1; }
}
@keyframes rain {
to { top: 200vh; }
}
</style>
<div id="papa">
<div id='tp'><div id='rain'></div>
</div>
<div>
<img id="mdiv" src="https://pic.imgdb.cn/item/671d8801d29ded1a8cbb3fa7.png" alt="" />
<audio id="aud" src="https://s2.ananas.chaoxing.com/sv-w9/audio/2a/fd/7f/36208a1240aa7e16bede45b9a765cb96/audio.mp3" autoplay loop ></audio>
</div>
</div>
<script>
mdiv.onclick = () => aud.paused ? aud.play(): aud.pause();
mState = () => {
papa.style.setProperty('--state', aud.paused ? 'paused' : 'running');
};
aud.onplaying = aud.onpause = () => mState();
let raindrop = document.querySelector('#rain');
for (let i = 0; i< 500; i++) {
let newRaindrop = raindrop.cloneNode(true);
newRaindrop.style.left += Math.floor(Math.random() * 100) + 'vw';
newRaindrop.style.top = '-' + (Math.floor(Math.random() * 300) + 'vh');
newRaindrop.style.animationDuration = getRandom(2, 6) + 's';
papa.appendChild(newRaindrop);
}
function getRandom(min, max) {
const floatRandom = Math.random();
const random = Math.round((max - min) * floatRandom);
const randomWithinRange = random + min;
return randomWithinRange;
}
</script>
因为要控制多个 CSS动画,因此,可以将CSS动画交给 --state 变量统一管理,实现函数是 mState(),mState() 可以扩展,比如控制视频,你就加一个控制视频的语句。
|