最近有个需求是实现围绕旋转的css特效,但是网上找的大部分是写死的,没办法只改变内容,个数的情况下自动算出css参数,所以有了这个需求
效果图
<div>
<h1>CSS Swiper</h1>
<div v-for="item in imageList" :key="item.id" class="ball" :style="item.style">
<img :src="item.img" />
</div>
</div>
</template>
<script>
export default {
name: "cssSwiper",
data() {
return {
imageList: [ //图片列表
{
img: "https://picsum.photos/200/200?random=1",
style: {},
},
{
img: "https://picsum.photos/200/200?random=2",
style: {},
},
{
img: "https://picsum.photos/200/200?random=3",
style: {},
},
],
ballCount: 0,
aniSec: 20,//循环时长
};
},
mounted() {
this.ballCount = this.imageList.length;
this.imageList.forEach((element, index) => {
// 获取每个球的动画时间
let base = this.aniSec / this.ballCount;
// 获取每个球的动画延迟时间(这里要从动画的四分之一开始,要不然动画会有一个起始位置)
let delBase = this.aniSec / 4;
element.style = {
animation:
`animX ${this.aniSec / 2}s cubic-bezier(0.36, 0, 0.64, 1) -${
base * index + delBase
}s infinite alternate, ` +
`animY ${this.aniSec / 2}s cubic-bezier(0.36, 0, 0.64, 1) -${
base * index
}s infinite alternate, ` +
`scale ${this.aniSec}s cubic-bezier(0.36, 0, 0.64, 1) -${
base * index
}s infinite alternate`,
};
});
},
methods: {},
};
</script>
<style lang="scss">
@keyframes animX {
0% {
left: 0px;
}
100% {
left: 1000px;
}
}
@keyframes animY {
0% {
top: 0px;
}
100% {
top: 600px;
}
}
@keyframes scale {
0% {
transform: scale(0.7);
}
50% {
transform: scale(1);
}
100% {
transform: scale(0.7);
}
}
.ball {
width: 200px;
height: 200px;
background-color: #f66;
border-radius: 50%;
position: absolute;
color: #fff;
font-size: 22px;
display: flex;
align-items: center;
justify-content: center;
}
#lopp {
width: 1000px;
height: 600px;
border: 2px solid #999;
border-radius: 50%;
position: relative;
left: 50px;
top: 100px;
}
</style>
23/12/15更新:
49年入国军,google在Chrome 111 增加了对三角函数 sin()、cos()、tan()、asin()、acos()、atan() 和 atan2(),不需要复杂计算即可实现。