最近在学Vue,顺便用了框架实现了需求
mounted() 是生命周期钩子,在Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)后进行回调
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#daojishi{
color: green;
text-align: center;
display: flex;
justify-content: center;
margin-top: 30px;
}
#img{
display: block;
margin: 0 auto;
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/dayjs/1.10.6/dayjs.min.js"></script>
<script src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<img id="img" src="../img/hzyyh.png">
<span id="daojishi">
<span>倒计时 {{calSpan()}}</span>
</span>
</div>
<script>
Vue.config.productionTip = false;
var vm=new Vue({
el:'#root',
data:{
day:'',
hour:'',
min:'',
sec:'',
},
methods:{
calSpan(){
var startTime = Date.now();
var endTime = new Date('09/10/2022 00:00:00');
var timeSpan = endTime.getTime() - startTime;
this.sec = Math.floor( timeSpan/1000%60);
this.min = Math.floor(timeSpan/1000/60%60);
this.hour = Math.floor(timeSpan/1000/60/60%24);
this.day =Math.floor( timeSpan/1000/60/60/24);
return this.day+"天 "+ this.hour+'时 '+this.min+"分 "+this.sec+'秒'
},
},
mounted() {
setInterval(this.calSpan, 1000);
},
});
</script>
</body>
</html>