Vue -- filters 过滤器、倒计时效果
局部过滤器
时间、***号
<div id="wrapper" class="wrapper" style="display: none;">
距离活动结束还剩:<p v-html="times"></p>
</div>
<script src="vue.min.js"></script>
<script src="axios.min.js"></script>
<script src="qs.min.js"></script>
var mainVM = new Vue({
el: '#wrapper',
data: {
initActiveMsgObj:{}, // 接收接口返回的数据
timer: null, // 计时器
times: '<span>0</span>天<span>00</span>时<span>0</span>分', // 倒计时到分钟,比真正的秒还差60秒,需要赋值的时候加进去,如果是到秒,就不需要了
countDown: 0
},
filters: {
formatNumber(value){ // 格式化金额
var nStr = value;
if(!nStr){return nStr;}
nStr += '';
x = nStr.split('.');
x1 = x[0];
if(!x[1]){
x[1] = '00';
}
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
},
formatDate(timestamp) {
if (!timestamp) return;
var date = new Date(timestamp * 1000);
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
return M + "." + D;
},
formatDateChina(timestamp) {
if (!timestamp) return;
var date = new Date(timestamp * 1000);
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
return M + "月" + D + "日";
},
},
mounted() {
document.getElementById('wrapper').style.display = 'block';
},
created() {
this.getDetail();
},
methods: {
getDetail(){ // 初始化数据
axios({
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'post',
dataType: "json",
data: Qs.stringify({
tid: this.GetQueryString('tid'), // 从url上获取id
}),
url: url,
}).then(function (res) {
if(res.data && res.data.result){
res = res.data
if(res.status == 200){
mainVM.initActiveMsgObj = res.result;
mainVM.countDown = Number(res.result.end_time) + 60; // 这里如果是时间到分钟,需要增加一个60s,这样防止,到00还需要等一分钟才结束活动,如果显示到秒就不需要了次变量了。
mainVM.timeDown();
}else{
}
}
}).catch(function(error){
})
},
GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
},
timeDown() { // 倒计时
clearInterval(this.timer);
var starttime = this.initActiveMsgObj.start_time;
var nowDate = Math.round(new Date() / 1000); // 当前时间
//var endtime = Number(this.initActiveMsgObj.end_time);
var endtime = this.countDown;
// endtime = Math.round(new Date('2019/7/10 14:56:00') / 1000); + 60;
if(endtime < nowDate){return}
var totalSeconds = parseInt((endtime - nowDate)); // 相差的总秒数
//天数
var days = Math.floor(totalSeconds / (60 * 60 * 24));
//取模(余数)
var modulo = totalSeconds % (60 * 60 * 24);
//小时数
var hours = Math.floor(modulo / (60 * 60));
hours = hours < 10 ? ('0' + hours) : hours;
modulo = modulo % (60 * 60);
//分钟
var minutes = Math.floor(modulo / 60);
minutes = minutes < 10 ? '0' + minutes : minutes;
// console.log(minutes)
//秒
// var seconds = modulo % 60;
// console.log(seconds);
//输出到页面
this.times = '<span>'+ days +'</span>天<span>'+ hours +'</span>时<span>'+ minutes +'</span>分';
// console.log(days + "天" + hours + "时" + minutes + "分");
//if(totalSeconds <= 0){
if(totalSeconds <= 60){
clearInterval(this.timer);
window.location.reload()
}else{
this.timer = setInterval(this.timeDown, 1000);
}
},
}
})
Vue -- filters 过滤器、倒计时效果的更多相关文章
- vue filters过滤器
vue filters过滤器 vue.js允许我们自定义过滤器,可被使用于一些常见的文本格式化,过滤器可以用在两个地方,双花括号插值和 v-bind表达式.最常见的就是双花括号插值. 比如如下代码:{ ...
- vue filters过滤器的使用
说的很详细 https://www.w3cplus.com/vue/how-to-create-filters-in-vuejs.html
- 六、vue基础--过滤器定义
七.过滤器定义 1.使用:{{username|strip}}.<a :href="url|strip">百度</a> 2.定义:都是定义一个函数,这个函数 ...
- Vue学习之--------Vue中过滤器(filters)的使用(代码实现)(2022/7/18)
1.过滤器 1.1 概念 过滤器: 定义:对要显示的数据进行特定格式化后再显示(适用于一些简单逻辑的处理). 语法: 1.注册过滤器:Vue.filter(name,callback) 或 new V ...
- Vue.js Cookbook: 添加实例属性; 👍 axios(4万➕✨)访问API; filters过滤器;
add instance properties //加上$,防止和已经定义的data,method, computed的名字重复,导致被覆写.//可以自定义添加其他符号. Vue.prototype. ...
- vue自定义过滤器的创建和使用
1.简单介绍 过滤器的作用:实现数据的筛选.过滤.格式化. 过滤器的本质是一个有参数,有返回值的方法. 过滤器可以用在两个地方:双花括号插值和v-bind表达式(后者从2.1.0+开始支持 ...
- Vue自定义过滤器
gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...
- Asp.Net MVC 3【Filters(过滤器)】
这里分享MVC里的Filters(过滤器),什么是MVC里的过滤器,他的作用是什么? 过滤器的请求处理管道中注入额外的逻辑.他们提供了一个简单而优雅的方式来实现横切关注点.这个术语是指所有对应用程序的 ...
- vue filter过滤器简单应用
vue中过滤器,用于一些常见的文本格式化,用 | 来操作. 过滤器可以用在两个地方: 1.在{{}}双花括号中插入值 2.v-bind表达式中使用 <!-- 在双花括号中 --> {{ m ...
随机推荐
- centos7 install docker
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo y ...
- Spring之28:AliasRegistry&SimpleAliasRegistry
AliasRegistry接口定义了alias的基本操作. package org.springframework.core; public interface AliasRegistry { //对 ...
- 大数据架构(PB级)
1.随着互联网快速发展,数据量的快速膨胀,我们日增3000多亿数据量,因此需要针对PB级存储.几百TB的增量数据处理架构设计 2.系统逻辑划分总图: 暂不便透露 3.系统架构图: 4.大数据计算引擎我 ...
- Spring Boot开启Druid数据库监控功能
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...
- Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)
1. 利用for循环遍历整个列表 magicians = ['alice', 'dsvid', 'carolina'] # 遍历整个列表 for magician in magicians: prin ...
- python第三天---列表的魔法
# list 列表 # 中括号括起来,逗号分隔每个元素, # 列表中可以是数字字符串.列表等都可以放进去 list1 = [123, "book", "手动", ...
- WUSTOJ 1338: The minimum square sum(Java)
题目链接:1338: The minimum square sum Description Given a prime p(p<108), you are to find min{x2+y2}, ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- 文件类型分类:头文件dirent.h中定义的文件类型与linux内文件符号对应关系
头文件 dirent.h 定义了文件类型: enum{ DT_UNKNOWN = 0, //未知类型 DT_FIFO = 1, //first in, ...
- 十六、USB驱动
一.USB固件和USB传输方式 USB固件: USB固件一般不需要我们编写,在此不做程序分析. USB固件中包含USB设备的出厂信息,如厂商ID.产品ID.主版本号和次版本号等.这就是为什么当我们把U ...