微信小程序官方的效果主要是通过canvas写的,实现起来可能不会像css动画这么繁琐,而且看起来会更加真实。
但是我就是想用css实现~~😏
知识点:
1.sass nth 数组下标获取
$colors:red blue pink;
...
.item{
color:#{nth($color,1)}; //color:red;
}
2.sass @for样式遍历简写方式
在线代码
代码片段
<template>
<div class="body">
<div class="container">
<img class="lid" src="https://res.wx.qq.com/mpres/htmledition/images/wxopen/wap_mockup@2.png" alt="" />
<img class="trunk" src="https://res.wx.qq.com/mpres/htmledition/images/wxopen/wap_mockup_top@2.png" alt="" />
<div class="bubble-box">
<div class="bubble" v-for="b in 100" :key="b"></div>
</div>
</div>
</div>
</template>
<script>
export default {};
</script>
<style lang="scss" scoped>
.body {
width: 100%;
height: 100vh;
display: grid;
place-content: end center;
overflow: hidden;
background: #fefefe;
}
.container {
width: 500px;
height: 342px;
position: relative;
img {
width: 100%;
position: absolute;
}
.lid {
z-index: 1;
}
.trunk {
z-index: 99;
}
.bubble-box {
background: red;
position: absolute;
top: 220px;
width: 72%;
left: 18%;
}
.bubble {
width: 50px;
height: 50px;
z-index: 2;
border-radius: 50%;
position: absolute;
}
}
$bubbleColors: #6262ea #ffffff #eddc46 #7bed46;
@for $i from 0 to 100 {
.bubble:nth-child(#{$i}) {
$background: nth($bubbleColors, random(4));
$width: random(70) + 30 + px;
left: #{(random(72)) + "%"};
width: $width;
height: $width;
animation: moveToTop#{$i} 10s linear #{random(60)}s infinite;
background: #{$background + "b4"};
border: 2px solid $background;
}
@keyframes moveToTop#{$i} {
100% {
transform: translate(random(400)- 200 + px, -100vh);
}
}
}
</style>