ios风格的时间选择插件

更新日期: 2018-11-17阅读: 2.7k标签: 插件

1.起因

在上个项目中,客户希望时间选择插件可以是ios风格的那种,但是找了很久,发现并没有用vue的ios风格时间插件,于是自己便自己造了一个轮子.


2.插件效果



3.插件依赖以及安装使用

插件依赖于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);


4.源码查看与调试

可以在我的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中的代码.


5.实现思路

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


链接: https://www.fly63.com/article/detial/1358

对于前端开发,整理推荐好用的chrome插件或应用

向web前端开发者整理提供的chrome插件或应用:比如Postman、JSON Viewer、Page Ruler 、ChromeADB 等等

使用原生js开发插件的实现方法

作为前端开发,我们都习惯使用一些开源的插件例如jquery工具库,那么如何使用原生js来开发封装一个自己的插件呢?接下来就看一下怎么去开发一个自己的js插件,先上代码

typeahead.js_jquery input 搜索自动补全jQuery插件

jquery.typeahead.js是一款高级的自动补全jQuery插件。该自动补全插件提供超过50个配置选项和回调方法,用于完成自动补全功能,能够完成绝大部分表单自动补全的需求。

js轮播插件_轮播图js代码插件总汇

这篇文章为大家分享图片轮播插件,最全最简单最通用的 幻灯片轮播插件,pc端和移动端都可完美使用,能满足绝大部分网站的轮播需求。js轮播插件包括Swiper、slick、owl carousel2、jssor/slider 、iSlider 等

前端最常用的vscode插件集

在前端开发中,使用Visual Studio Code有哪些你常用的插件?推荐几个自己喜欢的,不带链接,自己搜索安装吧。这些都是比较实用、前端必备的插件集

浏览器插件_常用谷歌浏览器插件推荐

常用谷谷歌浏览器确实没有其它国产软件的内置功能丰富。但是 Google 浏览器的的优点恰恰就体现在拥有超简约的界面,以及支持众多强大好用的扩展程序,用户能够按照自己的喜好去个性化定制浏览器。今天我就给大家介绍几款自己常用的插件。

sublime安装插件

安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Packages目录,也可以安装package control组件,然后直接在线安装

BlockUI详细用法_Jquery中ajax加载提示插件blickUI

BlockUI 插件是用于进行AJAX操作时模拟同步传输时锁定浏览器操作。当它被激活时,它会阻止使用者与页面(或页面的一部分)进行交互,直至它被取消。BlockUI以在DOM中添加元素的方法来实现阻止用户与浏览器交互的外观和行为

vue项目中vscode格式化配置和eslint配置冲突

使用vscode开发vue项目的时候,从远端拉下一个新的项目后,安装完依赖后跑起项目时,发现直接报了一堆语法错误:包括换行、空格、单双引号、分号等各种格式问题

Vue实现一个图片懒加载插件

图片懒加载是一个很常用的功能,特别是一些电商平台,这对性能优化至关重要。今天就用vue来实现一个图片懒加载的插件。 这篇博客采用“三步走”战略——Vue.use()、Vue.direction、Vue图片懒加载插件实现,逐步实现一个Vue的图片懒加载插件。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!