一个简易的选择小时(时分秒)的插件

老规矩,先上图,效果如下所示:

图片.png

图片.png

首先引入boostrap和juqery相关

<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">  
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>、

再引入timepicker插件

<!-- 时间插件 -->
<link rel="stylesheet" type="text/css" href="css/timePicker.css">
<script type="text/javascript" src="js/jquery-timepicker.js"/>"></script>

简洁的js代码

//时间选择
        $("#beginTime").hunterTimePicker();
        $("#endTime").hunterTimePicker();

代码示例:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">  
        <!-- 小时 -->
        <link rel="stylesheet" type="text/css" href="css/timePicker.css">
    </head>

    <body>
        <div class="row">
        <div class="form-group">
            <label class="col-sm-1 control-label" for="" id="">开始时间</label>
            <div class="col-sm-5">
                <input type="text" class="form-control" id="beginTime"></input>
                <font color="red" id="validateBeginTime"></font>
            </div>
        </div>
        </div>
        
        <div class="row">
        <div class="form-group">
            <label class="col-sm-1 control-label" for="" id="">结束时间</label>
            <div class="col-sm-5">
                <input type="text" class="form-control" id="endTime" ></input>
                <font color="red" id="validateEndTime"></font>
            </div>
        </div>
         </div>
        
    </body>
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!-- 时间插件 -->
    <script type="text/javascript" src="js/jquery-timepicker.js"/>"></script>
    <script>
        //时间选择
        $("#beginTime").hunterTimePicker();
        $("#endTime").hunterTimePicker();
    </script>

</html>

附上: timePicker.css

.Hunter-time-picker{
    position: absolute;
    border: 2px solid #c9cbce;
    width: 390px;
    background: #ffffff;
    z-index: 999999;
    font-size: 0;
}
.Hunter-time-picker:before, .Hunter-time-picker:after{
    content: '';
    display: block;
    width: 0;
    height: 0;
    border-width: 10px;
    border-style: solid;
    position: absolute;
    left: 20px;
    z-index: 999999;
}
.Hunter-time-picker:before{
    border-color: transparent transparent #ffffff;
    top: -17px;
    z-index: 9999999;
}
.Hunter-time-picker:after{
    border-color: transparent transparent #c9cbce;
    top: -20px;
}
.Hunter-time-picker *{
    box-sizing: border-box;
    margin: 0 auto;
    padding: 0;
    color: #666666;
    font-family: "Microsoft YaHei";
    font-size: 14px;
}
.Hunter-time-picker ul{
    list-style: none;
}
.Hunter-time-picker ul li{
    display: inline-block;
    position: relative;
    margin: 4px;
    cursor: pointer;
}
.Hunter-time-picker p{
    font-weight: bold;
    padding: 0 4px;
    margin-top: 4px;
    margin-bottom: 10px;
}
.Hunter-time-picker .line{
    width: 340px;
    margin: 0 auto;
    margin-top: 4px;
    border-bottom: 1px solid #d8d8d8;
}

/*选择小时*/
.Hunter-time-picker .Hunter-wrap{
    position: relative;
    width: 100%;
    background: #ffffff;
    padding: 9px;
}
.Hunter-time-picker .Hunter-hour-name{
    display: inline-block;
    width: 50px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    position: relative;
    background-color: #f5f5f5;
}
.Hunter-time-picker .Hunter-hour-name:hover{
    color: #002DFF;
}

.Hunter-time-picker .Hunter-hour.active{
    z-index: 999999999;
}
.Hunter-time-picker .active .Hunter-hour-name{
    color: #ffffff;
    background-color: #3A7ADB;
}
.Hunter-time-picker .Hunter-minute-wrap{
    display: none;
    border: 1px solid #D8D8D8;
    background: #ffffff;
    position: absolute;
    top: 29px;
    width: 370px;
    padding: 10px 10px 5px 10px;
}
.Hunter-time-picker .Hunter-minute{
    width: 50px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: #999999;
    background-color: #f5f5f5;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
.Hunter-time-picker .Hunter-minute:hover{
    color: #002DFF;
}
.Hunter-time-picker .Hunter-minute.active {
    color: #ffffff;
    background-color: #3A7ADB;
}
.Hunter-time-picker .Hunter-clean-btn{
    width: 108px;
    height: 30px;
    background-color: #3A7ADB!important;
    color: #ffffff;
    background-image: none !important;
    border: 5px solid #3A7ADB;
    border-radius: 0;
}
.Hunter-time-picker .Hunter-clean-btn:hover{
    background-color: #0B4B94 !important;
    border-color: #3A7ADB;
}

附上: jquery-timepicker.js

/**
 *
 * @author  李凯飞
 *
 * 描述:选择时间组件,先选择小时再选择分钟,只支持00-23小时、00-05-10...50-55分钟。回调函数的e为源对象。
 *
 * 示例如下:
 *
 * $("#timePicker").hunterTimePicker();
 *
 * $('.time-picker').hunterTimePicker({
 *      callback: function(e){
 *          ....
 *      }
 * });
 *
 */

(function ($) {
    $.hunterTimePicker = function (box, options) {
        var dates = {
          hour: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'],
          minute: ['00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55'],
        };

        var box = $(box);

        var template = $('<div class="Hunter-time-picker" id="Hunter_time_picker"><div class="Hunter-wrap"><ul class="Hunter-wrap" id="Hunter_time_wrap"></ul></div></div>');

        var time_wrap = $('#Hunter_time_wrap', template);

        $(document).click(function() {
            template.remove();
        });

        box.click(function(event){
            event.preventDefault();
            event.stopPropagation();
            $('.Hunter-time-picker').remove();
            var _this = $(this);
            var offset = _this.offset();
            var top = offset.top + _this.outerHeight() + 15;
            template.css({
                'left': offset.left,
                'top': top
            });
            buildTimePicker();
            $('body').append(template);

            $('.Hunter-time-picker').click(function(event){
                event.preventDefault();
                event.stopPropagation();
            });
        });

        function buildTimePicker(){
            buildHourTpl();
            hourEvent();
            minuteEvent();
            cleanBtnEvent();
        };

        function buildHourTpl(){
            var hour_html = '<p>小时</p>';
            for(var i = 0; i < dates.hour.length; i++){
                var temp = box.val().split(":")[0];
                if(dates.hour[i]==temp){
                    hour_html += '<li class="Hunter-hour active" data-hour="' + dates.hour[i] +'"><ul class="Hunter-minute-wrap"></ul><div class="Hunter-hour-name">' + dates.hour[i] + '</div></li>';
                }else{
                    hour_html += '<li class="Hunter-hour" data-hour="' + dates.hour[i] +'"><ul class="Hunter-minute-wrap"></ul><div class="Hunter-hour-name">' + dates.hour[i] + '</div></li>';
                }
            }

            hour_html += '<li class="Hunter-clean"><input type="button" class="Hunter-clean-btn" id="Hunter_clean_btn" value="清 空"></li>'
            
            time_wrap.html(hour_html);
        };

        function buildMinuteTpl(cur_time){
            var poi = cur_time.position();
            var minute_html = '<p>分钟</p>';
            var temp = box.val().split(":")[1];
            for(var j = 0; j < dates.minute.length;j++){
                if(dates.minute[j]==temp){
                    minute_html += '<li class="Hunter-minute active" data-minute="' + dates.minute[j] + '">' + dates.minute[j] + '</li>';
                }else{
                    minute_html += '<li class="Hunter-minute" data-minute="' + dates.minute[j] + '">' + dates.minute[j] + '</li>';
                }
            }
            cur_time.find('.Hunter-minute-wrap').html(minute_html).css('left', '-' + (poi.left - 37) + 'px').show();
        };

        function hourEvent(){
            time_wrap.on('click', '.Hunter-hour', function(event){
                event.preventDefault();
                event.stopPropagation();
                var _this = $(this);

                time_wrap.find('.Hunter-hour').removeClass('active');
                time_wrap.find('.Hunter-hour-name').removeClass('active');
                time_wrap.find('.Hunter-minute-wrap').hide().children().remove();

                _this.addClass('active');
                _this.find('.Hunter-hour').addClass('active');

                var hourValue = _this.data('hour');
                var temp = box.val().split(":");
                if(temp.length > 1){
                    box.val(hourValue+":"+temp[1]);
                }else{
                    box.val(hourValue+":00");
                }
                buildMinuteTpl(_this);
                
                if(options.callback) options.callback(box);

                return false;
            });
        };

        function minuteEvent(){
            time_wrap.on('click', '.Hunter-minute', function(event) {
                event.preventDefault();
                event.stopPropagation();
                var _this = $(this);

                var minuteValue = _this.data('minute');
                var temp = box.val().split(":")[0]+":"+minuteValue;
                box.val(temp);
                template.remove();

                if(options.callback) options.callback(box);

                return false;
            });
        };

        function cleanBtnEvent(){
            time_wrap.on('click', '#Hunter_clean_btn', function(event){
                event.preventDefault();
                event.stopPropagation();

                box.val('');
                template.remove();
                if(options.callback) options.callback(box);
                return false;
            });
        };
    };

    $.fn.extend({
        hunterTimePicker: function (options) {
            options = $.extend({}, options);
            this.each(function () {
                new $.hunterTimePicker(this, options);
            });
            return this;
        }
    });
})(jQuery);

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏JavaEdge

2018-08-22

6420
来自专栏趣学算法

数据结构 第11讲 二叉树及其创建

二叉树(Binary Tree)是n(n≥0)个结点所构成的集合,它或为空树(n = 0);或为非空树,对于非空树T:

13420
来自专栏lgp20151222

解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00

spring-data-redis的RedisTemplate<K, V>模板类在操作redis时默认使用JdkSerializationRedisSerial...

86510
来自专栏Android机动车

数据结构——树、森林和二叉树的转换

森林是由若干棵树组成的,所以可以完全理解为,森林中的每一棵树都是兄弟,可以按照兄弟的处理办法来操作。步骤不如:

9720
来自专栏青玉伏案

算法与数据结构(十一) 平衡二叉树(AVL树)(Swift版)

今天的博客是在上一篇博客的基础上进行的延伸。上一篇博客我们主要聊了二叉排序树,详情请戳《二叉排序树的查找、插入与删除》。本篇博客我们就在二叉排序树的基础上来聊聊...

28570
来自专栏计算机视觉与深度学习基础

Leetcode 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a compl...

20770
来自专栏菩提树下的杨过

spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate

默认情况下,spring-boot的redis自动配置,只能注册一个StringRedisTemplate实例,如果希望注入多个,比如:1个读写database...

1K100
来自专栏Petrichor的专栏

leetcode: 96. Unique Binary Search Trees

9940
来自专栏用户画像

PAT 1004

A family hierarchy is usually presented by a pedigree tree. Your job is to count...

6830
来自专栏武培轩的专栏

剑指Offer-二叉树中和为某一值的路径

题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 思路 回...

33540

扫码关注云+社区

领取腾讯云代金券