vue的列表加载过渡动画
- 效果实现的方法就是sass for loop方法(参考文章:http://thesassway.com/intermediate/if-for-each-while)
只要的重点就是 sass的写法
重点代码:
html : :class="`animation-${index+1}`"
css :
$grid-columns: 10;
@for $i from 1 through $grid-columns {
$time: ($i * 100+200) + ms;
.animation-#{$i} {
animation-delay: $time;
}
}
完整代码如下:
<template>
<div class="content">
<div v-for="(item,index) in new Array(10)" :key="index" class="box" :class="`animation-${index+1}`"> </div>
</div>
</template>
<script>
export default {
name: "listLoadAnimation",
components: {},
methods: {}
};
</script>
<style lang="scss" scoped>
$grid-columns: 10;
.content {
padding: 0.3rem;
.box {
width: 100%;
height: 2rem;
background: red;
margin-bottom: 0.3rem;
box-shadow: 0 0 0.2rem rgba($color: #000000, $alpha: 1);
}
@for $i from 1 through $grid-columns {
$time: ($i * 100+200) + ms;
.animation-#{$i} {
transition: all 0.5s linear;
animation-name: toTop; // toBottom
animation-duration: 0.5s; // 注释掉 会没有动画 就是帕帕一帧一帧的出来
animation-fill-mode: both;
animation-delay: $time;
}
}
}
// 方向 下-->上
@keyframes toTop {
0% {
opacity: 0;
transform: translate3d(0, 100%, 0);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
// 方向 上-->下
@keyframes toBottom {
0% {
opacity: 0;
transform: translate3d(0, -100%, 0);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
</style>
- 这里再次介绍一个 Sass if else 的用法(针对上述的代码修改)
@for $i from 1 through $grid-columns {
$time: ($i * 100+200) + ms;
.animation-#{$i} {
transition: all 0.5s linear;
animation-duration: 0.5s;
animation-fill-mode: both;
animation-delay: $time;
@if $i%2==0 {
animation-name: toLeft;
} @else {
animation-name: toRight;
}
}
}
css:
// 方向 右-->左
@keyframes toLeft {
0% {
opacity: 0;
transform: translate3d(-30px, 0, 0);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
// 方向 左-->右
@keyframes toRight {
0% {
opacity: 0;
transform: translate3d(30px, 0, 0);
}
100% {
opacity: 1;
transform: translate3d(0, 0, 0);
}
}
效果图:
- @while的简单用法:
$types: 4
$type-width: 20px
@while $types > 0
.while-#{$types}
width: $type-width + $types
$types: $types - 1
转化成以下代码
.while-4 {
width: 24px;
}
.while-3 {
width: 23px;
}
.while-2 {
width: 22px;
}
.while-1 {
width: 21px;
}