首先,什么是进度条,跟大家举个例子,我们在浏览一个网页时,网页在加载的时候会有一个进度的提示,这样能够准确的告诉我们什么时候资源可以完全加载出来,最常见的就是一个进度条不断的变化,数字在不断的增加。
进度条经常运用于网页,即使我们意识到不是所有的东西都将瞬间被加载完成,这些进度条用于提醒使用者关于网页上具体的任务进程,譬如上传,下载,加载应用程序等。
首先展示一下效果
动态的加载进度
看代码
html
<div id="home">
<template id="xcprogress">
<div class="xcprogress">
<div class="xcprogress-bar" :style="{ width: `${value}%` }">
<span class="xcprogress-bar-value">{{ value }}%</span>
</div>
</div>
</template>
</div>
js
data() {
return {
value: 0,
};
},
watch: {
value(newVal) {
if (newVal > 100) {
this.value = 100;
} else if (newVal < 0) {
this.value = 0;
}
},
},
components: {},
mounted() {
setTimeout(() => {
setInterval(() => {
this.value += 20;
}, 800);
}, 1000);
},
css
#home {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
margin-top: 30px;
}
.xcprogress {
width: 300px;
height: 20px;
background: #e5e5e5;
border-radius: 4px;
overflow: hidden;
}
.xcprogress-bar {
width: 0;
height: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background: cornflowerblue;
background-image: linear-gradient(
45deg,
rgba(255, 255, 255, 0.15) 25%,
transparent 25%,
transparent 50%,
rgba(255, 255, 255, 0.15) 50%,
rgba(255, 255, 255, 0.15) 75%,
transparent 75%,
transparent
);
background-size: 40px 40px;
transition: width 0.6s ease;
border-radius: 4px;
animation: xcprogress-bar-anim 2s linear infinite;
}
.xcprogress-bar-value {
font-size: 13px;
font-weight: bold;
color: white;
margin-right: 5px;
}
@keyframes xcprogress-bar-anim {
from {
background-position: 40px 0;
}
to {
background-position: 0 0;
}
}
整体代码
<template>
<div id="home">
<template id="xcprogress">
<div class="xcprogress">
<div class="xcprogress-bar" :style="{ width: `${value}%` }">
<span class="xcprogress-bar-value">{{ value }}%</span>
</div>
</div>
</template>
</div>
</template>
<script>
// @ is an alias to /src
export default {
name: "HomeView",
data() {
return {
value: 0,
};
},
watch: {
value(newVal) {
if (newVal > 100) {
this.value = 100;
} else if (newVal < 0) {
this.value = 0;
}
},
},
components: {},
mounted() {
setTimeout(() => {
setInterval(() => {
this.value += 20;
}, 800);
}, 1000);
},
};
</script>
<style lang="less" scoped>
#home {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
margin-top: 30px;
}
.xcprogress {
width: 300px;
height: 20px;
background: #e5e5e5;
border-radius: 4px;
overflow: hidden;
}
.xcprogress-bar {
width: 0;
height: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background: cornflowerblue;
background-image: linear-gradient(
45deg,
rgba(255, 255, 255, 0.15) 25%,
transparent 25%,
transparent 50%,
rgba(255, 255, 255, 0.15) 50%,
rgba(255, 255, 255, 0.15) 75%,
transparent 75%,
transparent
);
background-size: 40px 40px;
transition: width 0.6s ease;
border-radius: 4px;
animation: xcprogress-bar-anim 2s linear infinite;
}
.xcprogress-bar-value {
font-size: 13px;
font-weight: bold;
color: white;
margin-right: 5px;
}
@keyframes xcprogress-bar-anim {
from {
background-position: 40px 0;
}
to {
background-position: 0 0;
}
}
</style>