资讯专栏INFORMATION COLUMN

jquery日期和时间选择插件

raledong / 2691人阅读

摘要:在做公司移动端项目的时候,需要频繁选择日期和时间,在网上搜到的大部分插件都有存在,于是自己干脆写了一个插件出来,以应对公司项目的需求。

在做公司移动端项目的时候,需要频繁选择日期和时间,在网上搜到的大部分插件都有bug存在,于是自己干脆写了一个插件出来,以应对公司项目的需求。

下面把插件源码放出来,供需要的小伙伴们复制粘贴(插件没有bug,可以放心使用,喜欢的给个赞哦(o^*^o)!!!):

插件使用方法:

</>复制代码

  1. HTML:
  2. 时间
  3. 日期
  4. js:
  5. $(function(){
  6. $(".date").pickTimer({
  7. "pickType":"y:m:d",
  8. "yearSize":"30"
  9. });
  10. $(".time").pickTimer({
  11. "pickType":"h:m:s",
  12. "speed":3
  13. });
  14. });
jquery.pickTimer.js

</>复制代码

  1. /*
  2. * pluginName:pickTimer,
  3. * author:yangyang2010cs@163.com
  4. * Individual contact method:986372959(It"s my QQ)
  5. * date:2017/09/07 18:45:00
  6. * */
  7. ;(function($,window,document,undefined){
  8. "use strict"; //严格模式,提高效率
  9. var pluginName = "pickTimer", //定义插件的名字
  10. defaults = {},//定义一个默认的参数
  11. liH,//每一个li的高度
  12. $list,//滑动列表
  13. globalThis_launchHtml,
  14. pluginThis;//指代指向plugin的this
  15. var global = {
  16. options:""
  17. };
  18. function Plugin(options){ //创建构造函
  19. //this -- Plugin对象
  20. pluginThis = this;
  21. this.options = options;
  22. this.init(); //初始化
  23. }
  24. Plugin.prototype = {
  25. init:function(){
  26. //this -- Plugin对象
  27. var str = "
    "+
  28. "
    "+
  29. "
    "+
  30. "取消"+
  31. ""+
  32. "
    "+(this.options.pickType=="y:m:d"?"设置日期":"设置时间")+"
    "+
  33. "
    "+
  34. "确定"+
  35. ""+
  36. ""+
  37. "
    "+
  38. "
    "+
  39. "
    "+
  40. "
      "+
    • "
    "+
  41. "
    "+
  42. "
    "+
  43. ""+
  44. "
    "+
  45. "
    "+
  46. "
      "+
    • "
    "+
  47. "
    "+
  48. "
    "+
  49. ""+
  50. "
    "+
  51. "
    "+
  52. "
      "+
    • "
    "+
  53. "
    "+
  54. "
    "+
  55. ""+
  56. ""+
  57. ""+
  58. "
    ";
  59. $("body").append(str);
  60. $(".pick-cancel,.pick-layer").on("click",function(){
  61. $(".touches,.pick-layer").remove();
  62. $("body").unbind("touchmove"); //恢复了body的拖动事件
  63. });
  64. $(".pick-sure").on("click",function(){
  65. var val = "";
  66. $(".pick-active").each(function(){
  67. if(pluginThis.options.pickType=="y:m:d"){
  68. val += $(this).text()+"-";
  69. } else if(pluginThis.options.pickType=="h:m:s"){
  70. val += $(this).text()+":";
  71. }
  72. });
  73. $(globalThis_launchHtml).html(val.substring(0,val.length-1));
  74. $(".touches,.pick-layer").remove();
  75. $("body").unbind("touchmove"); //恢复了body的拖动事件
  76. });
  77. $("body").on("touchmove",function (e){
  78. e.preventDefault();
  79. });
  80. this.render(); //渲染
  81. },
  82. render:function(){
  83. //this -- Plugin对象
  84. global.options = this.options;
  85. $list = $(".list");
  86. if(this.options.pickType=="h:m:s"){
  87. for(var h=0;h<24;h++) {
  88. $list.eq(0).append("
  89. " + (h >= 10 ? h : "0" + h) + "
  90. ")
  91. }
  92. for (var m = 0; m < 60; m++) {
  93. $list.eq(1).append("
  94. " + (m >= 10 ? m : "0" + m) + "
  95. ")
  96. }
  97. for (var s = 0; s < 60; s++) {
  98. $list.eq(2).append("
  99. " + (s >= 10 ? s : "0" + s) + "
  100. ")
  101. }
  102. liH = $list.find("li").eq(0).height();//li的高度
  103. var hour = new Date().getHours(),min = new Date().getMinutes(),sec = new Date().getSeconds();
  104. $list.eq(0).find("li").eq(hour).addClass("pick-active");//一开始默认第三行选中
  105. $list.eq(0).css("top",(-hour+2)*liH);
  106. $list.eq(1).find("li").eq(min).addClass("pick-active");//一开始默认第三行选中
  107. $list.eq(1).css("top",(-min+2)*liH);
  108. $list.eq(2).find("li").eq(sec).addClass("pick-active");//一开始默认第三行选中
  109. $list.eq(2).css("top",(-sec+2)*liH);
  110. } else if(this.options.pickType=="y:m:d"){
  111. var year = new Date().getFullYear(),month = new Date().getMonth();
  112. for(var _y=0;_y" +(year-Math.floor(this.options.yearSize/2)+_y)+ "")
  113. }
  114. for (var _m = 1; _m < 13; _m++) {
  115. $list.eq(1).append("
  116. " + (_m >= 10 ? _m : "0" + _m) + "
  117. ")
  118. }
  119. liH = $list.find("li").eq(0).height();//li的高度
  120. $list.eq(0).find("li").eq(year-$list.eq(0).find("li").eq(0).text()).addClass("pick-active");//一开始默认第三行选中
  121. $list.eq(0).css("top",(-(year-$list.eq(0).find("li").eq(0).text())+2)*liH);
  122. $list.eq(0).addClass("js_year");
  123. $list.eq(1).find("li").eq(month).addClass("pick-active");//一开始默认第三行选中
  124. $list.eq(1).css("top",(-month+2)*liH);
  125. $list.eq(1).addClass("js_month");
  126. this.createDate();
  127. }
  128. this.handleEvent(); //绑定事件
  129. return this;
  130. },
  131. createDate:function(){ //创建日期选择中的天数一列
  132. $list.eq(2).html("");
  133. var createDate_year = $(".js_year").find("li.pick-active").text();
  134. var createDate_month = $(".js_month").find("li.pick-active").text();
  135. if (((createDate_year % 4)==0) && ((createDate_year % 100)!=0) && createDate_month=="02"|| ((createDate_year % 400)==0) && createDate_month=="02") {
  136. //闰年 2
  137. setDateFun(29);
  138. } else if(!(((createDate_year % 4)==0) && ((createDate_year % 100)!=0)) && createDate_month=="02"|| !((createDate_year % 400)==0) && createDate_month=="02"){
  139. //非闰年 2
  140. setDateFun(28);
  141. } else if(createDate_month=="01"||createDate_month=="03"||createDate_month=="05"||createDate_month=="07"||createDate_month=="08"||createDate_month=="10"||createDate_month=="12"){
  142. setDateFun(31);
  143. } else if(createDate_month=="04"||createDate_month=="06"||createDate_month=="09"||createDate_month=="11"){
  144. setDateFun(30);
  145. }
  146. function setDateFun(len){
  147. var date = new Date().getDate();
  148. for (var _d = 1; _d <= len; _d++) {
  149. $list.eq(2).append("
  150. " + (_d >= 10 ? _d : "0" + _d) + "
  151. ")
  152. }
  153. $list.eq(2).find("li").eq(date-1).addClass("pick-active");//一开始默认第几行选中
  154. $list.eq(2).css("top",(-date+2+1)*liH);
  155. $list.eq(2).addClass("js_date");
  156. }
  157. return this;
  158. },
  159. handleEvent:function(){ //函数绑定
  160. //this -- Plugin对象
  161. $list.each(function(){
  162. var startY = null,//开始的pageY
  163. endY = null,//结束的pageY
  164. distY = null,//endY - startY
  165. cTop = null,//currentTop
  166. _top = null,//ul.list的top值
  167. timeS = null,//滚动的开始时间
  168. distT = null,//每一次滚动的时间差
  169. speed = null;//速度
  170. var SE = null;
  171. var ME = null;
  172. function startCallBack(e){
  173. //这里的this指向当前滑动的$list
  174. //这里的this指向当前滑动的$list
  175. if(e.originalEvent.touches){
  176. SE=e.originalEvent.targetTouches[0];
  177. console.log(SE)
  178. }
  179. startY = SE.pageY;
  180. cTop = $(this).position().top;
  181. timeS = new Date();
  182. }
  183. function moveCallBack(e){
  184. //这里的this指向当前滑动的$list
  185. if(e.originalEvent.touches){
  186. ME=e.originalEvent.targetTouches[0];
  187. //console.log(ME)
  188. }
  189. var scrollSpeed = pluginThis.options.speed || 2;
  190. endY = ME.pageY;
  191. distY = scrollSpeed*(endY - startY);
  192. //console.log(distY);//往下滑动是正直,往上是负值
  193. if(cTop+distY>88){//从顶部往下滑动
  194. _top = 88;
  195. } else if(cTop+distY<$(this).parent().height()-$(this).height()-88){//从底部往上滑动
  196. _top = $(this).parent().height() - $(this).height()-88;
  197. } else {//中间地方滑动
  198. _top = cTop+distY;
  199. }
  200. _top = _top - _top % liH;//取整
  201. $(this).css("top",_top);
  202. if(_top==44){
  203. $(this).find("li").eq(1).addClass("pick-active").siblings().removeClass("pick-active");
  204. } else if(_top==88){
  205. $(this).find("li").eq(0).addClass("pick-active").siblings().removeClass("pick-active");
  206. } else {
  207. $(this).find("li").eq(Math.abs(_top/liH)+2).addClass("pick-active").siblings().removeClass("pick-active");
  208. }
  209. }
  210. function endCallBack(e){
  211. //这里的this指向当前滑动的$list
  212. var $this = $(this);
  213. var dir = distY < 0 ? 1 : -1;//方向 上移为1,下移为-1
  214. distT = new Date() - timeS;
  215. speed = Math.abs(distY / distT);//单位px/ms
  216. if(speed>0.6) {
  217. /*alert(1)*/
  218. if (dir == 1 && Math.abs(_top / liH) + 3 == $(this).find("li").length) { //手指向上滑动
  219. if($this.attr("class")!="list js_date" && pluginThis.options.pickType=="y:m:d"){
  220. //判断闰年下,2月份天数的展示及其它月份天数的展示
  221. pluginThis.createDate();
  222. }
  223. return;//到底了,不能滑了
  224. } else if(dir==-1 && _top==88){ //手指向下滑动
  225. if($this.attr("class")!="list js_date" && pluginThis.options.pickType=="y:m:d"){
  226. //判断闰年下,2月份天数的展示及其它月份天数的展示
  227. pluginThis.createDate();
  228. }
  229. return;//到顶了,不能滑了
  230. }
  231. }
  232. setTimeout(function(){
  233. $this.css("top",_top);
  234. if(_top==44){
  235. $(this).find("li").eq(1).addClass("pick-active").siblings().removeClass("pick-active");
  236. } else if(_top==88){
  237. $(this).find("li").eq(0).addClass("pick-active").siblings().removeClass("pick-active");
  238. } else {
  239. $(this).find("li").eq(Math.abs(_top/liH)+2).addClass("pick-active").siblings().removeClass("pick-active");
  240. }
  241. if($this.attr("class")!="list js_date" && pluginThis.options.pickType=="y:m:d"){
  242. //判断闰年下,2月份天数的展示及其它月份天数的展示
  243. pluginThis.createDate();
  244. }
  245. },50);
  246. }
  247. $(this).off("touchstart").on("touchstart",startCallBack); //下滑开始 这里的this指向plugin对象
  248. $(this).off("touchmove").on("touchmove",moveCallBack); //滑动的时候 这里的this指向plugin对象
  249. $(this).off("touchend").on("touchend",endCallBack); //滑动完了 这里的this指向plugin对象
  250. })
  251. }
  252. };
  253. $.fn[pluginName] = function(options){
  254. //do someting
  255. $(this).click(function(){
  256. globalThis_launchHtml = this;
  257. new Plugin(options);
  258. });
  259. return this;//返回调用插件的对象,以便支持链式调用
  260. }
  261. })(jQuery,window,document);
jquery.pickTimer.css

</>复制代码

  1. .pick-container ul{
  2. margin:0;
  3. padding:0;
  4. }
  5. .pick-container ul,.pick-container li{
  6. list-style: none;
  7. }
  8. .pick-container a{
  9. text-decoration: none;
  10. }
  11. /*materialize*/
  12. .pick-container *{-webkit-tap-highlight-color:transparent;}
  13. .pick-container {
  14. position:fixed;
  15. z-index:99999999;
  16. left:0;
  17. bottom:0;
  18. width:100vw;
  19. background:#fff;
  20. margin: 0 auto;
  21. max-width: 1280px;
  22. }
  23. *, *:before, *:after {
  24. -webkit-box-sizing: inherit;
  25. box-sizing: inherit;
  26. }
  27. .pick-m0 {
  28. margin: 0;
  29. }
  30. .row:after {
  31. content: "";
  32. display: table;
  33. clear: both;
  34. }
  35. .row .col {
  36. float: left;
  37. -webkit-box-sizing: border-box;
  38. box-sizing: border-box;
  39. padding: 0 .75rem;
  40. min-height: 1px;
  41. }
  42. .row .col.s3 {
  43. width: 25%;
  44. margin-left: auto;
  45. left: auto;
  46. right: auto;
  47. }
  48. .row .col.s4 {
  49. width: 33.33333333333%;
  50. margin-left: auto;
  51. left: auto;
  52. right: auto;
  53. }
  54. .row .col.s6 {
  55. width: 50%;
  56. margin-left: auto;
  57. left: auto;
  58. right: auto;
  59. }
  60. .center, .center-align {
  61. text-align: center;
  62. }
  63. /*layer*/
  64. .pick-layer{
  65. position: fixed;
  66. top:0;
  67. bottom:0;
  68. left:0;
  69. right:0;
  70. z-index:99999998;
  71. background:rgba(0,0,0,0.4);
  72. }
  73. /*pick-timer*/
  74. .pick-timer{
  75. overflow: hidden;
  76. position:relative;
  77. border-top:1px solid #eee;
  78. height:220px;
  79. }
  80. .pick-timer .list{
  81. position:absolute;
  82. top:0;
  83. left:0;
  84. z-index:2;
  85. width:100%;
  86. transition:top .4s ease-out;
  87. }
  88. .pick-timer .list li{
  89. height:44px;
  90. line-height:44px;
  91. text-align:center;
  92. color:#666;
  93. font-size:14px;
  94. transition:all .1s ease-out;
  95. }
  96. .pick-timer .list li.pick-active{
  97. font-size:20px;
  98. color:#e02222;
  99. }
  100. .pick-timer .current{
  101. position:absolute;
  102. top:88px;
  103. left:0;
  104. z-index:1;
  105. border-top:1px solid #e02222;
  106. border-bottom:1px solid #e02222;
  107. width:100%;
  108. height:44px;
  109. background:#fff;
  110. }
  111. /*pick-head*/
  112. .pick-cancel{
  113. display: block;
  114. font-size:14px;
  115. color:#666;
  116. height:40px;
  117. line-height:40px;
  118. }
  119. .pick-sure{
  120. display: block;
  121. font-size:14px;
  122. color:#e02222;
  123. height:40px;
  124. line-height:40px;
  125. }
  126. .pick-title{
  127. font-size:14px;
  128. color:#666;
  129. height:40px;
  130. line-height:40px;
  131. }
  132. /*current-time*/
  133. .current-time:after{
  134. content:":";
  135. font-size: 14px;
  136. text-align: center;
  137. line-height: 44px;
  138. color: #666;
  139. position: absolute;
  140. top:-2px;
  141. right:0;
  142. }
  143. .current-date-right:after{
  144. content:".";
  145. color:transparent;
  146. width:5px;
  147. border-top:1px solid #999;
  148. position: absolute;
  149. top:20px;
  150. right:0;
  151. }
  152. .current-date-left:before{
  153. content:".";
  154. color:transparent;
  155. width:5px;
  156. border-top:1px solid #999;
  157. position: absolute;
  158. top:20px;
  159. left:0;
  160. }
  161. /**/
  162. .pick-bgTop{
  163. position:absolute;
  164. top:0;
  165. left:0;
  166. width:100%;
  167. height:88px;
  168. /*background: linear-gradient(#fff 0%, rgba(255, 255, 255, .85)45%, rgba(255, 255, 255, .6) 75%, rgba(255, 255, 255, .4) 100%);*/
  169. background: -webkit-gradient(linear, left top, left bottom, from(#efefef),to(rgba(255, 255, 255, .1)));
  170. z-index:1;
  171. }
  172. .pick-bgBottom{
  173. position:absolute;
  174. bottom:0;
  175. left:0;
  176. width:100%;
  177. height:87px;
  178. /*background: linear-gradient(rgba(255, 255, 255, .4) 0%, rgba(255, 255, 255, .6)25%, rgba(255, 255, 255, .85) 65%, #fff 100%);*/
  179. background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, .1)),to(#efefef));
  180. z-index:1;
  181. }
特别注意:

</>复制代码

  1. 由于在网上找到的插件都是用来触发插件,但是使用input,在ios上会出现光标和系统自带的底部带有‘完成’的确认横条,这样很难看,虽然有办法解决,但是麻烦。于是我在插件里没有使用input的value属性来获取值,而是使用了除input元素外的其它任何标签,代码原理中使用jquery中text()方法来写值。在插件使用方法中就可以看出来,现在就回到页面顶部看一看吧,我用的是

  2. ,你也可以用其他的标签。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/88426.html

相关文章

  • 前端技术 博客文章、书籍 积累

    摘要:好多编辑器例如等都支持这样的语法来快速的编写代码如何优雅地使用把标签放在结束标签之后结束标签之前的差别什么是响应式设计怎样进行 书籍 《JavaScriptDOM编程艺术》《JavaScript高级程序设计》《JavaScript框架设计》《JavaScript专家编程》《JavaScript Ninjia》《JavaScript语言精粹(修订版)》《JavaScript设计模式》《J...

    LiangJ 评论0 收藏0
  • 前端技术 博客文章、书籍 积累

    摘要:好多编辑器例如等都支持这样的语法来快速的编写代码如何优雅地使用把标签放在结束标签之后结束标签之前的差别什么是响应式设计怎样进行 书籍 《JavaScriptDOM编程艺术》《JavaScript高级程序设计》《JavaScript框架设计》《JavaScript专家编程》《JavaScript Ninjia》《JavaScript语言精粹(修订版)》《JavaScript设计模式》《J...

    codercao 评论0 收藏0
  • 前端技术 博客文章、书籍 积累

    摘要:好多编辑器例如等都支持这样的语法来快速的编写代码如何优雅地使用把标签放在结束标签之后结束标签之前的差别什么是响应式设计怎样进行 书籍 《JavaScriptDOM编程艺术》《JavaScript高级程序设计》《JavaScript框架设计》《JavaScript专家编程》《JavaScript Ninjia》《JavaScript语言精粹(修订版)》《JavaScript设计模式》《J...

    huayeluoliuhen 评论0 收藏0
  • jquery日期时间选择插件

    摘要:在做公司移动端项目的时候,需要频繁选择日期和时间,在网上搜到的大部分插件都有存在,于是自己干脆写了一个插件出来,以应对公司项目的需求。 在做公司移动端项目的时候,需要频繁选择日期和时间,在网上搜到的大部分插件都有bug存在,于是自己干脆写了一个插件出来,以应对公司项目的需求。 下面把插件源码放出来,供需要的小伙伴们复制粘贴(插件没有bug,可以放心使用,喜欢的给个赞哦(o^*^o)!!...

    missonce 评论0 收藏0

发表评论

0条评论

raledong

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<