倒计时组件,比较复杂一点,大神勿调侃,精确到毫秒,因为项目中多次出现倒计时,所以拿出来分享下

使用方法:
1.在父组件中引入"uni-countdown"
//import uniCountdown from "./uni-countdown";

2.在父组件中注册 uniCountdown
//components:{uniCountdown},

3.放在父组件中使用
//<uni-countdown></uni-countdown> 参数代码太多直接看文件里面的吧

4.调用uniCountdown组件
//this.StatusLock = true

注:index.vue为父组件,后者为子组件,效果图先上,可以先看是否是自己想要的组件,再选择使用

index.vue

<template>
<div>
<div class="TimingBox" v-show="StatusLock">
<div>
<p class="texttitle">您的粉丝为您创造{{Amount}}元奖励</p>
<p class="text">倒计时结束您将永远失去他们</p>
<div class="ContentOfTheTiming">
<div style="font-size:.6rem;" class="flex center">
<uni-countdown
bordercolor="#FFAA00"
background="url('/wxviews/img/组10.png') center center"
fontSize=".7rem"
splitorColor="#fa0"
:show-day="false"
:hour="cdHours"
:minute="cdMins"
:second="cdSecs"
:ms="cdMs"
></uni-countdown>
</div>
</div>
<p class="become_a_Member">点击“立即解锁”获得该奖励</p>
<div class="bigBoxBtn flex center" @click="TheBuyButton()">
<div class="TheBuyButton">立即获得</div>
</div>
</div>
</div>
</div>
</template> <script> import uniCountdown from "../components/uni-countdown"; export default {
data() {
return {
// 时
cdHours: 0,
// 分
cdMins: 0,
// 秒
cdSecs: 0,
// 毫秒
cdMs: 60, Statuslock: false,
productDetail: {},
Amount: null
};
},
components:{
uniCountdown
},
created() {
//在钩子里面先请求接口拿到时间
this.CountDown();
},
methods: {
CountDown() {
//请求方式,看你们自己想用啥 就用啥
Request.post(
"api/Post_GetCommissionOrderCountdown",
{
MemLoginId: localStorage.getItem("MemLoginId")
},
res => { this.productDetail = res;
this.Amount = res.Amount;
//执行showTime()
const countDownArr = this.showTime(
null,
// 拿到结束时间 EndTime
this.productDetail.EndTime
).split("-");
//Status ? 1 : 0
if (res.Status === 0) {
// 控制显示或隐藏 StatusLock
this.StatusLock = false;
return;
}
if ((this.cdHours = 0 && this.cdMins == 0 && this.cdSecs == 0)) {
this.StatusLock = false;
return;
}
if (this.cdHours > 0 || this.cdMins > 0 || this.cdSecs > 0) {
this.StatusLock = true;
this.cdHours = countDownArr[0] * 24 + countDownArr[1];
this.cdMins = countDownArr[2];
this.cdSecs = countDownArr[3];
return;
}
if (res.Status === 1) {
this.StatusLock = true;
this.cdHours = countDownArr[0] * 24 + countDownArr[1];
this.cdMins = countDownArr[2];
this.cdSecs = countDownArr[3];
return;
}
},
err => {}
);
},
TheBuyButton(){
alert('暂时还没有开放此功能')
},
showTime(startTimeStr, endTimeStr) {
var startTime = startTimeStr
? new Date(startTimeStr.replace(/-/g, "/"))
: new Date();
var endTime = new Date(endTimeStr.replace(/-/g, "/"));
var totalSecs = (endTime - startTime) / 1000;
var days = Math.floor(totalSecs / 3600 / 24);
var hours = Math.floor((totalSecs - days * 24 * 3600) / 3600);
var mins = Math.floor((totalSecs - days * 24 * 3600 - hours * 3600) / 60);
var secs = Math.floor(
totalSecs - days * 24 * 3600 - hours * 3600 - mins * 60
);
if ((days + "").slice(0, 1) != "-") {
return days + "-" + hours + "-" + mins + "-" + secs;
} else {
return 0 + "-" + 0 + "-" + 0 + "-" + 0;
}
}
}
};
</script> <style lang="less" scoped>
.TheBuyButton {
width: 4rem;
height: 1.2rem;
background: linear-gradient(
90deg,
rgba(254, 239, 54, 1) 0%,
rgba(246, 197, 25, 1) 100%
);
box-shadow: 0px 0px 3px 0px rgba(115, 91, 6, 1);
border-radius: 0.6rem;
margin: 0 auto;
position: relative;
color: #735b06;
font-size: 0.6rem;
line-height: 1.2rem;
}
.TimingBox {
width: 100%;
background: #fff;
height: 6.7rem;
}
.ContentOfTheTiming {
width: 100%;
height: 1rem;
margin: 0 0 0.2rem 0;
}
.become_a_Member {
color: #fff;
font-size: 0.5rem;
}
.djs > div {
width: 13.8rem;
height: 6rem;
margin: 0 auto;
background-color: #fff;
background: url("") no-repeat;
background-size: 100% 100%;
background-repeat: no-repeat;
text-align: center;
border-radius: 0.5rem;
box-sizing: border-box;
}
.bigBoxBtn {
width: 4.3rem;
height: 1.5rem;
background: linear-gradient(
90deg,
rgba(254, 239, 54, 1) 0%,
rgba(246, 197, 25, 1) 100%
);
box-shadow: 0px 1px 3px 0px rgba(115, 91, 6, 0.67);
border-radius: 0.7rem;
position: relative;
margin: 0 auto;
bottom: -0.2rem;
}
.center {
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
</style>

uni-countdown.vue

<template>
<div class="uni-countdown">
<div
v-if="showDay"
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize}"
class="uni-countdown__number">{{ d }}</div>
<div
v-if="showDay"
:style="{color:splitorColor}"
class="uni-countdown__splitor">天</div>
<div :style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ h1 }}</p><hr class="hrxt"></div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ h2 }}</p><hr class="hrxt"></div>
<div
:style="{color:splitorColor}"
class="uni-countdown__splitor">{{ showColon ? ':' : '时' }}</div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ i1 }}</p><hr class="hrxt"></div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ i2 }}</p><hr class="hrxt"></div>
<div
:style="{color:splitorColor}"
class="uni-countdown__splitor">{{ showColon ? ':' : '分' }}</div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ s1 }}</p><hr class="hrxt"></div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ s2 }}</p><hr class="hrxt"></div>
<div
<div
:style="{color:splitorColor}"
class="uni-countdown__splitor">{{ showColon ? ':' : '秒' }}</div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ ms1 }}</p><hr class="hrxt"></div>
<div
:style="{borderColor:borderColor, color:color, background:background, fontSize:fontSize,backgroundSize:backgroundSize,backgroundRepeat:backgroundRepeat}"
class="uni-countdown__number"><p style="line-height:1rem; margin:0;">{{ ms2 }}</p><hr class="hrxt"></div>
<div
v-if="!showColon"
:style="{color:splitorColor}"
class="uni-countdown__splitor">秒</div>
</div>
</template>
<script>
export default {
name: 'UniCountdown',
props: {
showDay: {
type: Boolean,
default: true
},
showColon: {
type: Boolean,
default: true
},
background: {
type: String,
default: '#FFFFFF'
},
backgroundColor: {
type: String,
default: '#FFFFFF'
},
borderColor: {
type: String,
default: '#000000'
},
backgroundImg: {
type:String,
default:""
},
fontSize: {
type: String,
default: '30px'
},
color: {
type: String,
default: '#fff'
},
splitorColor: {
type: String,
default: '#000000'
},
day: {
type: Number,
default: 0
},
hour: {
type: Number,
default: 0
},
minute: {
type: Number,
default: 0
},
second: {
type: Number,
default: 0
},
ms: {
type: Number,
default: 0
}
},
data () {
return {
timer: null,
d: '00',
h: '00',
h1: '00',
h2: '00',
i: '00',
i1: '00',
i2: '00',
s: '00',
s1: '0',
s2: '0',
ms: '00',
ms1: '0',
ms2: '0',
leftTime: 0,
seconds: 0
}
},
created: function (e) {
this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second, this.ms)
this.countDown()
this.timer = setInterval(() => {
this.seconds--
if (this.seconds < 0) {
this.timeUp()
return
}
this.countDown()
}, 1000/60)
},
beforeDestroy () {
clearInterval(this.timer)
},
methods: {
toSeconds (day, hours, minutes, seconds, ms) {
return (day * 60 * 60 * 60 * 24) + (hours * 60 * 60 * 60) + (minutes * 60 * 60) + (seconds * 60) + ms
},
timeUp () {
clearInterval(this.timer)
this.$emit('timeup')
},
countDown () {
var seconds = this.seconds
var [day, hour, minute, second, ms] = [0, 0, 0, 0, 0]
if (seconds > 0) {
day = Math.floor(seconds / (60 * 60 * 60 * 24))
hour = Math.floor(seconds / (60 * 60 * 60)) - (day * 24)
minute = Math.floor(seconds / 60 / 60) - (day * 24 * 60) - (hour * 60)
second = Math.floor(seconds / 60) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
ms = Math.floor(seconds) - (day * 24 * 60 * 60 * 60) - (hour * 60 * 60 * 60) - (minute * 60 * 60) - (second * 60)
} else {
this.timeUp()
}
if (day < 10) {
day = '0' + day
}
if (hour < 10) {
hour = '0' + hour
}
if (minute < 10) {
minute = '0' + minute
}
if (second < 10) {
second = '0' + second
}
if (ms < 10) {
ms = '0' + ms
}
this.d = day
this.h = hour
this.h1 = hour.toString().slice(0,1)
this.h2 = hour.toString().slice(1,2)
this.i = minute
this.i1 = minute.toString().slice(0,1)
this.i2 = minute.toString().slice(1,2)
this.s = second
this.s1 = second.toString().slice(0,1)
this.s2 = second.toString().slice(1,2)
this.ms = ms
this.ms1 = ms.toString().slice(0,1)
this.ms2 = ms.toString().slice(1,2)
}
}
}
</script>
<style lang="scss">
$countdown-height:.8rem;
.uni-countdown__number{
background-size: 100% 100%;
background-repeat: no-repeat;
}
.uni-countdown {
padding: .04rem 0;
display: inline-flex;
flex-wrap: nowrap;
justify-content: center; &__splitor {
justify-content: center;
line-height: $countdown-height;
padding: 0 .1rem;
font-size: .6rem; } &__number {
line-height: $countdown-height;
justify-content: center;
height: $countdown-height;
border-radius: .12rem;
margin: 0 .1rem;
font-size: .7rem;
font-weight: 500;
width: .7rem;
height: 1rem;
box-sizing: border-box;
background-size: 100% 100%;
background-repeat: no-repeat;
}
} </style>
<style lang="less">
.hrxt{
position: relative;
top: -.9rem;
left: 0;
width: .6rem;
border:1px solid #e5e5e5;
}
</style>

Vue自行封装常用组件-倒计时的更多相关文章

  1. Vue自行封装常用组件-弹出框

    使用方法: 1.在父组件中引入"box.vue" //import popUpBox from "./box.vue";   2.在父组件中注册 popUpBo ...

  2. Vue自行封装常用组件-文本提示

    使用方法:1.在父组件中引入"toast.vue" //import toast from "./toast"; 2.在父组件中注册 toast //compo ...

  3. Vue 数组封装和组件data定义为函数一些猜测

     数组封装 var vm={ list:[0,1] } var push=vm.list.push;//把数组原来的方法存起来 vm.list.push=function(arg){//重新定义数组的 ...

  4. Vue文件封装日历组件

    封装就是要具有灵活性,样式自适应,调用的时候传入props就可以变成自己想要的样式. 效果展示网址:https://1963331542.github.io/ 源代码: <template> ...

  5. Vue中封装axios组件实例

    首先要创建一个网络模块network文件夹  里面要写封装好的几个组件 在config.js里面这样写 在index.js要这样写 core.js文件里面内容如下 然后要在main.js文件里面要设置 ...

  6. [ vue ] Quasar封装q-dialog组件,在外层实现弹出框的开启和关闭

    场景描述: 见:https://www.cnblogs.com/remly/p/12981582.html 具体实现: <!-- 父组件 --> <template> < ...

  7. vue移动端常用组件

    3d picker组件 参考链接:https://segmentfault.com/a/1190000007253581?utm_source=tag-newest安装:npm install vue ...

  8. vue中封装swipe组件

    <template> <!-- TODO swipe --> <div id="hy-swiper"> <div class=" ...

  9. vue封装一些常用组件loading、switch、progress

    vue封装一些常用组件loading.switch.progress github文档https://github.com/zengjielin/vue-component-library loadi ...

随机推荐

  1. 在xcode找不到发布证书

    解决方法 1.访问XCode的Preferences>Accounts,在Apple IDs里面找到你的帐号,选中后,在右侧,在Name下面会有一行描述.双击.在弹出窗口里面,有个刷新按钮,点击 ...

  2. JavaScript高程第三版笔记(1-5章)

    第2章:在html中使用javascript ①script标签的defer属性 <script type="text/javascript" defer="def ...

  3. 并发编程之原子Atomic&Unsafe

    1.原子更新基本类型类   用于通过原子的方式更新基本类型,Atomic包提供了以下三个类: AtomicBoolean:原子更新布尔类型. AtomicInteger:原子更新整型. AtomicL ...

  4. linux如何设置磁盘配额?

    环境:CentOS7 需求:  zhang3 用户在  /dev/sdb1  上,只能使用100M的空间,建立 200个文件. 注意事项:XFS  不能对  /   开启配额             ...

  5. AI测试——旅程的终点

    在2019年6月,我产生了一个想法,即人工智能探索测试AIET(Artificial intelligence exploration test),大概用了一周时间来思考怎么把人工智能应用到测试领域, ...

  6. C# 重写WndProc

    重写WndProc方法来处理 Windows 消息 处理 Windows 消息. 在开发winForm时,常常要处理Windows消息,可以重写WndProc来实现.常见代码如下: using Sys ...

  7. java并发学习资料

    1.Java 并发编程知识梳理以及常见处理模式 https://github.com/Fadezed/concurrency 2.Java 高并发多线程编程系列 https://github.com/ ...

  8. sql server 查询某段日期某段时间内的数据

    如我要取8月1号到8月30号之间的早上7点半到晚上八点半这段时间内的数据. SELECT * FROM tableName AS tWHERE t.create_date BETWEEN '2017- ...

  9. azkaban安装步骤

    安装包 1.得到软件包 azkaban-executor-server-2.5.0.tar.gz azkaban-sql-script-2.5.0.tar.gz azkaban-web-server- ...

  10. CSP-J 2019 T3 纪念品

    \(\mathfrak{a}\).反思: 通过这道题成功发现自己的背包还是很差\(w\): 可能这是我\(gu\)了好久好久博客的报应叭 就在做这个题的时候,自己连背包\(dp\)的思想都忘了 背包可 ...