calendar_mobile.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*日历控件手机端*/
  2. //郑子民 吴春杰 曾振峰
  3. Namespace.register("U.Control.calenm");
  4. //创建一个时间对象
  5. U.Control.calenm.Dateinit = (function () {
  6. var date = new Date();
  7. return {
  8. getDate: function () { return date; },
  9. setDate: function (date) { this.date = date; }
  10. };
  11. })();
  12. //定义需要用到的变量
  13. //触摸滑动记录x y .日期 月份 年份
  14. U.Control.calenm.old_x, U.Control.calenm.old_y, U.Control.calenm.Udate, U.Control.calenm.Umonth;
  15. U.Control.calenm.aboveY, U.Control.calenm.movelength, U.Control.calenm.startY;
  16. //触摸滑动后上一次的值 触摸滑动后移动的距离 记录div的top值(触摸事件)
  17. //加载"设置时间"
  18. //调用函数U.Control.calenm.BT 返回"设置时间"的div
  19. U.Control.calenm.BT = function () {
  20. var CB = $$("div", { "className": "U_CM_confirm" });
  21. $$("div", { "className": "U_CM_confirm_A", "innerHTML": "设置时间" }, CB);
  22. $$("div", { "className": "U_CM_confirm_B", "onclick": "U.Control.calenm.GetKJ()" }, CB);
  23. return CB;
  24. }
  25. //页面初始化
  26. U.Control.calenm.init = function () {
  27. var U_CM_Box = $$("div", { "className": "U_CM_Box" }, $("body")[0]);
  28. var U_CM_YBox = $$("div", { "className": "U_CM_IN" }, U_CM_Box);
  29. $$("div", { "className": "U_CM_Year" }, U_CM_YBox);
  30. var U_CM_MBox = $$("div", { "className": "U_CM_IN" }, U_CM_Box);
  31. $$("div", { "className": "U_CM_Month" }, U_CM_MBox);
  32. var U_CM_DBox = $$("div", { "className": "U_CM_IN" }, U_CM_Box);
  33. $$("div", { "className": "U_CM_Date" }, U_CM_DBox);
  34. var _month = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
  35. var _day = new Array(31, U.Control.calenm.ifrun(U.Control.calenm.Dateinit.getDate().getFullYear()), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  36. //三个for循环打印年月日
  37. for (var i = 1; i < _day[U.Control.calenm.Dateinit.getDate().getMonth()] + 1; i++) {
  38. $$("div", { "className": "U_CM_box", "innerHTML": i }, $(".U_CM_Date")[0]);
  39. }
  40. for (var i = 0; i < _month.length; i++) {
  41. $$("div", { "className": "U_CM_box", "innerHTML": _month[i] }, $(".U_CM_Month")[0]);
  42. }
  43. for (var i = 0; i < 100; i++) {
  44. $$("div", { "className": "U_CM_box", "innerHTML": (U.Control.calenm.Dateinit.getDate().getFullYear() - 50) + i }, $(".U_CM_Year")[0]);
  45. }
  46. U.Control.calenm.ifnow(_month);
  47. //创建按钮及功能
  48. $$("div", { "className": "U_CM_BC" }, U_CM_Box);
  49. $$("div", { "className": "U_CM_BCA" }, U_CM_Box);
  50. var U_CM_BTN = $$("div", { "className": "U_CM_btn" }, U_CM_Box);
  51. $$("div", { "className": "U_CM_Lbtn", "innerHTML": "取消" }, U_CM_BTN);
  52. $$("div", { "className": "U_CM_Rbtn", "innerHTML": "确认" }, U_CM_BTN);
  53. $(".U_CM_Rbtn").bind("click", function () {
  54. $(".U_CM_confirm_B")[0].innerHTML = $(".U_CM_select")[0].innerHTML + "年" + $(".U_CM_select")[1].innerHTML + $(".U_CM_select")[2].innerHTML + "日";
  55. $(".U_CM_Box")[0].remove();
  56. })
  57. $(".U_CM_Lbtn").bind("click", function () {
  58. $(".U_CM_Box")[0].remove();
  59. })
  60. }
  61. //判断是否为闰年
  62. U.Control.calenm.ifrun = function (a) {
  63. if ((a % 4 == 0) && (a % 100 != 0) || (a % 400 == 0)) { return 29; } else return 28;
  64. }
  65. //判断当前年份确认样式
  66. U.Control.calenm.ifnow = function (a) {
  67. var _year = U.Control.calenm.Dateinit.getDate().getFullYear();
  68. var _month = U.Control.calenm.Dateinit.getDate().getMonth();
  69. var _day = U.Control.calenm.Dateinit.getDate().getDate();
  70. //获取当前年月日并从控件中找到相应日期 赋予样式
  71. for (var i = 0; i < $(".U_CM_Date div").length; i++) {
  72. if (_day == $(".U_CM_Date div")[i].innerHTML) {
  73. $(".U_CM_Date div")[i].className += " U_CM_select";
  74. $(".U_CM_Date")[0].style.top = "-" + $(".U_CM_Date div")[i - 3].offsetTop + "px";
  75. }
  76. }
  77. for (var i = 0; i < $(".U_CM_Month div").length; i++) {
  78. if (a[_month] == $(".U_CM_Month div")[i].innerHTML) {
  79. $(".U_CM_Month div")[i].className += " U_CM_select";
  80. $(".U_CM_Month")[0].style.top = "-" + $(".U_CM_Month div")[i - 3].offsetTop + "px";
  81. }
  82. }
  83. for (var i = 0; i < $(".U_CM_Year div").length; i++) {
  84. if (_year == $(".U_CM_Year div")[i].innerHTML) {
  85. $(".U_CM_Year div")[i].className += " U_CM_select";
  86. $(".U_CM_Year")[0].style.top = "-" + $(".U_CM_Year div")[i - 3].offsetTop + "px";
  87. }
  88. }
  89. }
  90. //日历上下滑动功能
  91. U.Control.calenm.touch = function () {
  92. var UY = $(".U_CM_Year")[0];
  93. var UM = $(".U_CM_Month")[0];
  94. var UD = $(".U_CM_Date")[0];
  95. $(".U_CM_Box").bind("touchstart", function (e) {
  96. U.UF.EV.stopBubble();
  97. //获取所需值 跟清空属性
  98. U.Control.calenm.old_x = e.touches[0].pageX;
  99. U.Control.calenm.old_y = e.touches[0].pageY;
  100. switch (e.target.parentNode) {
  101. case UY: U.Control.calenm.aboveY = UY.offsetTop; UY.style.transition = ""; break;
  102. case UM: U.Control.calenm.aboveY = UM.offsetTop; UM.style.transition = ""; break;
  103. case UD: U.Control.calenm.aboveY = UD.offsetTop; UD.style.transition = ""; break;
  104. default: break;
  105. }
  106. }, false)
  107. $(".U_CM_Box").bind("touchmove", function (e) {
  108. U.UF.EV.stopBubble();
  109. //触摸滑动时改变外部div的坐标
  110. switch (e.target.parentNode) {
  111. //将样式一致 再计算移动的距离 (年月日)
  112. case UY:
  113. for (var i = 0; i < UY.children.length; i++) {
  114. UY.children[i].className = "U_CM_box"
  115. }
  116. U.Control.calenm.movelength = e.touches[0].pageY - U.Control.calenm.old_y;
  117. UY.style.top = parseInt(U.Control.calenm.aboveY + U.Control.calenm.movelength) + "px";
  118. U.Control.calenm.startY = UY.style.top;
  119. break;
  120. case UM:
  121. for (var i = 0; i < UM.children.length; i++) {
  122. UM.children[i].className = "U_CM_box"
  123. }
  124. U.Control.calenm.movelength = e.touches[0].pageY - U.Control.calenm.old_y;
  125. UM.style.top = parseInt(U.Control.calenm.aboveY + U.Control.calenm.movelength) + "px";
  126. U.Control.calenm.startY = UM.style.top;
  127. break;
  128. case UD:
  129. for (var i = 0; i < UD.children.length; i++) {
  130. UD.children[i].className = "U_CM_box"
  131. }
  132. U.Control.calenm.movelength = e.touches[0].pageY - U.Control.calenm.old_y;
  133. UD.style.top = parseInt(U.Control.calenm.aboveY + U.Control.calenm.movelength) + "px";
  134. U.Control.calenm.startY = UD.style.top;
  135. break;
  136. default: break;
  137. }
  138. }, false)
  139. $(".U_CM_Box").bind("touchend", function (e) {
  140. U.UF.EV.stopBubble();
  141. if (e.target != $(".U_CM_IN")[0] && e.target != $(".U_CM_IN")[1] && e.target != $(".U_CM_IN")[1]) {
  142. U.Control.calenm.aboveY = $(".U_CM_select")[0].offsetTop;
  143. //触摸结束后判断当前焦点的是哪个div (年月日)
  144. switch (e.target.parentNode) {
  145. //判断滑动的距离是否满足更换焦点要求否则弹回触摸时的div (年月日)
  146. case UY:
  147. U.Control.calenm.Udate = $(".U_CM_select")[1].innerHTML;
  148. U.Control.calenm.Umonth = $(".U_CM_select")[0].innerHTML;
  149. if (parseInt(U.Control.calenm.startY) > 185) {
  150. e.target.parentNode.firstChild.className += " U_CM_select";
  151. e.target.parentNode.style.cssText += "transition:0.5s all;";
  152. e.target.parentNode.style.top = 120 + "px";
  153. }
  154. for (var i = 0; i < UY.children.length; i++) {
  155. if (parseInt(U.Control.calenm.startY) >= 80 - i * 40 && parseInt(U.Control.calenm.startY) < 120 - i * 40) {
  156. e.target.parentNode.children[i].className += " U_CM_select";
  157. UY.style.top = 120 - i * 40 + "px";
  158. e.target.parentNode.style.cssText += "transition:0.5s all;";
  159. }
  160. }
  161. if (parseInt(U.Control.calenm.startY) < -3838) {
  162. e.target.parentNode.lastChild.className += " U_CM_select";
  163. e.target.parentNode.style.cssText += "transition:0.5s all;";
  164. e.target.parentNode.style.top = -3838 + "px";
  165. }
  166. U.Control.calenm.UPdata($(".U_CM_select")[0].innerHTML, "0");
  167. break;
  168. case UM:
  169. U.Control.calenm.Udate = $(".U_CM_select")[1].innerHTML;
  170. if (parseInt(U.Control.calenm.startY) > 120) {
  171. e.target.parentNode.firstChild.className += " U_CM_select";
  172. e.target.parentNode.style.cssText += "transition:0.5s all;";
  173. e.target.parentNode.style.top = 120 + "px";
  174. }
  175. for (var i = 0; i < UM.children.length; i++) {
  176. if (parseInt(U.Control.calenm.startY) >= 80 - i * 40 && parseInt(U.Control.calenm.startY) < 120 - i * 40) {
  177. e.target.parentNode.children[i].className += " U_CM_select";
  178. UM.style.top = 120 - i * 40 + "px";
  179. e.target.parentNode.style.cssText += "transition:0.5s all;";
  180. }
  181. }
  182. if (parseInt(U.Control.calenm.startY) < -319) {
  183. e.target.parentNode.lastChild.className += " U_CM_select";
  184. e.target.parentNode.style.cssText += "transition:0.5s all;";
  185. e.target.parentNode.style.top = -319 + "px";
  186. }
  187. U.Control.calenm.UPdata($(".U_CM_select")[1].innerHTML, "1");
  188. break;
  189. case UD:
  190. var YY = 1125;
  191. if (UD.children.length == 30) { YY = 1170; }
  192. else if (UD.children.length == 28) { YY = 1080 }
  193. else if (UD.children.length == 31) { YY = 1078 };
  194. if (parseInt(U.Control.calenm.startY) > 120) {
  195. e.target.parentNode.firstChild.className += " U_CM_select";
  196. e.target.parentNode.style.cssText += "transition:0.5s all;";
  197. e.target.parentNode.style.top = 120 + "px";
  198. }
  199. for (var i = 0; i < UD.children.length; i++) {
  200. if (parseInt(U.Control.calenm.startY) >= 80 - i * 40 && parseInt(U.Control.calenm.startY) < 120 - i * 40) {
  201. e.target.parentNode.children[i].className += " U_CM_select";
  202. UD.style.top = 120 - i * 40 + "px";
  203. e.target.parentNode.style.cssText += "transition:0.5s all;";
  204. }
  205. }
  206. if (parseInt(U.Control.calenm.startY) < -YY) {
  207. e.target.parentNode.lastChild.className += " U_CM_select";
  208. e.target.parentNode.style.cssText += "transition:0.5s all;";
  209. e.target.parentNode.style.top = -YY + "px";
  210. }
  211. break;
  212. default: break;
  213. }
  214. };
  215. }, false)
  216. }
  217. //当换了月份后 更新日期 a蓝色div的innerHTML b= 0 或者1 1表示日期做了拖动 0表示月份做了拖动
  218. U.Control.calenm.UPdata = function (a, b) {
  219. var UD = $(".U_CM_Date")[0];
  220. var dataa;
  221. var _month = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
  222. var _day = new Array(31, U.Control.calenm.ifrun(U.Control.calenm.Dateinit.getDate().getFullYear()), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  223. if (b == 1) {
  224. UD.innerHTML = '';
  225. for (var i = 0; i < 12; i++) {
  226. if (a == _month[i]) {
  227. dataa = i;
  228. for (var j = 1; j < _day[i] + 1; j++) {
  229. $$("div", { "className": "U_CM_box", "innerHTML": j }, UD);
  230. }
  231. }
  232. }
  233. //判断日期那一列的焦点div
  234. for (var i = 0; i < $(".U_CM_Date div").length; i++) {
  235. if (U.Control.calenm.Udate == $(".U_CM_Date div")[i].innerHTML) {
  236. $(".U_CM_Date div")[i].className += " U_CM_select";
  237. UD.style.top = "-" + $(".U_CM_Date div")[i - 3].offsetTop + "px"; break;
  238. }
  239. else if (_month[dataa] == "二月" && U.Control.calenm.Udate == 31 || _month[dataa] == "二月" && U.Control.calenm.Udate == 30) {
  240. if (UD.lastChild.innerHTML == "29") {
  241. UD.lastChild.className += " U_CM_select";
  242. UD.style.top = "-" + $(".U_CM_Date div")[28 - 3].offsetTop + "px"; break;
  243. }
  244. UD.lastChild.className += " U_CM_select";
  245. UD.style.top = "-" + $(".U_CM_Date div")[29 - 3].offsetTop + "px"; break;
  246. }
  247. if (_month[dataa] == "二月" && (UD.lastChild.innerHTML == "31" || UD.lastChild.innerHTML == "30")) {
  248. UD.lastChild.className += " U_CM_select";
  249. UD.style.top = "-" + $(".U_CM_Date div")[28 - 3].offsetTop + "px";
  250. }
  251. else if (U.Control.calenm.Udate == "31") {
  252. UD.lastChild.className += " U_CM_select";
  253. UD.style.top = "-" + $(".U_CM_Date div")[29 - 3].offsetTop + "px";
  254. }
  255. }
  256. }
  257. else {
  258. //判断月份那一列的焦点div
  259. for (var i = 0; i < $(".U_CM_Month div").length; i++) {
  260. if (U.Control.calenm.Umonth == "二月") {
  261. var Ua = U.Control.calenm.ifrun($(".U_CM_select")[0].innerHTML);
  262. if (Ua == 28) {
  263. UD.innerHTML = '';
  264. for (var i = 1; i < 28 + 1; i++) {
  265. $$("div", { "className": "U_CM_box", "innerHTML": i }, UD);
  266. }
  267. UD.lastChild.className += " U_CM_select";
  268. UD.style.top = "-" + $(".U_CM_Date div")[27 - 3].offsetTop + "px";
  269. }
  270. else {
  271. UD.innerHTML = '';
  272. for (var i = 1; i < 29 + 1; i++) {
  273. $$("div", { "className": "U_CM_box", "innerHTML": i }, UD);
  274. }
  275. UD.lastChild.className += " U_CM_select";
  276. UD.style.top = "-" + $(".U_CM_Date div")[28 - 3].offsetTop + "px";
  277. }
  278. }
  279. }
  280. }
  281. }
  282. //回调函数
  283. U.Control.calenm.GetKJ = function () {
  284. if ($(".U_CM_Box")[0]) { return }
  285. U.Control.calenm.init();
  286. U.Control.calenm.touch();
  287. }