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 ...
随机推荐
- Mac OS 装gdb
1 要求按照mac ports 2 命令:sudo port install gdb 3 安装位置在: /opt/local/bin/ggdb , 注意,ggdb是执行命令
- struts2 的自定义 拦截器
Struts2的 拦截器: 对于拦截器,Struts2官方给出的 定义是: 拦截器是动态拦截Action调用的对象.它提供了一种机制,使开发者可以定义一段代码,在Action执行之前或者之后被调用执行 ...
- APK反编译之一:基础知识—APK、Dalvik字节码和smali文件
refs: APK反编译之一:基础知识http://blog.csdn.net/lpohvbe/article/details/7981386 APK反编译之二:工具介绍http://blog.csd ...
- C++模版详解(-)
C++模版: 模版时C++支持多参数多态的工具,使用模版可以为用户为类或函数声明一般模式,使得类的数据成员,或者成员函数的参数,返回值取得任意类型. 模版是一种对类型进行参数化的工具: 通 ...
- ubuntu下访问其他盘出现挂在错误解决办法
Error mounting /dev/sda5 at /media Linux下不能进入windows的NTFS分区之挂载错误问题 电 脑安装了win8,后在另一个分区(在win8下未分配空间)安装 ...
- iperf测试网络性能
分类: LINUX 2013-06-17 18:52:21 Iperf是一个网络性能测试工具.可以测试TCP和UDP带宽质量,可以测量最大TCP带宽,具有多种参数和UDP特性,可以报告带宽 ...
- php生成word
https://packagist.org/packages/phpoffice/phpword
- python 中__getitem__ 和 __iter__ 的区别
# -*- coding: utf-8 -*- class Library(object): def __init__(self): self.books = { 'title' : 'a', 'ti ...
- jmeter上传图片附件-小插曲
背景 最近,接到新项目的接口测试,发现该接口是需要上传图片,开始折腾了好久没有搞定,最后才发现st和sid,并不是作为请求实体,而是url的一部分,好吧,是我没有仔细 请求参数 { "con ...
- 拟牛顿法/Quasi-Newton,DFP算法/Davidon-Fletcher-Powell,及BFGS算法/Broyden-Fletcher-Goldfarb-Shanno
拟牛顿法/Quasi-Newton,DFP算法/Davidon-Fletcher-Powell,及BFGS算法/Broyden-Fletcher-Goldfarb-Shanno 转载须注明出处:htt ...