calendar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //日历PC命名空间
  2. Namespace.register("U.Control.calen");
  3. //日历PC 田海涛 陈裕豪 陈卓锐
  4. //调用函数:U.Control.calen.A()
  5. //返回参数:divs 日历整体
  6. U.Control.calen.A = function () {
  7. var DateJson = ['一', '二', '三', '四', '五', '六', '日'];
  8. //外框
  9. var divs = $('<div class="U_Control_calen" style="position:relative;"></div>')[0];
  10. var input = $$('div', { 'class': 'U_BP_Cal-input' }, divs);
  11. var div = $$('div', { 'class': 'U_BP_Cal-div' }, divs);
  12. input.onclick = function () { U.Control.calen.A.click(div) }
  13. //日历头部
  14. var divTop = $$('div', { 'class': 'U_BP_Cal-Top' }, div);
  15. var TopTitle = $$('div', { 'class': 'U_BP_Cal-Top-Title' }, divTop);
  16. var tYear = $$('div', { 'class': 'U_BP_Cal-Top-tYear' }, TopTitle);
  17. tYear.onclick = function () { U.Control.calen.A.Close(conetY, this, conet, yearText, monthText, conetDB); }
  18. var yearText = $$('a', { 'class': 'U_BP_Cal-Top-yearText', 'innerHTML': '2016' }, tYear);
  19. var yearTitle = $$('a', { 'class': 'U_BP_Cal-Top-yearText', 'innerHTML': '年' }, tYear);
  20. var tMonths = $$('div', { 'class': 'U_BP_Cal-Top-tMonths' }, TopTitle);
  21. var tMonth = $$('div', { 'class': 'U_BP_Cal-Top-tMonth' }, tMonths);
  22. tMonth.onclick = function () { U.Control.calen.A.Close(conetM, this, conet, yearText, monthText, conetDB); }
  23. var monthText = $$('a', { 'class': 'U_BP_Cal-Top-monthText', 'innerHTML': '9' }, tMonth);
  24. var monthTitle = $$('a', { 'class': 'U_BP_Cal-Top-monthText', 'innerHTML': '月' }, tMonth);
  25. //日历内容
  26. var conet = $$('div', { 'class': 'U_BP_Cal-conet', 'i': 'U_BP_Cal-conetD' }, div);
  27. var conetY = $$('div', { 'class': 'U_BP_Cal-conetY', 'style': { 'cssText': 'display:none;'} }, conet);
  28. var conetM = $$('div', { 'style': { 'cssText': 'display:none' }, 'class': 'U_BP_Cal-conetM' }, conet);
  29. var conetD = $$('div', { 'class': 'U_BP_Cal-conetD', 'style': { 'cssText': 'display:block'} }, conet);
  30. var conetDT = $$('div', { 'class': 'U_BP_Cal-conetDT' }, conetD);
  31. var conetDB = $$('div', { 'class': 'U_BP_Cal-conetDB' }, conetD);
  32. //月份左右切换
  33. var tMonthLeft = $$('div', { 'class': 'U_BP_Cal-triangleLeft', 'onclick': [U.Control.calen.A.Ontop, ([yearText, monthText, conetDB])] }, tMonths); //'U.Control.calen.A.Ontop()'
  34. var tMonthRight = $$('div', { 'class': 'U_BP_Cal-triangleRight', 'onclick': [U.Control.calen.A.Onbottom, ([yearText, monthText, conetDB])] }, tMonths); //'U.Control.calen.A.Onbottom()'
  35. //获取今天的信息
  36. var mydate = U.Control.calen.Date();
  37. //年份 把元素放在conetY 起始年今年
  38. U.Control.calen.A.couldY(conetY, mydate['year'], conetM, conet, yearText);
  39. //月份
  40. for (i = 1; i < 13; i++) {
  41. blockM = $$('div', { 'class': 'U_BP_Cal-blockM', 'innerHTML': i + "月" }, conetM);
  42. blockM.onclick = function () {
  43. U.Control.calen.A.Open(conetD, this, conet, yearText, monthText, conetDB);
  44. }
  45. }
  46. //日历的星期
  47. for (i = 0; i < 7; i++) {
  48. $$('div', { 'class': 'U_BP_Cal-conetDTT', 'innerHTML': DateJson[i] }, conetDT);
  49. }
  50. yearText.innerHTML = mydate['year']; //今天的年
  51. monthText.innerHTML = mydate['month']; //今天的月
  52. //生成本月的日历
  53. U.Control.calen.A.nowMonth(U.Control.calen.Mouth(mydate['year'], mydate['month'], mydate['date'], 7, 5), conetDB, monthText.innerHTML, yearText.innerHTML);
  54. return divs;
  55. }
  56. //点击日历隐藏显示
  57. U.Control.calen.A.click = function (div) {
  58. (div.style.display != "block") ? div.style.display = "block" : div.style.display = "none";
  59. }
  60. //通用 返回:年-月-日 星期 时-分-秒
  61. U.Control.calen.Date = function () {
  62. var myDate = new Date()//获取日期时间
  63. //年月日
  64. var year = myDate.getFullYear();
  65. var month = myDate.getMonth() + 1;
  66. var date = myDate.getDate();
  67. var day = myDate.getDay();
  68. //时分秒
  69. var hours = myDate.getHours();
  70. var minutes = myDate.getMinutes();
  71. var seconds = myDate.getSeconds();
  72. //时与分小于9前面加上0
  73. if (minutes <= 9) minutes = "0" + minutes;
  74. if (seconds <= 9) seconds = "0" + seconds;
  75. //数字转文字
  76. var days = { 0: "日", 1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六" }
  77. return { "year": year, "month": month, "date": date, "day": days[day], "hours": hours, "minutes": minutes, "seconds": seconds }
  78. }
  79. //生成年(年份整体id,哪一年)
  80. U.Control.calen.A.couldY = function (conetY, nowYear, conetM, conet,yearText) {
  81. //blocks(年月横向)i j定义for循环
  82. var blocks, i, j;
  83. //div清空年月的div
  84. conetY.innerHTML = "";
  85. //for循环生产年份的div
  86. for (i = 0; i < 4; i++) {//竖向四个
  87. blocks = $$('div', { 'class': 'U_BP_Cal-blocks' }, conetY);
  88. for (j = 0; j < 3; j++) {//横向三个
  89. block = $$('div', { 'class': 'U_BP_Cal-block', 'innerHTML': nowYear++ }, blocks);
  90. block.onclick = function () {
  91. U.Control.calen.A.Open(conetM, this, conet, yearText);
  92. }
  93. }
  94. }
  95. //在年份添加鼠标滑动事件 传入DOM的event
  96. var last, year, blocks, block; //定义年
  97. conetY.onmousewheel = function (event) {
  98. event = event || window.event;
  99. if (event.wheelDeltaY == -120) {//向下滚动
  100. last = conetY.firstChild; //年份第一个数
  101. year = parseInt(conetY.lastChild.lastChild.innerHTML); //获取年最后一个数-->转成数字形式
  102. last.parentNode.removeChild(last); //删除年第一个
  103. blocks = $$('div', { 'class': 'U_BP_Cal-blocks' }, conetY); //创建一个年
  104. for (i = 0; i < 3; i++) {//循环出一行连续的年份
  105. year = year + 1; //连续所以++
  106. $$('div', { 'onclick': 'U.Control.calen.conetM,this)', 'class': 'U_BP_Cal-block', 'innerHTML': year }, blocks);
  107. }
  108. }
  109. if (event.wheelDeltaY == 120) {//向上滚动
  110. last = conetY.lastChild; //获取年份最后一个数
  111. year = parseInt(conetY.firstChild.firstChild.innerHTML); //获取年第一个数-->转成数字形式
  112. last.parentNode.removeChild(last); //删除最后一个数
  113. blocks = $('<div class="U_BP_Cal-blocks"></div>')[0]; //创建一个年
  114. conetY.insertBefore(blocks, conetY.childNodes[0]); //插入第一行第一个
  115. for (i = 0; i < 3; i++) {
  116. year = year - 1; //连续所以--
  117. block = $('<div class="U_BP_Cal-block">' + year + '</div>')[0]; //创建一个年
  118. block.onclick = function () {
  119. U.Control.calen.A.Open(conetM, this, conet, yearText); //点击之后出现放大动画
  120. }
  121. blocks.insertBefore(block, blocks.childNodes[0]); //插入到元素之前
  122. }
  123. }
  124. }
  125. }
  126. //放大的动画 传入参数(年/月,获取点击的元素)
  127. U.Control.calen.A.Open = function (div2, thiss, conet, yearText, monthText, conetDB) {
  128. //将获取到的ID再去获取元素
  129. div = thiss.parentNode.parentNode.parentNode.parentNode.getElementsByClassName(conet.i)[0]; //$('.' + U.Control.calen.conet.i)[0]; //document.getElementById("UCD_CT_SM_IF").contentWindow.
  130. //判断是年还是月
  131. if (thiss.className == "U_BP_Cal-block") {
  132. yearText.innerHTML = parseInt(thiss.innerHTML);
  133. }
  134. if (thiss.className == "U_BP_Cal-blockM") {
  135. monthText.innerHTML = parseInt(thiss.innerHTML);
  136. U.Control.calen.A.nowMonth(U.Control.calen.Mouth(yearText.innerHTML, parseInt(thiss.innerHTML), 1, 7, 5), conetDB, monthText.innerHTML, yearText.innerHTML); //mydate['date']
  137. }
  138. conet.i = div2.className;
  139. //动画初始
  140. div.style.cssText += "transition: 0.3s all;transform: scale(1.3);opacity:0;";
  141. //动画开始
  142. setTimeout(function () {
  143. div.style.cssText += "transition: 0s all;transform: scale(1);opacity:1;display:none;";
  144. div2.style.cssText += "transition: 0.2s all;transform: scale(0.6);opacity:0;display:block;";
  145. setTimeout(function () {
  146. div2.style.cssText += "transition: 0.2s all;transform: scale(1);opacity:1;display:block;";
  147. }, 200)
  148. }, 300)
  149. }
  150. //缩小的动画 传入参数(年/月,获取点击的元素)
  151. U.Control.calen.A.Close = function (div2, thiss, conet, yearText, monthText, conetDB) {
  152. //将获取到的ID再去获取元素
  153. div = thiss.parentNode.parentNode.parentNode.parentNode.getElementsByClassName(conet.i)[0]; //$('.' + U.Control.calen.conet.i)[0]; //document.getElementById("UCD_CT_SM_IF").contentWindow.
  154. //判断是年还是月
  155. conet.i = div2.className;
  156. //动画初始
  157. div.style.cssText += "transition: 0.3s all;transform: scale(0.8);opacity:0;display:block;";
  158. //动画开始
  159. setTimeout(function () {
  160. div.style.cssText += "transition: 0s all;transform: scale(1);opacity:1;display:none;";
  161. div2.style.cssText += "transition: 0.2s all;transform: scale(0.8);opacity:0;display:block;";
  162. setTimeout(function () {
  163. div2.style.cssText += "transition: 0.2s all;transform: scale(1);opacity:1;display:block;";
  164. }, 150)
  165. }, 300)
  166. }
  167. //获取月下面的日历 参year, month, nowDate, across, line 返回一个数组[1,2,3.....29,30]
  168. U.Control.calen.Mouth = function (year, month, nowDate, across, line) {
  169. var total = across * line;
  170. var oldDate = new Date(year, month - 1, 0).getDate(); //上个月最大天数
  171. var newDate = new Date(year, month, 0).getDate(); //本月最大天数
  172. var week = new Date(year + "/" + month + "/" + 1).getDay(); //本月第一天是星期几
  173. //本页日历第一个数字
  174. var calNum, calInt;
  175. if (week == 0) week = 7;
  176. calNum = 0, calFirst = oldDate - week + 1;
  177. //calNum有三层 上月 本月 下月,将本页第一个数赋给calInt.
  178. calInt = calFirst, myjson = [];
  179. //变量不要定义在for循环里面,定义在函数开始位置,所有变量定义都放在函数开始位置---楚王辉
  180. //一个for循环出一个日历
  181. for (i = 0; i < total; i++) {
  182. if (calNum == 0 && calInt <= oldDate) { //上个月
  183. if (calInt == oldDate) {
  184. calNum++; calInt = 1;
  185. myjson.push(calInt); continue;
  186. } else { calInt++; }
  187. }
  188. if (calNum == 1 && calInt <= newDate) { //本月
  189. if (calInt == newDate) {
  190. calNum++; calInt = 1;
  191. myjson.push(calInt); continue;
  192. } else { calInt++; }
  193. }
  194. if (calNum == 2 && i < total) calInt++ //下个月
  195. myjson.push(calInt);
  196. }
  197. return myjson; //返回一个数组[1,2,3.....29,30]
  198. }
  199. //生成日期 传入参数(日历数组)
  200. U.Control.calen.A.nowMonth = function (conetDs, conetDB, monthText, yearText) {
  201. conetDB.innerHTML = ""; //清空日历
  202. //monthText = U.Control.calen.monthText;
  203. var firstI = 1; //定义第一个数
  204. //循环出一个月的日历
  205. for (i = 0; i < 35; i++) {
  206. blockD = $$('div', { 'class': 'U_BP_Cal-blockD', 'innerHTML': conetDs[i] }, conetDB);
  207. if (conetDs[i] == firstI) { //给本月增加样式(字体黑色)
  208. firstI++;
  209. blockD.style.color = "#000";
  210. blockD.year = yearText; //parseInt(yearText.innerHTML);
  211. blockD.month = monthText; //parseInt(monthText.innerHTML);
  212. } else { //上个月或者下个月
  213. if (firstI == 1) {
  214. blockD.month = monthText - 1; //parseInt(monthText.innerHTML) - 1;
  215. } else {
  216. blockD.month = monthText + 1; //parseInt(monthText.innerHTML) + 1;
  217. }
  218. }
  219. //日历的点击事件
  220. blockD.onclick = function () {
  221. div = this.parentNode.parentNode.parentNode.parentNode;
  222. input = this.parentNode.parentNode.parentNode.parentNode.parentNode.children[0];
  223. year = parseInt(this.year); //年
  224. month = parseInt(this.month);
  225. if (month < 10) month = "0" + month;
  226. day = parseInt(this.innerHTML); //天
  227. if (day < 10) day = "0" + day;
  228. input.innerHTML = year + '-' + month + '-' + day; //给input赋值
  229. div.style.display = "none"; //隐藏控件
  230. }
  231. }
  232. }
  233. //年份鼠标往上滑动
  234. U.Control.calen.A.Ontop = function (yearText, monthText, conetDB) {
  235. //判断是否为1月
  236. if (monthText.innerHTML == 1) {
  237. yearText = yearText.innerHTML = parseInt(yearText.innerHTML) - 1;
  238. monthText = monthText.innerHTML = 12;
  239. } else {
  240. yearText = parseInt(yearText.innerHTML);
  241. monthText = monthText.innerHTML = monthText.innerHTML - 1;
  242. }
  243. U.Control.calen.A.nowMonth(U.Control.calen.Mouth(yearText, monthText, 1, 7, 5), conetDB, monthText, yearText);
  244. }
  245. //年份鼠标往下滑动
  246. U.Control.calen.A.Onbottom = function (yearText, monthText, conetDB) {
  247. //判断是否为12月
  248. if (monthText.innerHTML == 12) {
  249. yearText = yearText.innerHTML = parseInt(yearText.innerHTML) + 1;
  250. monthText = monthText.innerHTML = 1;
  251. } else {
  252. yearText = parseInt(yearText.innerHTML);
  253. monthText = monthText.innerHTML = parseInt(monthText.innerHTML) + 1;
  254. }
  255. //将值传进去
  256. U.Control.calen.A.nowMonth(U.Control.calen.Mouth(yearText, monthText, 1, 7, 5), conetDB, monthText, yearText);
  257. }