selectDate.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. 名称:手势拖动选择日期-移动端
  3. 邮箱:helang.love@qq.com
  4. 作者:helang
  5. */
  6. ;$.extend({
  7. /* 年月日版 */
  8. selectDate: function (el, info, cbFn) {
  9. var createDateData = function (start, end) {
  10. var dateData = [{data: []}];
  11. var returnDayLen = function (year, month) {
  12. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  13. return 31
  14. } else if (month == 2) {
  15. if (year % 4 == 0 && year % 100 != 0) {
  16. return 29
  17. } else if (year % 400 == 0) {
  18. return 29
  19. } else {
  20. return 28
  21. }
  22. } else {
  23. return 30
  24. }
  25. };
  26. for (var x = start; x <= end; x++) {
  27. var data = {id: x, value: x + "年", childs: []};
  28. for (var y = 1; y <= 12; y++) {
  29. var dayArr = [];
  30. var len = returnDayLen(x, y);
  31. for (var z = 1; z <= len; z++) {
  32. dayArr.push({id: z, value: z + "日"})
  33. }
  34. data.childs.push({id: y, value: y + "月", childs: dayArr})
  35. }
  36. dateData[0].data.push(data)
  37. }
  38. return dateData
  39. };
  40. var infoData = {}, now = new Date();
  41. if (!info.start || !info.end || info.end < info.start) {
  42. infoData.start = now.getFullYear() - 60;
  43. infoData.end = now.getFullYear()
  44. } else {
  45. infoData.start = info.start;
  46. infoData.end = info.end
  47. }
  48. if (!info.select || info.select.length != 3) {
  49. infoData.select = [infoData.end - infoData.start, now.getMonth(), now.getDate() - 1]
  50. } else {
  51. infoData.select = [info.select[0] - infoData.start, info.select[1] - 1, info.select[2] - 1]
  52. }
  53. var selectDate = new MobileSelect({
  54. trigger: el,
  55. triggerDisplayData:false,
  56. wheels: createDateData(infoData.start, infoData.end),
  57. position: infoData.select,
  58. callback: function (item, data) {
  59. var dateInfo = {year: data[0].id, month: data[1].id, day: data[2].id};
  60. cbFn && cbFn(dateInfo)
  61. }
  62. });
  63. return selectDate;
  64. },
  65. });