下面是“Vue组件实现卡片动画倒计时示例详解”的完整攻略:
标题
一、项目介绍
介绍该项目的背景和目的,如:Vue组件实现卡片动画倒计时,可以应用在各类网页和手机应用中,方便用户知晓某个活动、限时促销等的剩余时间。
二、技术栈
列举使用的技术,如:Vue.js框架、CSS3动画等。
三、项目实现
1.基础HTML、CSS实现
通过HTML和CSS实现基本的卡片样式和动画效果。示例代码如下:
<div class="card">
<div class="content">
<div class="time">
<span class="digit">0</span>
<span class="digit">0</span>
<span class="colon">:</span>
<span class="digit">0</span>
<span class="digit">0</span>
<span class="colon">:</span>
<span class="digit">0</span>
<span class="digit">0</span>
</div>
</div>
</div>
.card {
position: relative;
width: 300px;
height: 450px;
margin: 20px;
overflow: hidden;
background-color: #fff;
box-shadow: 0 10px 20px rgba(0,0,0,.3);
}
.card .content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
text-align: center;
}
.card .content .time {
display: flex;
justify-content: center;
align-items: center;
margin: 30px;
font-size: 40px;
font-weight: bold;
color: #fff;
}
.card .content .time .digit {
position: relative;
margin: 0 5px;
font-style: italic;
}
.card .content .time .colon:before {
content: ":";
position: absolute;
top: 50%;
left: -12px;
transform: translateY(-50%);
font-size: 50px;
color: #fff;
}
2.Vue.js实现
使用Vue.js框架进行开发,具体实现方法如下:
1)在Vue的模板中引用该卡片组件,并传入倒计时的起始时间。
<div id="app">
<card count-down="600"></card>
</div>
2)在Vue的data中定义倒计时的变量。
data: {
time: 0
}
3)通过Vue的computed计算属性,将时间转化为分钟、秒数等,并渲染在卡片上。
computed: {
minutes() {
return parseInt(this.time / 60)%60 < 10 ? '0'+ parseInt(this.time/60)%60 : parseInt(this.time/60)%60;
},
seconds() {
return this.time%60 < 10 ? '0'+ this.time%60 : this.time%60;
},
rest() {
return this.time <= 0 ? 'time up' : this.minutes + '分' + this.seconds + '秒';
}
}
<div class="time">
<span class="digit">{{ minutes.toString()[0] }}</span>
<span class="digit">{{ minutes.toString()[1] }}</span>
<span class="colon">:</span>
<span class="digit">{{ seconds.toString()[0] }}</span>
<span class="digit">{{ seconds.toString()[1] }}</span>
</div>
<p v-text="rest"></p>
4)通过Vue的定时器setInterval()实现秒数的递减。
mounted() {
setInterval(() => {
this.time--;
}, 1000);
}
3. CSS3动画实现
为了让卡片具有倒计时的动感效果,使用了CSS3的transform、transition等属性,具体实现方法如下:
.card .content .time .digit {
position: relative;
margin: 0 5px;
font-style: italic;
transform: translateY(0);
transition: all 0.3s ease-out;
}
.card .content .time .digit.out {
transform: translateY(-100%);
}
.card .content .time .digit.outback {
transform: translateY(100%);
}
.card .content .time .colon:before {
content: ":";
position: absolute;
top: 50%;
left: -12px;
transform: translateY(-50%);
font-size: 50px;
color: #fff;
opacity: 0.8;
transform: scale(1);
animation: colon 1s infinite;
}
@keyframes colon {
0% {
transform: scale(1);
opacity: 0.8;
}
50% {
transform: scale(2);
opacity: 1;
}
100% {
transform: scale(1);
opacity: 0.8;
}
}
四、完整代码展示
以上就是Vue组件实现卡片动画倒计时的完整攻略,下面是完整的HTML、CSS和Vue.js代码。
<!DOCTYPE html>
<head>
<title>Vue组件实现卡片动画倒计时示例详解</title>
<meta charset="utf-8">
</head>
<body>
<div id="app">
<card count-down="600"></card>
</div>
<template id="card-template">
<div class="card">
<div class="content">
<h2>活动倒计时</h2>
<div class="time">
<span class="digit">{{ minutes.toString()[0] }}</span>
<span class="digit">{{ minutes.toString()[1] }}</span>
<span class="colon">:</span>
<span class="digit">{{ seconds.toString()[0] }}</span>
<span class="digit">{{ seconds.toString()[1] }}</span>
</div>
<p v-text="rest"></p>
</div>
</div>
</template>
<script src="https://unpkg.com/vue"></script>
<script>
Vue.component('card', {
template: '#card-template',
props: ['countDown'],
data: function() {
return {
time: this.countDown
}
},
computed: {
minutes() {
return parseInt(this.time / 60)%60 < 10 ? '0'+ parseInt(this.time/60)%60 : parseInt(this.time/60)%60;
},
seconds() {
return this.time%60 < 10 ? '0'+ this.time%60 : this.time%60;
},
rest() {
return this.time <= 0 ? 'time up' : this.minutes + '分' + this.seconds + '秒';
}
},
mounted() {
setInterval(() => {
this.time--;
}, 1000);
}
});
new Vue({
el: '#app'
});
</script>
<style>
.card {
position: relative;
width: 300px;
height: 450px;
margin: 20px;
overflow: hidden;
background-color: #fff;
box-shadow: 0 10px 20px rgba(0,0,0,.3);
}
.card .content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
text-align: center;
}
.card .content .time {
display: flex;
justify-content: center;
align-items: center;
margin: 30px;
font-size: 40px;
font-weight: bold;
color: #fff;
}
.card .content .time .digit {
position: relative;
margin: 0 5px;
font-style: italic;
transform: translateY(0);
transition: all 0.3s ease-out;
}
.card .content .time .digit.out {
transform: translateY(-100%);
}
.card .content .time .digit.outback {
transform: translateY(100%);
}
.card .content .time .colon:before {
content: ":";
position: absolute;
top: 50%;
left: -12px;
transform: translateY(-50%);
font-size: 50px;
color: #fff;
opacity: 0.8;
transform: scale(1);
animation: colon 1s infinite;
}
@keyframes colon {
0% {
transform: scale(1);
opacity: 0.8;
}
50% {
transform: scale(2);
opacity: 1;
}
100% {
transform: scale(1);
opacity: 0.8;
}
}
</style>
</body>
这里只是展示了一种倒计时样式的实现,实际上你可以通过改变CSS样式、调整Vue组件的实现等等来拓展更多酷炫的效果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vue组件实现卡片动画倒计时示例详解 - Python技术站