上图:

1.derective代码

  1. .directive('monthPicker', function ($ionicScrollDelegate) {
  2. return {
  3. restrict: 'E',
  4. templateUrl: 'component/tab_bill/month-picker.html',
  5. scope: {
  6. localDate: '='
  7. },
  8. link: function ($scope) {
  9. var yearIndex = 0, year = new Date().getFullYear() + 2, localYear = new Date().getFullYear(),
  10. localMonth = month = new Date().getMonth() + 1, localDay = new Date().getDate();
  11. var monthIndex = 0, month = new Date().getMonth() + 1;
  12. if (month < 10) {
  13. month = '0' + month
  14. }
  15. if (localMonth < 10) {
  16. localMonth = '0' + localMonth;
  17. }
  18. if (localMonth < 10) {
  19. localDay = '0' + localDay;
  20. }
  21. var dayIndex = 0;
  22. $scope.years = []; // 年数组
  23. $scope.months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
  24. $scope.days = [];
  25. var spar = {
  26. 28: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28'],
  27. 29: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29'],
  28. 30: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'],
  29. 31: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']
  30. };
  31. // $scope.hasDate = false; // 是否选择需要选择日期
  32. $scope.changeFlag = 1; // 1是开始时间,2是结束时间
  33. for (var i = 2000; i <= year; i++) {
  34. $scope.years.push(i)
  35. }
  36. var loop1, loop2, loop3;
  37. var checkTag = 0, checkTag2 = 0, checkTag3 = 0;
  38. var _endDay, _endYear, _endMonth;
  39.  
  40. $scope.goChoose = function () {
  41. $scope.blockFlag = true;
  42. setTimeout(function () {
  43. _initScroll()
  44. },300)
  45. };
  46.  
  47. $scope.$on('initPicker', function (scope, unReset) {
  48. if (!unReset) {
  49. $scope.localDate = angular.extend($scope.localDate, {
  50. startYear: localYear,
  51. startMonth: localMonth,
  52. startDay: localDay,
  53. endYear: localYear,
  54. endMonth: localMonth,
  55. endDay: localDay
  56. });
  57. _initScroll();
  58. return;
  59. }
  60.  
  61. _endDay = $scope.localDate.endDay;
  62. _endYear = $scope.localDate.endYear;
  63. _endMonth = $scope.localDate.endMonth;
  64. _initScroll();
  65. });
  66.  
  67. $scope.check1 = function () {
  68. clearInterval(loop1);
  69. if (checkTag < 0) {
  70. return false;
  71. }
  72. checkTag = 0;
  73. loop1 = setInterval(function () {
  74. checkTag++;
  75. if (checkTag > 2) {
  76. chooseYear();
  77. checkTag = -1;
  78. }
  79. }, 100)
  80. };
  81. $scope.check2 = function () {
  82. clearInterval(loop2);
  83. if (checkTag2 < 0) {
  84. return false;
  85. }
  86. checkTag2 = 0;
  87. loop2 = setInterval(function () {
  88. checkTag2++;
  89. if (checkTag2 > 2) {
  90. chooseMonth();
  91. checkTag2 = -1;
  92. }
  93. }, 100)
  94. };
  95. $scope.check3 = function () {
  96. clearInterval(loop3);
  97. if (checkTag3 < 0) {
  98. return false;
  99. }
  100. checkTag3 = 0;
  101. loop3 = setInterval(function () {
  102. checkTag3++;
  103. if (checkTag3 > 2) {
  104. chooseDay();
  105. checkTag3 = -1;
  106. }
  107. }, 100)
  108. };
  109. $scope.changeType = function () {
  110. if ($scope.hasDate) {
  111. $scope.hasDate = !$scope.hasDate;
  112. $scope.blockFlag = false;
  113. $scope.localDate = angular.extend($scope.localDate, {
  114. startYear: '',
  115. startMonth: '',
  116. startDay: '',
  117. endYear: '',
  118. endMonth: '',
  119. endDay: ''
  120. });
  121. return;
  122. }
  123. $scope.hasDate = !$scope.hasDate;
  124. if ($scope.hasDate) {
  125. $scope.localDate = angular.extend($scope.localDate, {
  126. startYear: localYear,
  127. startMonth: localMonth,
  128. startDay: localDay,
  129. endYear: localYear,
  130. endMonth: localMonth,
  131. endDay: localDay
  132. });
  133. $scope.days = spar[getDaysInOneMonth(localYear, localMonth)];
  134. setTimeout(function () {
  135. _initScroll()
  136. },100)
  137. } else {
  138. $scope.localDate.startYear = $scope.localDate.endYear;
  139. $scope.localDate.startMonth = $scope.localDate.endMonth;
  140. $scope.localDate.startDay = $scope.localDate.endDay;
  141. _initScroll()
  142. }
  143. };
  144. $scope.setDate = function () {
  145. if ($scope.changeFlag === 1) {
  146. $scope.localDate.startYear = $scope.years[yearIndex];
  147. $scope.localDate.startMonth = $scope.months[monthIndex];
  148. if (!$scope.hasDate) {
  149. $scope.localDate.endYear = $scope.years[yearIndex];
  150. $scope.localDate.endMonth = $scope.months[monthIndex];
  151. $scope.localDate.startDay = '01';
  152. $scope.localDate.endDay = getDaysInOneMonth($scope.years[yearIndex], $scope.months[monthIndex]);
  153. } else {
  154. $scope.localDate.startDay = $scope.days[dayIndex];
  155. }
  156. } else {
  157. $scope.localDate.endYear = $scope.years[yearIndex];
  158. $scope.localDate.endMonth = $scope.months[monthIndex];
  159. $scope.localDate.endDay = $scope.days[dayIndex];
  160. }
  161. console.log($scope.localDate)
  162. };
  163.  
  164. $scope.setTime = function (num, _year, _month, _day) {
  165. $scope.changeFlag = num;
  166. if (!_year) {
  167. _year = $scope.localDate.startYear;
  168. _month = $scope.localDate.startMonth;
  169. _day = $scope.localDate.startDay;
  170. }
  171. $ionicScrollDelegate.$getByHandle('scroll1').scrollTop();
  172. $ionicScrollDelegate.$getByHandle('scroll2').scrollTop();
  173. $ionicScrollDelegate.$getByHandle('scroll3').scrollTop();
  174. _animation(_year, _month, _day)
  175. };
  176. function chooseYear() {
  177. var top = $ionicScrollDelegate.$getByHandle('scroll1').getScrollPosition().top;
  178. yearIndex = (top / 44).toFixed(0);
  179. $ionicScrollDelegate.$getByHandle('scroll1').scrollTo(0, yearIndex * 44);
  180. _initDate();
  181. $scope.setDate();
  182. setTimeout(function () {
  183. checkTag = 0;
  184. clearInterval(loop1);
  185. $scope.$apply();
  186. }, 100)
  187. }
  188.  
  189. function chooseMonth() {
  190. var top = $ionicScrollDelegate.$getByHandle('scroll2').getScrollPosition().top;
  191. monthIndex = (top / 44).toFixed(0);
  192. $ionicScrollDelegate.$getByHandle('scroll2').scrollTo(0, monthIndex * 44);
  193. _initDate();
  194. $scope.setDate();
  195. setTimeout(function () {
  196. checkTag2 = 0;
  197. clearInterval(loop2);
  198. $scope.$apply();
  199. }, 100)
  200. }
  201.  
  202. function chooseDay() {
  203. var top = $ionicScrollDelegate.$getByHandle('scroll3').getScrollPosition().top;
  204. dayIndex = Number((top / 44).toFixed(0));
  205. $ionicScrollDelegate.$getByHandle('scroll3').scrollTo(0, dayIndex * 44);
  206. $scope.setDate();
  207. setTimeout(function () {
  208. checkTag3 = 0;
  209. clearInterval(loop3);
  210. $scope.$apply();
  211. }, 100)
  212. }
  213.  
  214. function _animation(year, month, day) {
  215. $ionicScrollDelegate.$getByHandle('scroll1').scrollTop();
  216. $ionicScrollDelegate.$getByHandle('scroll2').scrollTop();
  217. for (var i = 0; i < $scope.years.length; i++) {
  218. if ($scope.years[i] == year) {
  219. yearIndex = i;
  220. $ionicScrollDelegate.$getByHandle('scroll1').scrollTo(0, yearIndex * 44);
  221. }
  222. }
  223. for (var j = 0; j < 12; j++) {
  224. if ($scope.months[j] == month) {
  225. monthIndex = j;
  226. $ionicScrollDelegate.$getByHandle('scroll2').scrollTo(0, monthIndex * 44);
  227. }
  228. }
  229. if (day) {
  230. for (var k = 0; k < 32; k++) {
  231. if ($scope.days[k] == day) {
  232. dayIndex = k;
  233. $ionicScrollDelegate.$getByHandle('scroll3').scrollTo(0, dayIndex * 44);
  234. }
  235. }
  236. }
  237. }
  238.  
  239. function _initDate() {
  240. var sparNum = getDaysInOneMonth($scope.years[yearIndex], $scope.months[monthIndex]);
  241. $scope.days = spar[sparNum];
  242. if (dayIndex + 1 > sparNum) {
  243. for (var k = 0; k < 32; k++) {
  244. if ($scope.days[k] == sparNum) {
  245. dayIndex = k;
  246. $ionicScrollDelegate.$getByHandle('scroll3').scrollTo(0, dayIndex * 44);
  247. }
  248. }
  249. }
  250. }
  251.  
  252. function getDaysInOneMonth(year, month) {
  253. month = parseInt(month, 10);
  254. var d = new Date(year, month, 0);
  255. return d.getDate();
  256. }
  257.  
  258. function _initScroll() {
  259. if (!$scope.blockFlag) {
  260. return;
  261. }
  262. $scope.changeFlag = 1;
  263.  
  264. if ($scope.hasDate) {
  265. _animation($scope.localDate.startYear, $scope.localDate.startMonth, $scope.localDate.startDay)
  266. return
  267. }
  268.  
  269. if (_endDay) {
  270. $scope.localDate = angular.extend($scope.localDate, {
  271. startYear: '',
  272. startMonth: '',
  273. startDay: '',
  274. endYear: '',
  275. endMonth: '',
  276. endDay: ''
  277. });
  278. _animation(_endYear,_endMonth);
  279. return;
  280. }
  281. _animation(localYear,month)
  282.  
  283. }
  284. }
  285. };
  286. }
  287. );

  2.html代码

  1. <div class="switch-btn-zx" ng-click="changeType()" ng-if="blockFlag">
  2. {{hasDate?'按日选择':'按月选择'}}
  3. </div>
  4. <div class="switch-btn-zx" ng-click="goChoose()" ng-if="!blockFlag">
  5. 未筛选
  6. </div>
  7. <div id="month-picker" ng-if="blockFlag">
  8. <div class="title" ng-if="!hasDate">{{localDate.endYear?localDate.endYear + '-' + localDate.endMonth:localDate.endDay + '-' + localDate.startMonth}}</div>
  9. <div class="title1" ng-if="hasDate">
  10. <span ng-click="setTime(1,localDate.startYear,localDate.startMonth,localDate.startDay)" ng-class="{'active': changeFlag == 1}">
  11. {{localDate.startYear + '-' + localDate.startMonth + '-' + localDate.startDay}}
  12. </span>
  13. <span></span>
  14. <span ng-click="setTime(2,localDate.endYear,localDate.endMonth,localDate.endDay)" ng-class="{'active': changeFlag == 2}">
  15. <span ng-if="localDate.endYear" style="border-bottom:0" >{{localDate.endYear + '-' + localDate.endMonth + '-' + localDate.endDay}}</span>
  16. <span ng-if="!localDate.endYear" style="color:#666;border-bottom:0">结束日期</span>
  17. </span>
  18. </div>
  19. <div class="select-container">
  20. <div class="hook">
  21. <ion-scroll scrollbar-y="false" delegate-handle="scroll1" on-scroll="check1()">
  22. <ul>
  23. <li ng-repeat="year in years">{{year}}年</li>
  24. </ul>
  25. </ion-scroll>
  26. <div class="picker-center-highlight"></div>
  27. </div>
  28. <div class="hook">
  29. <ion-scroll scrollbar-y="false" delegate-handle="scroll2" on-scroll="check2()">
  30. <ul>
  31. <li ng-repeat="month in months">{{month}}月</li>
  32. </ul>
  33. </ion-scroll>
  34. <div class="picker-center-highlight"></div>
  35. </div>
  36. <div class="hook" ng-if="hasDate">
  37. <ion-scroll scrollbar-y="false" delegate-handle="scroll3" on-scroll="check3()">
  38. <ul>
  39. <li ng-repeat="day in days">{{day}}日</li>
  40. </ul>
  41. </ion-scroll>
  42. <div class="picker-center-highlight"></div>
  43. </div>
  44. </div>
  45. </div>

  3.scss代码

  1. #month-picker {
  2. width: 5.4rem;
  3. height: (378px/2);
  4. border-radius: 0.1rem;
  5. padding: 6px 6px 0px 6px;
  6. background: rgba(72, 128, 237, 0.06);
  7. position: relative;
  8. .title {
  9. height: 50px;
  10. line-height: 50px;
  11. border-bottom: 1px solid #4880ED;
  12. color: #4880ED !important;
  13. text-align: center;
  14. font-size: 0.4rem !important;
  15. }
  16. .title1 {
  17. height: 50px;
  18. line-height: 50px;
  19. color: #aaa !important;
  20. text-align: center;
  21. font-size: 0.4rem !important;
  22. display: -webkit-flex;
  23. display: flex;
  24. span:first-child, span:last-child {
  25. -webkit-flex: 1;
  26. flex: 1;
  27. }
  28. span:nth-child(2) {
  29. width: 0.5rem;
  30. font-size: 0.28rem;
  31. }
  32. .active {
  33. color:#4880ED !important;
  34. font-weight: 500;
  35. border-bottom: 1px solid #4880ED;
  36. }
  37. }
  38. .select-container {
  39. display: -webkit-flex;
  40. display: flex;
  41. position: relative;
  42. .hook {
  43. -webkit-flex: 1;
  44. flex: 1;
  45. height: (44*3px);
  46. position: relative;
  47. z-index: 33!important;
  48. }
  49. ion-scroll {
  50. width: 100%;
  51. height: 100%;
  52. z-index: 1!important;
  53. .scroll {
  54. z-index: 1!important;
  55. }
  56. ul {
  57. padding: 44px 0;
  58. background: rgba(72, 128, 237, 0.12);
  59. }
  60. li {
  61. height: 44px;
  62. text-align: center;
  63. line-height: 44px;
  64. font-size: 0.4rem;
  65. color: #000;
  66. }
  67. }
  68. }
  69. .picker-center-highlight {
  70. position: absolute;
  71. bottom: 0;
  72. left: 0;
  73. width: 100%;
  74. height: 132px;
  75. -webkit-box-sizing: border-box;
  76. -moz-box-sizing: border-box;
  77. box-sizing: border-box;
  78. margin-top: 0;
  79. pointer-events: none;
  80. z-index: 3333;
  81. &:after {
  82. content: '';
  83. width: 100%;
  84. height: 44px;
  85. position: absolute;
  86. left: 0;
  87. top: 50%;
  88. margin-top: -22px;
  89. z-index: 33333;
  90. background-image: -webkit-linear-gradient(top, #d0d0d0, #d0d0d0, transparent, transparent), -webkit-linear-gradient(bottom, #d0d0d0, #d0d0d0, transparent, transparent);
  91. background-image: linear-gradient(180deg, #d0d0d0, #d0d0d0, transparent, transparent), linear-gradient(0deg, #d0d0d0, #d0d0d0, transparent, transparent);
  92. background-position: top, bottom;
  93. background-size: 100% 1px;
  94. background-repeat: no-repeat;
  95. }
  96. &:before {
  97. height: 100%;
  98. margin: 0 auto;
  99. z-index: 333333;
  100. background-image: -webkit-linear-gradient(top, hsla(0, 0%, 100%, .8), hsla(0, 0%, 100%, .4)), -webkit-linear-gradient(bottom, hsla(0, 0%, 100%, .8), hsla(0, 0%, 100%, .4));
  101. background-image: linear-gradient(180deg, hsla(0, 0%, 100%, .8), hsla(0, 0%, 100%, 0.4)), linear-gradient(0deg, hsla(0, 0%, 100%, .8), hsla(0, 0%, 100%, .4));
  102. background-position: top , bottom;
  103. background-size: 100% 44px;
  104. background-repeat: no-repeat;
  105. position: absolute;
  106. left: 0;
  107. top: 0;
  108. content: '';
  109. width: 100%;
  110. }
  111. }
  112. }
  113.  
  114. .switch-btn-zx {
  115. width: 1.74rem;
  116. height: 0.48rem;
  117. border-radius: 1rem;
  118. background: rgba(72, 128, 237, 0.06);
  119. border: 1px solid #4880ED;
  120. font-size: 0.24rem;
  121. color: #4880ED;
  122. padding: 0.08rem 0 0.13rem 0.2rem;
  123. background: url('../img/switch.png') no-repeat 1.23rem center;
  124. background-size: 0.32rem 0.32rem;
  125. margin-bottom: 0.2rem;
  126. }

  4.controller用法

  1.  $scope.localDate = {}; // 初始化时间选择插件对象
     $scope.$broadcast('initPicker', true); // 需要手动初始化的时候调用一下广播。
  2.  
  3. iosscroll的层级会有问题,我在里面初始化了两次后显示正常。现在还找不到原因。
    ionicscroll指令在移动很短距离的时候不会触发scroll-complete事件。所以用了一个定时器在on-scroll事件上去计算值,然后再计算移动距离。
    有想法的童鞋可以指导改进一下。

ionic1滑动时间选择器的更多相关文章

  1. 移动端material风格日期时间选择器

    原文 好多时候在移动端需要一个的日期选择器,由于在应用上有可能应用各种框架库(Vue.js, React.js, zepto.js等):所以说一个无依赖的,这样易于上层进行封装.直接开门见山,先来张动 ...

  2. android 可以精确到秒级的时间选择器

    android自带的时间选择器只能精确到分,但是对于某些应用要求选择的时间精确到秒级,此时只有自定义去实现这样的时间选择器了.下面介绍一个可以精确到秒级的时间选择器. 先上效果图: 下面是工程目录: ...

  3. 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)

    转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...

  4. 移动端lCalendar纯原生js日期时间选择器

    网上找过很多的移动端基于zepto或jquery的日期选择器,在实际产品中也用过一两种,觉得都不太尽如人意,后来果断选择了H5自己的日期input表单,觉得还可以,至少不用引用第三方插件了,性能也不错 ...

  5. ClockPicker – 时钟风格 Bootstrap 时间选择器

    ClockPicker 是国内前端开发者发布的一个时钟样式 Timepicker,可以用于 Bootstrap 和 jQuery.所有主流浏览器都支持,包括 IE9+,支持移动设备,能够在触摸屏设备很 ...

  6. Android课程---日历选择器和时间选择器

    package com.hanqi.test5; import android.os.Bundle; import android.support.annotation.IdRes; import a ...

  7. 分享一下我封装iOS自定义控件的体会,附上三个好用的控件Demo <时间选择器&多行输入框&日期选择器>

    前段时间有小伙伴问到我:"这样的控件该怎么做呢?",我感觉是个比较简单的控件,可能对于入行不久的同志思路没有很清晰吧.趁着最近工作不忙,就来这里分享一下我封装自定义控件的几点体会吧 ...

  8. bootstrap 时间选择器 datetime

    $("'#datetimepicker").datetimepicker({ format: "yyyy-mm-dd hh:ii:ss",//设置时间格式,默认 ...

  9. Android日期时间选择器实现以及自定义大小

    本文主要讲两个内容:1.如何将DatePicker和TimePicker放在一个dialog里面:2.改变他们的宽度: 问题1:其实现思路就是自定义一个Dialog,然后往里面同时放入DatePick ...

随机推荐

  1. PHP-问题处理Fatal error: Uncaught Error: Call to undefined function simplexml_load_file()

    1.问题 今天重新安装了ubuntu,PHP,MySQL,Apache,到测试CMS项目时发生一个错误: Fatal error: Uncaught Error: Call to undefined ...

  2. [U3D Demo] 手机FPS射击游戏

    游戏截图 使用插件 DOTween Easy Touch UGUI 游戏介绍 游戏使用C#开发,是在<Unity3D手机游戏开发>一书第3章游戏的基础上优化和修改的. 机枪镭射光线和枪口特 ...

  3. Linux零基础入门第四课

    根据直播讲义整理的内容,从第四课开始.前三课的内容若后面有精力会一并整理进来. 文件的基本操作(上) 创建.删除.复制.移动和重命名 touch命令创建文件 语法 >$ touch file0 ...

  4. 优化实现Mobile/Bumped Diffuse

    在上一篇帖子的基础上增加一张法线贴图即可: Shader "James/Scene/Bumped_Diffuse" { Properties { _MainTex ("B ...

  5. 前后端分离springmvc和RESTful理解

    1. 理解MVC MVC是一种经典的设计模式,全名为Model-View-Controller,即模型-视图-控制器. 其中,模型是用于封装数据的载体,例如,在Java中一般通过一个简单的POJO(P ...

  6. GRE tunnel 2

    1.GRE简介 通用路由封装协议GRE(Generic Routing Encapsulation)可以对某些网络层协议(如IPX.ATM.IPv6.AppleTalk等)的数据报文进行封装,使这些被 ...

  7. 使用for of循环遍历获取的nodeList,配置babel编译,webpack打包之后在iphone5下报错

    报错信息: Symbol.iterator is not a function [duplicate] 代码示例: function insertCta() { let ctaArr = Array. ...

  8. A股滚动净利润增速最高排名

    最近2年(共8个季度)的滚动净利润都在增长,且平均增速超过10%. 计算举例:滚动净利润增速 = ((2018Q1 到 2018Q4的净利润之和) / (2017Q4 到 2018Q3的净利润之和) ...

  9. FastJSON 设置默认参数,全局配置方式 和 一些使用方式、坑

    如果要被序列化的对象含有一个date属性或者多个date属性按照相同的格式序列化日期的话,那我们可以使用下面的语句实现: 在应用的的Main方法体里配置全局参数: JSONObject.DEFFAUL ...

  10. chrome自动填表会遮挡input中背景图的问题解决方法

    在做某项目登录界面时,发现用户密码框在Chrome自动填充时,input中的背景框会被遮住.网上也搜了一下,没有一个有效的解决方法. 来看csdn的登录界面,也有这个问题. 后来在浏览网页时,无意中发 ...