项目中要用到倒计时,用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倒计时的更多相关文章

  1. VUE组件 之 倒计时(防刷新)

    思路: 一.效果图: 二.CSS代码 .box{ width: 300px; height: 100px; line-height: 100px; margin: 100px auto; backgr ...

  2. vue列表数据倒计时存在的一些坑

    vue 列表数据倒计时,在页面销毁前需要清除定时器,否着会报错. export default { data() { return { list: [] } }, mounted() { for (l ...

  3. vue实现验证码倒计时60秒的具体代码

    vue实现验证码倒计时60秒的具体代码 <span v-show="show" @click="getCode">获取验证码</span> ...

  4. jquery.countdown 倒计时插件的学习

    1.第一种简单的使用 第一个时间是你的倒计时截止时间,finalDate格式可以是YYYY/MM/DD MM/DD/YYYY YYYY/MM/DD hh:mm:ss MM/DD/YYYY hh:mm: ...

  5. vue 设计一个倒计时秒杀的组件

    简介: 倒计时秒杀组件在电商网站中层出不穷  不过思路万变不离其踪,我自己根据其他资料设计了一个vue版的 核心思路:1.时间不能是本地客户端的时间  必须是服务器的时间这里用一个settimeout ...

  6. Vue 获取验证码倒计时组件

    子组件 <template> <a class="getvalidate":class="{gray: (!stop)}"@click='cl ...

  7. zepto插件 countdown 倒计时插件 从jquery 改成 zepto

    插件特色:支持zepto库  支持时间戳格式 支持年月日时分秒格式 countdown 由jquery依赖库改成zepto zepto的event机制与jquery不同,所以更换之后代码不能正常运行 ...

  8. vue 15分钟倒计时

    HTML: <span>{{minute}}:{{second}}</span> script: 一          二 // 倒计时 num(n) { return n & ...

  9. jQuery.countdown倒计时插件

    https://github.com/hilios/jQuery.countdown 文档:http://hilios.github.io/jQuery.countdown/documentation ...

随机推荐

  1. 【AGC010F】Tree Game

    Description 有一棵\(n\)个节点的树(\(n \le 3000\)),第\(i\)条边连接\(a_i,b_i\),每个节点\(i\)上有\(A_i\)个石子,高桥君和青木君将在树上玩游戏 ...

  2. BZOJ4732. [清华集训2016]数据交互(树链剖分+线段树+multiset)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4732 题解 首先,一个比较显然的结论是:对于一棵有根树上的两条链 \((x_1, y_1 ...

  3. 【纪中集训2019.3.12】Mas的仙人掌

    题意: ​ 给出一棵\(n\)个点的树,需要加\(m\)条边,每条边脱落的概率为\(p_{i}\) ,求加入的边在最后形成图中仅在一个简单环上的边数的期望: \(1 \le n \ , m \le 1 ...

  4. std::async

    https://www.cnblogs.com/qicosmos/p/3534211.html https://bobjin.com/blog/c_cpp_docs/reference/en/cpp/ ...

  5. Linux下的wine生活(QQ/微信/Office)

    My wine life like windows 本篇内容涉及QQ.微信.Office在wine中的使用配置. QQ 到deepin下载轻聊版. 如果安装了crossover,那么将其中opt/cx ...

  6. 面向对象SOLID原则的自我理解

    S.O.L.I.D 是面向对象设计(OOD)和面向对象编程(OOP)中的几个重要编码原则(Programming Priciple)的首字母缩写.面向对象设计的原则SRP The Single Res ...

  7. Docker部署Tomcat实例

    1.使用Docker部署Tomcat服务 http://www.open-open.com/lib/view/open1455717671698.html 2.搭建docker私有仓库 http:// ...

  8. GO_02:GO语言开篇

    Go的发展史 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发,后 ...

  9. fopen()、fwrite()、fread()函数使用说明与示例

    fopen()函数: 1.作用: 在C语言中fopen()函数用于打开指定路径的文件,获取指向该文件的指针. 2.函数原型: FILE * fopen(const char * path,const  ...

  10. canvas 入门

    <canvas>是HTML5新增的,是可以使用脚本(JavaScript)在其中绘制图像的HTML元素. canvas是由HTML代码配合高度和宽度属性而定义出的可绘制区域,JavaScr ...