在上个项目中,客户希望时间选择插件可以是ios风格的那种,但是找了很久,发现并没有用vue的ios风格时间插件,于是自己便自己造了一个轮子.
插件依赖于better-scroll和vue,安装流程如下:
step1: npm install vue -D
step2: npm install better-scroll -D
step3: npm install vue-ios-timer -D
step4: import vueIosTimer from 'vue-ios-timer';
step5: vue.use(vueIosTimer);
可以在我的Github上查看源码,或者已经下载过插件的同学可以在node_modules/vue-ios-timer/src/packages/timer.vue中查看,需要调试源码的同学可以将node_modules/package.json中main的value值改为src/packages/index.js,然后正常使用,其运行的代码便是node_modules/vue-ios-timer/src/packages/timer.vue中的代码.
1) 生成年,月,日,时,分,五个数组,根据插件属性type(可选值有date,datetime,time)初始化timeList二维数组,并初始化初始值;
initBasicData(){
for(let i=1900; i<=2100; i++){
this.years.push(i+'年');
}
for(let i=0; i<60; i++){
if(i>0 && i<=12){
this.monthes.push(String(i).padStart(2,'0')+'月');
}
if(i>0 && i<=31){
this.days.push(String(i).padStart(2,'0')+'日');
}
if(i<24){
this.hours.push(String(i).padStart(2,'0')+'时');
}
this.minutes.push(String(i).padStart(2,'0')+'分');
}
// 当type=date并且有默认值时
if(this.type == 'date' && this.datex){
let y = new Date(this.datex).getFullYear();
let m = new Date(this.datex).getMonth();
let d = new Date(this.datex).getDate();
this.timerSelectIndex = [y-1900, m, d-1];
// 当type=datetime并且有默认值
}else if(this.type == 'datetime' && this.datetimex){
let y = new Date(this.datetimex).getFullYear();
let m = new Date(this.datetimex).getMonth();
let d = new Date(this.datetimex).getDate();
let h = new Date(this.datetimex).getHours();
let min= new Date(this.datetimex).getMinutes();
this.timerSelectIndex = [y-1900, m, d-1, h, min];
// 当type=time并且有默认值
}else if(this.type == 'time' && this.timex){
let h = Number(this.timex.split(':')[0]);
let min= Number(this.timex.split(':')[1]);
this.timerSelectIndex = [h, min];
}else{
// 当没有默认值的时候
this.timerSelectIndex = [0,0,0,0,0];
}
},
initTimeList(){
if(this.type == 'datetime'){
this.timeList.push(this.years);
this.timeList.push(this.monthes);
this.timeList.push(this.days);
this.timeList.push(this.hours);
this.timeList.push(this.minutes);
}else if(this.type == 'time'){
this.timeList.push(this.hours);
this.timeList.push(this.minutes);
}else {
this.timeList.push(this.years);
this.timeList.push(this.monthes);
this.timeList.push(this.days);
}
},
2) 有了基础数据,通过better-scroll初始化滚动列表,better-scroll可以开启wheel选项,实现多列表的滚动交互,而后我们每个月的天数是有可能不一样的,所以需要动态的改变日数组,改变的时机应该是当年份列表滚动或者月份列表滚动结束的时候;
initScroll(){
// 循环初始化多个列表
if(!this.$refs.timerWrapper){
return
};
let timerWrapper = this.$refs.timerWrapper;
for(let i=0; i<timerWrapper.children.length; i++){
let wheel = new Bscroll(timerWrapper.children[i],{
wheel : {
rotate : 25,
selectedIndex : this.timerSelectIndex[i],
wheelWrapperClass : 'wheel-scroll',
wheelItemClass : 'wheel-item'
},
probeType : 3
});
this.wheels.push(wheel);
}
// 监听scrollEnd事件,当滚动结束以后,重新渲染天这一列
this.wheels.forEach((wheel,i)=>{
wheel.on('scrollEnd',(pos)=>{
if((this.type == 'date' || this.type == 'datetime') && i != 2){
let year = 1900 + this.wheels[0].getSelectedIndex();
let month = this.wheels[1].getSelectedIndex()+1;
let newDays = this.getDays(Number(year),Number(month));
this.$set(this.timeList,2, newDays);
this.wheels[2].refresh();
}
})
})
},
getDays(year,month){
// 根据年份和月份得到当月的天数
let isLeapYear = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
let bigMonthes = [1,3,5,7,8,10,12];
let isBigMonth = bigMonthes.indexOf(month) > -1;
let days = [];
for(let i=1; i<=31; i++){
days.push(String(i).padStart(2,"0")+'日');
};
if(isBigMonth){
return days;
}else if(isLeapYear && month == 2){
return days.splice(1,29);
}else if(!isLeapYear && month == 2){
return days.splice(1,28);
}else{
return days.splice(1,30);
}
}
3) 当用户所有的滚动操作结束以后,这时候需要通过发送getTime事件将选择结果暴露出去;
getIndex(){
// 返回选中的值
let indexes = [],result = '';
this.wheels.forEach(wheel=>{
indexes.push(wheel.getSelectedIndex())
});
if(indexes.length == 3 || indexes.length == 5){
indexes = indexes.map((item,i)=>{
if(i==0){
item = 1900 + item;
}else if(i==1 || i==2){
item = String(item+1).padStart(2,'0');
}else{
item = String(item).padStart(2,'0');
}
return item;
})
}else{
indexes = indexes.map((item,i)=>{
item = String(item).padStart(2,'0');
return item;
})
}
if(indexes.length == 2){
result = indexes.join(':');
}else if(indexes.length == 3){
result = indexes.join('-');
}else{
result = `${indexes[0]}-${indexes[1]}-${indexes[2]} ${indexes[3]}:${indexes[4]}`;
}
this.showTimer = false;
this.$emit('getTime',result);
}
更多实现细节可以去看完整源码,以上。来源:https://www.cnblogs.com/024-faith/archive/2018/11/16/ios-timer.html
随着SOA的普及,各大互联网公司都拥有自己较为成熟的分布式跟踪系统,比如Google的Dapper,Twitter的Zipkin,淘宝的鹰眼,京东的Hydra,eBay的CAL,大众点评的CAT等等:
工欲善其事必先利其器,软件工程师每天打交道最多的可能就是编辑器了。入行几年来,先后折腾过的编辑器有 EditPlus、UltraEdit、Visual Studio、EClipse、WebStorm、Vim、SublimeText、Atom、VSCode,现在仍高频使用
AST(Abstract Syntax Tree, AST)抽象语法树,可以把代码转译成语法树的表现形式,大多数编译器的工作过程可以分为三部分:Parse(解析),Transform(转换),Generate(代码生成)
Plugin(插件) 是 webpack 生态的的一个关键部分。它为社区提供了一种强大的方法来扩展 webpack 和开发 webpack 的编译过程。这篇文章将尝试探索 webpack plugin
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
图片懒加载是一个很常用的功能,特别是一些电商平台,这对性能优化至关重要。今天就用vue来实现一个图片懒加载的插件。 这篇博客采用“三步走”战略——Vue.use()、Vue.direction、Vue图片懒加载插件实现,逐步实现一个Vue的图片懒加载插件。
EXIF(Exchangeable Image File)是“可交换图像文件”的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数、缩略图及其他属性信息,简单来说,Exif信息是镶嵌在 JPEG/TIFF 图像文件格式内的一组拍摄参数
先安装jquer插件,命令运行: npm i jquery --save-dev ;利用 expose-loader 模块 来引用jquery插件 ,命令运行: npm i expose-loade --save-dev;配置webpack.base.conf
本文介绍了目前前端开发最受欢迎的开发工具 VSCode 必装的 10 个开发插件,用于大大提高软件开发的效率。VSCode(Visual Studio Code)是由微软研发的一款免费、开源的跨平台文本(代码)编辑器,算是目前前端开发几乎完美的软件开发工具
向web前端开发者整理提供的chrome插件或应用:比如Postman、JSON Viewer、Page Ruler 、ChromeADB 等等
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!