Vue 实现countDown倒计时
项目中要用到倒计时,用Vue 实现了一个

<template>
<transition name="bkcd">
<div class="bkCountDown" v-show="bkCountDownShow">
<div class="kbCountDownTitle">
<img src="http://static.crecgec.com/Kaipiao/countDownTitle.png">
</div>
<div id="kbCountDownContent" class="kbCountDownContent" ref="kbCountDownContent">
</div>
<!--倒计时结束后提示的信息-->
<div class="cdEndCon" v-show="cdEndConShow">{{cdEndContent}}</div>
</div>
</transition>
</template> <script>
import $ from 'jquery' export default {
props: {
// 控制倒计时页面显示、隐藏
bkCountDownShow: {
type: Boolean,
default: true
},
// 这个参数:为了实现中途倒计时暂停功能
// 控制倒计时暂停/开始
cdStartOrEnd: {
type: Boolean,
default: true
},
// 倒计时的时间,有父组件传递
countDownTime: {
type: String,
default: '2017/11/9 15:03:01'
},
// 倒计时结束后显示的内容
cdEndContent: {
type: String,
default: '倒计时已经结束'
}
},
data () {
return {
// 倒计时结束后显示cdEndCon
cdEndConShow: false,
timestamp: '', // 倒计时的时间戳
cdTimer: '', // setTimeOut值
timeInterval: '', // 倒计时结束时间与当前时间的之间的间隔
timeIntervalVal: '', // 保存时间间隔的参数
d: '',
h: '',
m: '',
s: '',
days: * * ,
hours: * ,
minutes:
}
},
mounted () {
this.countdown()
},
watch: {
// 监控cdStartOrEnd值
cdStartOrEnd () {
if (this.cdStartOrEnd) {
this.tick()
} else {
clearTimeout(this.cdTimer)
}
}
},
methods: {
countdown () {
this.timestamp = new Date(this.countDownTime).getTime()
this.init('kbCountDownContent')
},
// 初始化
init (ele) {
$.each(['Hours', 'Minutes', 'Seconds'], function (i) {
$('<div class="count' + this + '">').html(
`<div class = "countPos">\
<span class="digit static"></span>\
</div>\
<div class="countPos">\
<span class="digit static"></span>\
</div>`
).appendTo($('#' + ele))
if (this !== 'Seconds') {
$('#' + ele).append('<div class="countDiv countDiv' + i + '"></div>')
}
})
this.tick()
},
tick () {
// 每次进入这个方法,就重新计算和当前时间的间隔,然后赋值给timeInterval
this.timeInterval = Math.floor((this.timestamp - (new Date())) / )
if (this.timeInterval < ) {
this.timeInterval =
}
this.timeIntervalVal = this.timeInterval
// Number of days left
// 现在是只有时分秒,可以通过调整下面的代码,来确定显示什么
// this.d = Math.floor(this.timeInterval / this.days)
// this.updateDuo(0, 1, this.d)
// this.timeInterval -= this.d * this.days
// Number of hours left
this.h = Math.floor(this.timeInterval / this.hours)
this.updateDuo(, , this.h)
this.timeInterval -= this.h * this.hours
// Number of minutes timeInterval
this.m = Math.floor(this.timeInterval / this.minutes)
this.updateDuo(, , this.m)
this.timeInterval -= this.m * this.minutes
// Number of seconds timeInterval
this.s = this.timeInterval
this.updateDuo(, , this.s)
// timeIntervalVal大于0,就执行setTimeout方法
if (this.timeIntervalVal > ) {
this.cdTimer = setTimeout(this.tick, )
} else {
// 倒计时结束
this.cdEndConShow = true
// 这块可以添加emit,给父组件传参
// 通过emit给父组件传参数来操作bkCountDownShow
// bkCountDownShow = false
}
},
updateDuo (minor, major, value) {
this.switchDigit($('#kbCountDownContent').find('.countPos').eq(minor), Math.floor(value / ) % )
this.switchDigit($('#kbCountDownContent').find('.countPos').eq(major), value % )
},
switchDigit (position, number) {
let digit = position.find('.digit')
if (digit.is(':animated')) {
return false
}
if (position.data('digit') === number) {
return false
}
position.data('digit', number)
var replacement = $('<span>', {
'class': 'digit',
css: {
top: '-170px',
opacity:
},
html: number
})
digit
.before(replacement)
.removeClass('static')
.animate({top: '170px', opacity: }, 'slow', function () {
digit.remove()
})
replacement
.delay()
.animate({top: , opacity: }, 'slow', function () {
replacement.addClass('static')
})
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
*{
margin:;
padding:;
font-family: 'Microsoft Yahei',Tahoma,'Simsun','宋体' !important;
} .bkCountDown{
width: %;
height: 980px;
background:url('http://static.crecgec.com/Kaipiao/background.png') #b0b0b0;
position: absolute;
background-size: cover;
overflow: hidden;
}
.kbCountDownTitle{
width: 1070px;
height: 120px;
line-height: 120px;
font-size: 120px;
margin: 190px auto ;
text-align: center;
color: #fff;
}
.kbCountDownContent{
width:1070px;
margin:160px auto ;
text-align:center;
letter-spacing:-3px;
overflow: hidden;
}
.countPos{
display: inline-block;
width: 150px;
height: 170px;
overflow: hidden;
position: relative;
margin-left: 15px;
} .digit{
position:absolute;
display:block;
width:150px;
height: 170px;
line-height: 170px;
text-align:center;
color:#fff;
font-size: 80px;
background: url('http://static.crecgec.com/Kaipiao/countDown.png') no-repeat;
} .digit.static{
background: url('http://static.crecgec.com/Kaipiao/countDown.png') no-repeat;
}
.countDays,.countHours,.countMinutes,.countSeconds{
float: left;
font-size: ;
}
.countDiv{
display:inline-block;
width:10px;
height:50px;
float: left;
margin-top: 60px;
margin-left: 15px;
background: url('http://static.crecgec.com/Kaipiao/countDown1.png') no-repeat;
}
.cdEndCon{
width:1070px;
margin:20px auto ;
text-align: center;
color: #fff;
font-size: 20px;
}
.bkcd-enter-active, .bkcd-leave-active{
transition: opacity .5s
}
.bkcd-enter, .bkcd-leave-to{
opacity:
}
</style>
Vue 实现countDown倒计时的更多相关文章
- VUE组件 之 倒计时(防刷新)
思路: 一.效果图: 二.CSS代码 .box{ width: 300px; height: 100px; line-height: 100px; margin: 100px auto; backgr ...
- vue列表数据倒计时存在的一些坑
vue 列表数据倒计时,在页面销毁前需要清除定时器,否着会报错. export default { data() { return { list: [] } }, mounted() { for (l ...
- vue实现验证码倒计时60秒的具体代码
vue实现验证码倒计时60秒的具体代码 <span v-show="show" @click="getCode">获取验证码</span> ...
- jquery.countdown 倒计时插件的学习
1.第一种简单的使用 第一个时间是你的倒计时截止时间,finalDate格式可以是YYYY/MM/DD MM/DD/YYYY YYYY/MM/DD hh:mm:ss MM/DD/YYYY hh:mm: ...
- vue 设计一个倒计时秒杀的组件
简介: 倒计时秒杀组件在电商网站中层出不穷 不过思路万变不离其踪,我自己根据其他资料设计了一个vue版的 核心思路:1.时间不能是本地客户端的时间 必须是服务器的时间这里用一个settimeout ...
- Vue 获取验证码倒计时组件
子组件 <template> <a class="getvalidate":class="{gray: (!stop)}"@click='cl ...
- zepto插件 countdown 倒计时插件 从jquery 改成 zepto
插件特色:支持zepto库 支持时间戳格式 支持年月日时分秒格式 countdown 由jquery依赖库改成zepto zepto的event机制与jquery不同,所以更换之后代码不能正常运行 ...
- vue 15分钟倒计时
HTML: <span>{{minute}}:{{second}}</span> script: 一 二 // 倒计时 num(n) { return n & ...
- jQuery.countdown倒计时插件
https://github.com/hilios/jQuery.countdown 文档:http://hilios.github.io/jQuery.countdown/documentation ...
随机推荐
- 解题:NOI 2014 动物园
题面 其实好像并不难,因为猫老师(应该是猫老师吧,还是LX大佬?)有一句话让我印象深刻:“包的(border)的包的还是包的”=.= 统计个数不就是统计长度么,然后根据上面那句话,当$nxt$长度大于 ...
- NTT+多项式求逆
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- 【DP】【CF1097D】 Makoto and a Blackboard
更好的阅读体验 Description 给定一个数 \(n\),对它进行 \(k\) 次操作,每次将当前的数改为自己的因数,包括 \(1\) 和自己.写出变成所有因数的概率是相等的.求 \(k\) 次 ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- 十、Shell基础
一.shell概述 1.Shell是什么 shell是一个命令行解释器,他为用户提供了一个向linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动.挂起.停止甚至编写一些程序 ...
- linux基础命令之sed
什么是sed? sed是一个流编辑器,英文全名为stream editor,流的意思就是能处理文件或者通过管 道传输来的数据.主要针对行,也就是行编辑.默认是不编辑文件的,处理时,把当前 ...
- NO.6LINUX基本命令
1.练习1 ) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cd / cat /etc/passwd /etc/group>1.txt 2) 将用户信息数据库文 ...
- 怎样彻底卸载(删除)SQL server2000
如何完全卸载SQL server 2000 一.卸载安装程序:找到电脑右下角的Windows开始按钮,找到控制面板——卸载程序. 找到server 2000的安装程序,并点击后面的卸载按钮. 是,确 ...
- [USACO09NOV]硬币的游戏A Coin Game
https://daniu.luogu.org/problemnew/show/P2964 dp[i][j] 表示桌面上还剩i枚硬币时,上一次取走了j个的最大得分 枚举这一次要拿k个,转移到dp[i- ...
- codevs 3160 最长公共子串
3160 最长公共子串 http://codevs.cn/problem/3160/ 时间限制: 2 s 空间限制: 128000 KB 题目描述 Description 给出两个由小写字母组 ...