jQuery-jcDate.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* jQuery - jcDate v1.0 */
  2. ;(function($){
  3. $.fn.jcDate = function(options) {
  4. var defaults = {
  5. IcoClass : "jcDateIco",
  6. Event : "click",
  7. Speed : 100,
  8. Left : 0,
  9. Top : 22,
  10. format : "/",
  11. Timeout : 100,
  12. Oldyearall : 9, // 配置过去多少年
  13. Newyearall : 8, // 配置未来多少年
  14. };
  15. var options = $.extend(defaults,options);
  16. return this.each(function() {
  17. if($("#jcDate").length == 0){
  18. $("body").prepend("<div id='jcDate'><input type='hidden' value='' id='dateHideText' /><div id='jcDateTt'><a id='d_prev'><</a><div><span></span><b>年</b><samp></samp><b>月</b></div><a id='d_next'>></a></div><dl id='jcYeas'></dl><dl id='jcMonth'></dl><div id='jcDayWrap'><dl id='jcDayCon'></dl><div id='jcDateMax'><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul><ul></ul></div><div id='jcDateBtn'><samp>今天</samp><span>清空</span><a id='d_sub'>确定</a><div id='jcTimeWrap'><input type='text' value='' /><label>:</label><input type='text' value='' /></div></div></div></div>");
  19. };
  20. var $dateInput = $(this),
  21. $window = $(window),
  22. dateObj = new Date(),
  23. $jcDate = $("#jcDate"),
  24. inputOffLeft = $(this).offset().left,
  25. inputOfftop = $(this).offset().top,
  26. $year = $("#jcDateTt").find("span"),
  27. $month = $("#jcDateTt").find("samp"),
  28. $b = $("#jcDateTt").find("b"),
  29. $jcDateMax = $("#jcDateMax"),
  30. $weekFrame = $("#jcDayCon"),
  31. $DateBtn = $("#jcDateBtn"),
  32. $jcYeas = $("#jcYeas"),
  33. $jcMonth = $("#jcMonth"),
  34. $Now = $DateBtn.find("samp"),
  35. $clear = $DateBtn.find("span"),
  36. $jcDayWrap = $("#jcDayWrap"),
  37. $dayFrame = $jcDayWrap.find("ul"),
  38. $submit = $DateBtn.find("a#d_sub"),
  39. $HiddenText = $("input#dateHideText"),
  40. $jcTimeWrap = $("#jcTimeWrap"),
  41. $hh = $jcTimeWrap.find("input:eq(0)"),
  42. $mm = $jcTimeWrap.find("input:eq(1)"),
  43. $d_prev = $("#d_prev"),
  44. $d_next = $("#d_next"),
  45. // 获取当前时间
  46. _year = parseInt(dateObj.getFullYear()),
  47. _month = dateObj.getMonth() + 1,
  48. _date = dateObj.getDate(),
  49. week = dateObj.getDay(),
  50. days = [ "日","一 ","二 ","三 ","四 ","五 ","六 "],
  51. _day = days[week],
  52. _hours = dateObj.getHours(),
  53. _minutes = dateObj.getMinutes(),
  54. weekDl = "",
  55. yearDl = "<dd class='visited'>"+_year+"年"+"</dd>",
  56. monthDl = "",
  57. $ul = "",
  58. _idx = 0,
  59. defInfo = "",
  60. getDateCount = function(y, m){
  61. return [31, y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ? 29 : 28 ,31,30,31,30,31,31,30,31,30,31][ m+1 ];
  62. };
  63. // 赋值数据
  64. $hh.val("00");
  65. $mm.val("00");
  66. for(var w = 0 in days) {
  67. weekDl = weekDl + "<dt>" + days[w] + "</dt>";
  68. };
  69. $weekFrame.html(weekDl);
  70. var NowY = _year,
  71. yArr = new Array(),
  72. mArr = new Array();
  73. for(var lys = 0; lys < options.Oldyearall ; lys++ ){
  74. lastyear = NowY - (lys+1);
  75. yArr.push("<dt>"+lastyear+"年"+"</dt>")
  76. };
  77. yArr.reverse();
  78. var dtf = ysArr(yArr);
  79. yArr.length = 0;
  80. for(var fys = 0; fys < options.Newyearall ; fys++ ){
  81. firstyear = NowY + (fys+1);
  82. yArr.push("<dt>"+firstyear+"年"+"</dt>")
  83. };
  84. var dtl = ysArr(yArr);
  85. $jcYeas.html(dtf+yearDl+dtl);
  86. function ysArr(arr){
  87. var $Dts = "";
  88. for(var index = 0 in arr){
  89. $Dts = $Dts + arr[index];
  90. };
  91. return $Dts
  92. };
  93. for(var ms = 1; ms <= 12; ms++ ){
  94. if(ms == _month){
  95. mArr.push("<dt class='visited'>" + ms + "月" +"</dt>");
  96. } else {
  97. mArr.push("<dt>" + ms + "月" +"</dt>");
  98. };
  99. };
  100. for(var mA = 0 in mArr){
  101. monthDl += mArr[mA];
  102. };
  103. $jcMonth.html(monthDl)
  104. function shFade(show,hide,hide2,bool,index){
  105. var fadeSpeed = options.Speed;
  106. hide.fadeOut(fadeSpeed);
  107. hide2.fadeOut(fadeSpeed);
  108. show.delay(fadeSpeed*2).fadeIn(fadeSpeed);
  109. if(bool){
  110. $("#d_prev,#d_next").fadeOut(fadeSpeed);
  111. $b.eq(index).hide();
  112. } else {
  113. $("#d_prev,#d_next").fadeIn(fadeSpeed);
  114. $b.show();
  115. $year.show();
  116. $month.show();
  117. };
  118. };
  119. $year.die().live("click",function(){
  120. $(this).addClass("visited").next("samp").removeClass("visited");
  121. shFade($jcYeas,$jcDayWrap,$jcMonth,true,1);
  122. $year.show();
  123. $month.hide();
  124. });
  125. $("#jcYeas dd,#jcYeas dt").die().live("click",function(){
  126. var y = parseInt($(this).text());
  127. Traversal(y);
  128. $year.text(y);
  129. shFade($jcDayWrap,$jcYeas,$jcMonth,false);
  130. });
  131. $month.die().live("click",function(){
  132. $(this).addClass("visited").prev().removeClass("visited");
  133. shFade($jcMonth,$jcDayWrap,$jcYeas,true,0);
  134. $month.show();
  135. $year.hide();
  136. });
  137. $jcMonth.find("dt").die().live("click",function(){
  138. var m = parseInt($(this).text());
  139. mAnimate(m-1);
  140. $month.text(m);
  141. shFade($jcDayWrap,$jcYeas,$jcMonth,false);
  142. });
  143. function Traversal(Ty){
  144. $year.text(Ty);
  145. for(var m = 0; m < $dayFrame.length; m++){
  146. var dayLi = "",
  147. $fLi = "",
  148. $lLi = "",
  149. firstLi = "",
  150. lastDay = 0,
  151. yearWeek = new Date(Ty,m,1).getDay();
  152. getDateCount(Ty,m-2) == undefined ? fristDay = 31 : fristDay = getDateCount(Ty,m-2);
  153. for(var d = 1; d <= getDateCount(Ty,m-1); d++){
  154. dayLi = dayLi + "<li class='jcDateColor'>"+ d +"</li>";
  155. };
  156. for(var f = 0 ; f < yearWeek; f++){
  157. firstLi += "<li>"+ fristDay +"</li>,";
  158. fristDay--;
  159. };
  160. for(var l = 0 ; l <= 42-(d+yearWeek); l++ ){
  161. lastDay = l + 1;
  162. $lLi += "<li>"+lastDay+"</li>";
  163. };
  164. fLiArr = firstLi.split(",").reverse();
  165. for(var arr = 0 in fLiArr){
  166. $fLi += fLiArr[arr];
  167. };
  168. $addLi = $fLi + dayLi + $lLi;
  169. $dayFrame.eq(m).html($addLi);
  170. };
  171. };
  172. mAnimate(_month-1);
  173. //功能方法
  174. $d_prev.die().live("click",function(){
  175. _idx = $dayFrame.filter(".dateV").index();
  176. if( _idx > 0){
  177. _idx --;
  178. } else {
  179. var _Tyear = parseInt($year.text());
  180. _Tyear--;
  181. Traversal(_Tyear);
  182. _idx = $dayFrame.length-1;
  183. };
  184. mAnimate(_idx);
  185. });
  186. $d_next.die().live("click",function(){
  187. _idx = $dayFrame.filter(".dateV").index();
  188. if( _idx < $dayFrame.length-1){
  189. _idx ++;
  190. } else {
  191. var _Tyear = parseInt($year.text());
  192. _Tyear++;
  193. Traversal(_Tyear);
  194. _idx = 0;
  195. };
  196. mAnimate(_idx);
  197. });
  198. function mAnimate(index){
  199. $dayFrame.eq(index).addClass("dateV").siblings().removeClass("dateV");
  200. $month.text(index+1);
  201. $jcDateMax.animate({ "left":-index*161 },options.Speed);
  202. };
  203. function today(y,m,d){
  204. $year.text(y);
  205. $month.text(m);
  206. Traversal(y);
  207. $.each($dayFrame.eq(m-1).find("li"),function(){
  208. if($(this).hasClass("jcDateColor")){
  209. $dayFrame.eq(m-1).find("li.jcDateColor").eq(d-1).addClass("visited");
  210. };
  211. });
  212. $HiddenText.val(_date);
  213. $dayFrame.eq(m-1).find("li").text();
  214. mAnimate(m-1);
  215. };
  216. function clearVisited(){
  217. $.each($("li.jcDateColor",$dayFrame),function(){
  218. if($(this).hasClass("visited")){
  219. $(this).removeClass("visited");
  220. };
  221. });
  222. };
  223. today(_year,_month,_date);
  224. $dayFrame.undelegate("li.jcDateColor","click").delegate("li.jcDateColor","click",function(){
  225. clearVisited();
  226. $(this).addClass("visited");
  227. $HiddenText.val(parseInt($(this).text()))
  228. });
  229. $dayFrame.undelegate("li.jcDateColor","dblclick").delegate("li.jcDateColor","click",function(){
  230. submitDate();
  231. });
  232. $Now.die().live("click",function(){
  233. today(_year,_month,_date);
  234. });
  235. $clear.die().live("click",function(){
  236. $("input.dateVisited").val("");
  237. });
  238. $submit.die().live("click",function(){
  239. submitDate();
  240. });
  241. function submitDate(){
  242. var Sy = $year.text(),
  243. Sm = $month.text(),
  244. Sd = $HiddenText.val();
  245. NowDateArr = new Array(Sy,Sm,Sd);
  246. dateInfo =NowDateArr.join(options.format);
  247. if($hh.val() != "00" || $mm.val() != "00" ){
  248. var Sh = $hh.val(),
  249. Sm = $mm.val();
  250. NowDateArr.push(Sh+":"+Sm);
  251. printDate = NowDateArr.join(options.format).substring(),
  252. format = printDate.split("/");
  253. dateInfo = format[0]+options.format+format[1]+options.format+format[2]+" "+format[3]
  254. };
  255. $("input.dateVisited").val(dateInfo);
  256. closeDate();
  257. };
  258. $dateInput.addClass(options.IcoClass)
  259. //.val(defInfo)
  260. .die()
  261. .live(options.Event,function(e){
  262. $(this).addClass("dateVisited");
  263. //$jcDate.css({ "left" : e.pageX+(options.Left),"top" : e.pageY+(options.Top) });
  264. var iof = $(this).offset();
  265. $jcDate.css({ "left" : iof.left+options.Left+125,"top" : iof.top+options.Top+13 });
  266. $jcDate.show(options.Speed);
  267. $jcDayWrap.show();
  268. $jcYeas.hide();
  269. $jcMonth.hide();
  270. $("#d_prev,#d_next").show();
  271. $year.show();
  272. $month.show();
  273. $b.show();
  274. });
  275. $jcDate.die().live("mouseleave",function(){
  276. setTimeout(closeDate,options.Timeout);
  277. });
  278. function closeDate(){
  279. $("input.dateVisited").removeClass("dateVisited");
  280. $jcDate.hide(options.Speed);
  281. };
  282. });
  283. };
  284. })(jQuery)