meteoVideo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. define(['html!meteoDir/other/meteoVideo/meteoVideo.html',
  2. 'css!meteoDir/other/meteoVideo/css/video.css'], function (video) {
  3. var video_stus = {
  4. //渲染div的id
  5. id: "",
  6. //时间轴移动的位置
  7. x: 0,
  8. //播放的当前页数
  9. nowPage: 0,
  10. //播放总页数
  11. dataCount: 0,
  12. //每秒移动的像素
  13. perTime: "",
  14. //播放间隔
  15. intervalTime: 800,
  16. //定时事件ID
  17. intervalId: null,
  18. //启报时间毫秒数
  19. qbTime: 0,
  20. //vti
  21. vti: ["000", "003", "006", "009", "012", "015", "018", "021", "024", "027", "030", "033",
  22. "036", "039", "042", "045", "048", "051", "054", "057", "060", "063", "066", "069",
  23. "072", "075", "078", "081", "084", "087", "090", "093", "096", "099", "102", "105",
  24. "108", "111", "114", "117", "120", "123", "126", "129", "132", "135", "138", "141",
  25. "144", "147", "150", "153", "156", "159", "162", "165", "168"]
  26. }
  27. //播放器初始化
  28. function init(id, callback) {
  29. video_stus.id = id;
  30. $("#" + id).html(video);
  31. video_stus.dataCount = video_stus.vti.length - 1;
  32. initVti();
  33. meaurePerTime();
  34. bingEvents();
  35. if (typeof callback === "function") {
  36. callback.call();
  37. }
  38. //设置最初默认时间
  39. clearTime();
  40. }
  41. //设置最大vti值
  42. function initVti() {
  43. $(".meteo-video-progress-lasttime").html(video_stus.vti[video_stus.vti.length - 1]);
  44. $(".meteo-video-progress-nowtime").html(video_stus.vti[0]);
  45. $(".meteo-title-left-c").html("大气海洋数值预报-<span style='color:#03e3fa;font-weight: 800'></span>");
  46. }
  47. //计算纵轴单个刻度长度
  48. function meaurePerTime() {
  49. var proWidth = $(".meteo-video-progress").width();
  50. video_stus.perTime = proWidth / (video_stus.vti.length - 1);
  51. }
  52. /**
  53. * 设置起报时间
  54. * @param time 当前时间
  55. * @param isSea 是否为海洋
  56. */
  57. function setQbTime(time, isSea) {
  58. if (time == null) {
  59. time = new Date().getTime();
  60. }
  61. var qbTime = isSea? getQBTime20(time) : getQbTimeStr(time); //如果是海洋,取20时起报,否则取最近起报
  62. var t = qbTime.split(' ');
  63. var t1 = t[0].split('-');
  64. var t2 = t[1].split(':');
  65. var now = new Date();
  66. now.setFullYear(t1[0], t1[1] - 1, t1[2]);
  67. now.setHours(t2[0], 0, 0);
  68. video_stus.qbTime = now.getTime();
  69. $("#meteo-video-query-startDate").val(qbTime);
  70. }
  71. //更换起报时间
  72. function updateQbTime() {
  73. stop();
  74. var startDate = $("#meteo-video-query-startDate").val();
  75. var t = startDate.split(' ');
  76. var t1 = t[0].split('-');
  77. var t2 = t[1].split(':');
  78. var newQbTime = new Date();
  79. newQbTime.setFullYear(t1[0], t1[1] - 1, t1[2]);
  80. newQbTime.setHours(t2[0], 0, 0);
  81. newQbTime = newQbTime.getTime();
  82. setQbTime(newQbTime, false);
  83. setVti(0); //更换起报时间时,重置vti为0
  84. }
  85. /**
  86. * 设置距离当前时间最合适的起报时间
  87. * @param isSea 是否为海洋
  88. */
  89. function clearTime(isSea) { //
  90. var date = new Date(); //取当前时间
  91. // date = new Date("2018/7/13 05:00"); //todo 默认时间修改为样例数据时间
  92. // var qbDate = new Date().setTime(date.getTime() - 43200000); //当前时间12小时前为起报时间
  93. var qbDate = new Date().setTime(date.getTime()); //当前时间12小时前为起报时间
  94. //10800000 = 3 * 60 * 60 * 1000
  95. //43200000 = 12 * 60 * 60 * 1000
  96. setQbTime(qbDate, isSea);
  97. var hours = (date - video_stus.qbTime) / 3600000; //根据当前时间及最近起报时间算出小时差值
  98. //3600000 = 60 * 60 * 1000
  99. var index = (hours / 3) >> 0; //计算vti索引
  100. setVti(index);
  101. }
  102. //按钮绑定事件
  103. function bingEvents() {
  104. //上一个
  105. $(".meteo-video-btn-back").off("click").on("click", function () {
  106. back();
  107. });
  108. //播放
  109. $(".meteo-video-btn-play").off("click").on("click", function () {
  110. if (!video_stus.intervalId) { //播放
  111. play();
  112. } else { //暂停
  113. stop();
  114. }
  115. });
  116. //下一个
  117. $(".meteo-video-btn-next").off("click").on("click", function () {
  118. next();
  119. });
  120. //进度轴时间点
  121. $(".meteo-video-progress_btn").off("mousedown").on("mousedown", function (event) {
  122. stop();
  123. var yuanleft = 0;
  124. var leftVal = event.clientX - this.offsetLeft - event.offsetX;
  125. var width = $(".meteo-video-progress").width();
  126. var pt = video_stus.perTime / 2;
  127. document.onmousemove = function (event) {
  128. var event = event || window.event;
  129. yuanleft = event.clientX - leftVal;
  130. if (yuanleft < 0) yuanleft = 0;
  131. if (yuanleft > width) yuanleft = width;
  132. var index = ((yuanleft + pt) / video_stus.perTime) >> 0;
  133. if (index != video_stus.nowPage) setVti(index);
  134. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  135. }
  136. document.onmouseup = function () {
  137. document.onmousemove = null; //弹起鼠标不做任何操作
  138. }
  139. })
  140. }
  141. function play() {
  142. if (video_stus.nowPage == video_stus.vti.length - 1) return;
  143. if (video_stus.intervalId) stop();
  144. $(".meteo-video-btn-play").removeClass("meteo-video-play-img");
  145. $(".meteo-video-btn-play").addClass("meteo-video-parse-img");
  146. video_stus.intervalId = window.setInterval(playNext, video_stus.intervalTime);
  147. }
  148. function playNext() {
  149. setVti(video_stus.nowPage + 1);
  150. if (video_stus.nowPage >= video_stus.vti.length - 1) {
  151. stop();
  152. return;
  153. }
  154. }
  155. function stop() {
  156. if (!video_stus.intervalId) return;
  157. $(".meteo-video-btn-play").removeClass("meteo-video-parse-img");
  158. $(".meteo-video-btn-play").addClass("meteo-video-play-img");
  159. clearInterval(video_stus.intervalId);
  160. video_stus.intervalId = null;
  161. }
  162. //时间格式化
  163. function getTimeStr(d) {
  164. var now = new Date(d);
  165. var year = now.getFullYear();
  166. var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1);
  167. var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
  168. var hours = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
  169. // var minutes = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
  170. // var seconds = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
  171. return year + '-' + month + '-' + date + ' ' + hours + ':00';//+ minutes+':'+seconds;
  172. }
  173. /**
  174. * 获取最近的起报时间(08或20)
  175. * @param d
  176. * @returns {string}
  177. */
  178. function getQbTimeStr(d) {
  179. var time = new Date(d);
  180. var hour = time.getHours();
  181. var vtiHour = "08";
  182. if (hour < 8) {
  183. vtiHour = "20";
  184. d = d - 60 * 1000 * 60 * 24;
  185. } else if (hour < 20) {
  186. vtiHour = "08"
  187. } else {
  188. vtiHour = "20";
  189. }
  190. var now = new Date(d);
  191. var year = now.getFullYear();
  192. var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1);
  193. var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
  194. return year + '-' + month + '-' + date + ' ' + vtiHour + ':00';
  195. }
  196. /**
  197. * 获取20时的起报时间
  198. * @param d 要寻找起报时间的时间
  199. * @returns {string}
  200. */
  201. function getQBTime20(d) {
  202. var time = new Date(d);
  203. var hour = time.getHours();
  204. var vtiHour = "08";
  205. if (hour < 20) {
  206. vtiHour = "20";
  207. d = d - 60 * 1000 * 60 * 24;
  208. } else {
  209. vtiHour = "20";
  210. }
  211. var now = new Date(d);
  212. var year = now.getFullYear();
  213. var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1);
  214. var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
  215. return year + '-' + month + '-' + date + ' ' + vtiHour + ':00';
  216. }
  217. //调整按钮样式
  218. function btnDisabled() {
  219. if (video_stus.nowPage > 0) {
  220. $(".meteo-video-btn-next").removeClass("meteo-video-next-img");
  221. $(".meteo-video-btn-next").addClass("meteo-video-next-img-blue");
  222. $(".meteo-video-btn-back").removeClass("meteo-video-back-img");
  223. $(".meteo-video-btn-back").addClass("meteo-video-back-img-blue");
  224. if (video_stus.nowPage >= video_stus.vti.length - 1) {
  225. $(".meteo-video-btn-next").removeClass("meteo-video-next-img-blue");
  226. $(".meteo-video-btn-next").addClass("meteo-video-next-img");
  227. }
  228. } else {
  229. $(".meteo-video-btn-next").removeClass("meteo-video-next-img");
  230. $(".meteo-video-btn-next").addClass("meteo-video-next-img-blue");
  231. $(".meteo-video-btn-back").removeClass("meteo-video-back-img-blue");
  232. $(".meteo-video-btn-back").addClass("meteo-video-back-img");
  233. }
  234. }
  235. function next() {
  236. stop(); //点击下一时次按钮时,先停止播放
  237. setVti(video_stus.nowPage + 1);
  238. }
  239. function back() {
  240. stop(); //点击上一时次按钮时,先停止播放
  241. setVti(video_stus.nowPage - 1);
  242. }
  243. function setVti(index) { //设置当前显示的vti
  244. if (isNaN(index)) index = 0;
  245. if (index >= video_stus.vti.length) return;
  246. if (index < 0) return;
  247. video_stus.nowPage = index;
  248. video_stus.x = video_stus.nowPage * video_stus.perTime;
  249. $(".meteo-video-progress_btn").css("left", video_stus.x - 12);
  250. $(".meteo-video-progress-nowtime").html(video_stus.vti[video_stus.nowPage]);
  251. setTimeout(function() {
  252. var firTimeVal = $("#meteo-video-query-startDate").val();
  253. firTimeVal = new Date(firTimeVal).getTime();
  254. var nowdate = getTimeStr(firTimeVal + parseInt(video_stus.vti[video_stus.nowPage]) * 60 * 60 * 1000);
  255. // var nowdate = getTimeStr(video_stus.qbTime + parseInt(video_stus.vti[video_stus.nowPage]) * 60 * 60 * 1000);
  256. $(".meteo-title-left-c span").html(nowdate);
  257. }, 300);
  258. btnDisabled();
  259. timeChangeEvent();
  260. }
  261. return ONEMAP.M.video = {
  262. init: init,
  263. updateQbTime: updateQbTime,
  264. clearTime: clearTime,
  265. stop: stop
  266. }
  267. //当前时间改变
  268. function timeChangeEvent() {
  269. if (meteo.c.time)
  270. meteo.c.time.timeUpdate();
  271. }
  272. })