项目中要用到倒计时,用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. 编写shell脚本需要特别关注的注意点

    shell脚本中的条件判断句式 1. if [ condition ];then statement fi 2. If [ condition ];then statement elif [ cond ...

  2. linux系统之定制rpm包

    FPM打包工具 FPM的作者是jordansissel FPM的github:https://github.com/jordansissel/fpm FPM功能简单说就是将一种类型的包转换成另一种类型 ...

  3. 控制对象的创建方式(禁止创建栈对象or堆对象)和创建的数量

    我们知道,C++将内存划分为三个逻辑区域:堆.栈和静态存储区.既然如此,我称位于它们之中的对象分别为堆对象,栈对象以及静态对象.通常情况下,对象创建在堆上还是在栈上,创建多少个,这都是没有限制的.但是 ...

  4. AS可视化布局中文乱码

    求助android studio 的可视化布局中文乱码-CSDN论坛-CSDN.NET-中国最大的IT技术社区http://bbs.csdn.net/topics/391887442 Android ...

  5. vue入门教程

    vue视频教程(对vue有个概览,要掌握vue-cli的用法,对vue-router,vuex有基本的概念) https://www.imooc.com/learn/1091 1. vue-cli v ...

  6. .net mvc 一个Action的 HttpGet 和 HttpPost

    http://www.cnblogs.com/freeliver54/p/3747836.html 本文转自:http://stackoverflow.com/questions/11767911/m ...

  7. Chapter12(动态内存)--C++Prime笔记

    1.分配再静态或栈内存中的对象由编译器自动创建销毁. 2.C++中动态内存的管理是通过 new:前者为对象非配空间并返回一个指向该对象的指针. delete:接受一个动态对象的指针,摧毁该对象,并释放 ...

  8. HDU 6249

    Alice’s Stamps Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. Hadoop部署方式-本地模式(Local (Standalone) Mode)

    Hadoop部署方式-本地模式(Local (Standalone) Mode) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Hadoop总共有三种运行方式.本地模式(Local ...

  10. python实现域账号登陆

    需求:公司的网路比较变态,每天到了24点自动断开,为了避免一台测试机断网,用python做了一个自动登录 原理:时间到了24点的时候,每隔10秒检测是否可以ping通www.baidu.com,如果p ...