简单实现在Vue中使用Stylus编写loading特效
效果
实现
先定义一些基础的样式
创建一些容器与8个重叠的白色线条
<template lang="html">
<div class="example-box">
<div class="loading-box">
<div class="bar-box">
<div
v-for="n in 8"
:key="n"
class="bar"
/>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="stylus" scoped>
.example-box
width 200px
height 200px
background-color blue
position relative
.loading-box
position absolute
top 0
left 0
right 0
bottom 0
background-color rgba(0, 0, 0, .7)
.bar-box
position: absolute
top 50%
left 50%
width 20px
.bar
position absolute
left 0
top 0
background-color rgba(255, 255, 255, 1)
height 4px
width 20px
border-top-right-radius 2px
border-bottom-right-radius 2px
transform-origin: left
</style>
vue
接下来结合v-for指令,我们将每个.bar旋转一定的角度
使用v-for的遍历数字特性,使得角度动态计算绑定为style属性
<template lang="html">
<div class="example-box">
<div class="loading-box">
<div class="bar-box">
<div
v-for="n in 8"
:key="n"
class="bar"
:style="{
transform: `rotate(${n * 360 / 8}deg)`
}"
/>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="stylus" scoped>
.example-box
width 200px
height 200px
background-color blue
position relative
.loading-box
position absolute
top 0
left 0
right 0
bottom 0
background-color rgba(0, 0, 0, .7)
.bar-box
position: absolute
top 50%
left 50%
.bar
position absolute
left 0
top 0
background-color rgba(255, 255, 255, 1)
height 4px
width 20px
border-top-right-radius 2px
border-bottom-right-radius 2px
transform-origin: left
</style>
vue
使用background: linear-gradient属性使中间空出一个占比39.2%的黄金占比透明区域
background: linear-gradient,可以创建渐变背景,也可以创建双色、多色背景
<template lang="html">
<div class="example-box">
<div class="loading-box">
<div class="bar-box">
<div
v-for="n in 8"
:key="n"
class="bar"
:style="{
transform: `rotate(${n * 360 / 8}deg)`
}"
/>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="stylus" scoped>
.example-box
width 200px
height 200px
background-color blue
position relative
.loading-box
position absolute
top 0
left 0
right 0
bottom 0
background-color rgba(0, 0, 0, .7)
.bar-box
position: absolute
top 50%
left 50%
.bar
position absolute
left 0
top 0
height 4px
width 20px
border-top-right-radius 2px
border-bottom-right-radius 2px
transform-origin: left
background: linear-gradient(to right, transparent 38.2%, #fff 38.2%)
</style>
vue
使用@keyframes定义关键帧动画,通过设置opacity属性实现消失、出现动画
<template lang="html">
<div class="example-box">
<div class="loading-box">
<div class="bar-box">
<div
v-for="n in 8"
:key="n"
class="bar"
:style="{
transform: `rotate(${n * 360 / 8}deg)`
}"
/>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="stylus" scoped>
@keyframes loading {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
.example-box
width 200px
height 200px
background-color blue
position relative
.loading-box
position absolute
top 0
left 0
right 0
bottom 0
background-color rgba(0, 0, 0, .7)
.bar-box
position: absolute
top 50%
left 50%
.bar
position absolute
left 0
top 0
height 4px
width 20px
border-top-right-radius 2px
border-bottom-right-radius 2px
transform-origin: left
background: linear-gradient(to right, transparent 38.2%, #fff 38.2%)
animation: loading 1.04s ease infinite both
</style>
vue
结合v-for,使用animation-delay造成动画时间差
<template lang="html">
<div class="example-box">
<div class="loading-box">
<div class="bar-box">
<div
v-for="n in 8"
:key="n"
class="bar"
:style="{
transform: `rotate(${n * 360 / 8}deg)`,
animationDelay: `${(n - 1) * 0.13}s`
}"
/>
</div>
</div>
</div>
</template>
<script>
export default {
}
</script>
<style lang="stylus" scoped>
@keyframes loading {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
.example-box
width 200px
height 200px
background-color blue
position relative
.loading-box
position absolute
top 0
left 0
right 0
bottom 0
background-color rgba(0, 0, 0, .7)
.bar-box
position: absolute
top 50%
left 50%
.bar
position absolute
left 0
top 0
height 4px
width 20px
border-top-right-radius 2px
border-bottom-right-radius 2px
transform-origin: left
background: linear-gradient(to right, transparent 38.2%, #fff 38.2%)
animation: loading 1.04s ease infinite both
</style>
vue
:metal: 这样就完成了一个简单的loading特效啦
评论