当前位置:   article > 正文

JS优化了4个自定义倒计时

JS优化了4个自定义倒计时

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>4个自定义倒计时</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. box-sizing: border-box;
  11. user-select: none;
  12. body {
  13. background: #0b1b2c;
  14. }
  15. }
  16. header {
  17. width: 100vw;
  18. height: 40px;
  19. display: flex;
  20. justify-content: space-between;
  21. align-items: center;
  22. background-color: #cbcbcb;
  23. /* logo */
  24. figure.logo {
  25. width: 262px;
  26. height: 50px;
  27. /* 缩小 */
  28. transform: scale(0.8);
  29. margin-top: 2px;
  30. margin-left: -15px;
  31. border-radius: 50%;
  32. background: #f30303;
  33. box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6),
  34. inset -4px -4px 5px rgba(0, 0, 0, 0.6);
  35. z-index: 99;
  36. figcaption {
  37. background: hsl(0, 0%, 0%);
  38. background-clip: text;
  39. -webkit-background-clip: text;
  40. -webkit-text-fill-color: transparent;
  41. font-weight: bold;
  42. box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6), inset -4px -4px 5px rgba(0, 0, 0, 0.6);
  43. letter-spacing: -3px;
  44. line-height: 45px;
  45. border-radius: 50%;
  46. font-size: 40px;
  47. width: 152px;
  48. }
  49. .my_name1 {
  50. text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.726);
  51. clip-path: polygon(0% 0%, 100% 0%, 100% 50%, 0% 50%);
  52. transform: translate(35%, 0%);
  53. }
  54. .my_name2 {
  55. text-shadow: 1px 1px 1px hsla(160, 100%, 37%, 0.555);
  56. clip-path: polygon(0% 50%, 100% 50%, 100% 100%, 0% 100%);
  57. transform: translate(35%, -93%);
  58. }
  59. img {
  60. width: 60px;
  61. height: 35px;
  62. border-radius: 50%;
  63. transition: transform 0.3s ease;
  64. position: absolute;
  65. &:hover {
  66. transform: scale(1.3);
  67. filter: drop-shadow(0 0 0.3em #ff0202);
  68. }
  69. }
  70. .kong {
  71. margin-top: 7px;
  72. margin-left: 200px;
  73. }
  74. .ying {
  75. top: 7px;
  76. margin-left: 2px;
  77. }
  78. }
  79. /* 时间 */
  80. time {
  81. background: -webkit-linear-gradient(315deg, #e1ff00 50%, #ff0000);
  82. background-clip: text;
  83. -webkit-background-clip: text;
  84. -webkit-text-fill-color: transparent;
  85. font-size: 1.5rem;
  86. font-weight: 900;
  87. text-shadow: 2px -1px 8px rgba(250, 80, 193, 0.323);
  88. sub {
  89. -webkit-text-fill-color: #cfd601;
  90. text-shadow: 1px 1px 1px #100000;
  91. font-size: 1rem;
  92. }
  93. }
  94. /* 全屏还原关闭按钮 */
  95. menu {
  96. display: flex;
  97. button {
  98. background: none;
  99. border: none;
  100. }
  101. img {
  102. width: 35px;
  103. margin-top: 5px;
  104. margin-right: 5px;
  105. cursor: pointer;
  106. &:hover {
  107. transform: scale(1.2);
  108. padding: 5px;
  109. background: #ffd000;
  110. }
  111. }
  112. button:nth-child(2) {
  113. display: none;
  114. /* 放大镜效果 */
  115. transform: scale(1.16);
  116. }
  117. }
  118. }
  119. main {
  120. display: flex;
  121. flex-direction: column;
  122. align-items: center;
  123. button {
  124. font-size: 1.5rem;
  125. color: #d6d301;
  126. text-shadow: 1px 1px 1px #100000;
  127. font-weight: bold;
  128. cursor: pointer;
  129. border-radius: 3px;
  130. background-color: #f30303;
  131. border: none;
  132. margin: 5px;
  133. padding: 3px;
  134. }
  135. dialog {
  136. transform: translate(180%, 80%);
  137. }
  138. dialog::backdrop {
  139. /* 模糊背景 */
  140. backdrop-filter: blur(2px);
  141. }
  142. div {
  143. margin-top: 20px;
  144. color: #67c23a;
  145. text-shadow: 1px 1px 1px #100000;
  146. }
  147. }
  148. </style>
  149. </head>
  150. <body>
  151. <header>
  152. <!-- logo -->
  153. <figure class="logo">
  154. <a href="https://blog.csdn.net/lulei5153?spm=1011.2415.3001.5343" title="与妖为邻CSDN博客" target="_blank">
  155. <img class="kong" src="file:///D:/img/icon.ico" alt="与妖为邻">
  156. </a>
  157. <figcaption class="my_name1">与妖为邻</figcaption>
  158. <figcaption class="my_name2">与妖为邻</figcaption>
  159. <a href="file:///D:/web/html%E9%A6%96%E9%A1%B5/%E5%A4%87%E5%BF%98%E5%BD%95.html" class="home_page"
  160. title="首页" target="_blank">
  161. <img class="ying" src="file:///D:/img/ying.png" alt="与妖为邻">
  162. </a>
  163. </figure>
  164. <!-- 时间 -->
  165. <time datetime="2024-07-10" title="2024-07-10" id="dateTime">2024-07-10</time>
  166. <!-- 天气 -->
  167. <iframe class="tianqi" frameborder="0" width="280" height="36" scrolling="no" hspace="0"
  168. src="https://i.tianqi.com/?c=code&id=99">
  169. </iframe>
  170. <!-- 全屏还原关闭按钮 -->
  171. <menu>
  172. <button class="fullScreen" type="button"><img src="file:///D:/img/全屏.svg" alt="全屏"
  173. onclick="fullScreen()"></button>
  174. <button class="exitFullScreen" type="button"><img src="file:///D:/img/还原.svg" alt="还原"
  175. onclick="exitFullScreen()"></button>
  176. <button type="button"><img src="file:///D:/img/关闭.svg" alt="关闭" onclick="closeAll()"> </button>
  177. </menu>
  178. </header>
  179. <main id="countdownContainer">
  180. <div>
  181. <h2>2025主要节日时间表</h2>
  182. <ul>
  183. <li>元旦:2025年1月1日</li>
  184. <li>春节:2025年2月12日</li>
  185. <li>清明节:2025年4月4日</li>
  186. <li>劳动节:2025年5月1日</li>
  187. <li>端午节:2025年6月2日</li>
  188. <li>中秋节:2025年9月21日</li>
  189. <li>国庆节:2025年10月1日</li>
  190. </ul>
  191. </div>
  192. </main>
  193. </body>
  194. <script>
  195. /* 现在时间*/
  196. var current_time = document.getElementById("dateTime");
  197. function showTime() {
  198. var now = new Date();
  199. var year = now.getFullYear();
  200. var month = ("0" + (now.getMonth() + 1)).slice(-2);
  201. var day = ("0" + now.getDate()).slice(-2);
  202. var hour = ("0" + now.getHours()).slice(-2);
  203. var min = ("0" + now.getMinutes()).slice(-2);
  204. var second = ("0" + now.getSeconds()).slice(-2);
  205. var week = [
  206. "星期日",
  207. "星期一",
  208. "星期二",
  209. "星期三",
  210. "星期四",
  211. "星期五",
  212. "星期六",
  213. ][now.getDay()];
  214. var time = `${year}-${month}-${day}<sub id='sub'>${week}</sub> ${hour}:${min}:${second}`;
  215. current_time.innerHTML = time;
  216. }
  217. showTime();
  218. setInterval(showTime, 1000);
  219. /* 现在时间 结束*/
  220. /* 全屏功能与关闭*/
  221. function fullScreen() {
  222. var elem = document.documentElement;
  223. if (elem.requestFullscreen) {
  224. elem.requestFullscreen();
  225. } else if (elem.mozRequestFullScreen) { // Firefox
  226. elem.mozRequestFullScreen();
  227. } else if (elem.webkitRequestFullscreen) { // Chrome, Safari and Opera
  228. elem.webkitRequestFullscreen();
  229. } else if (elem.msRequestFullscreen) { // IE/Edge
  230. elem.msRequestFullscreen();
  231. }
  232. }
  233. function exitFullScreen() {
  234. if (document.exitFullscreen) {
  235. document.exitFullscreen();
  236. } else if (document.mozCancelFullScreen) { // Firefox
  237. document.mozCancelFullScreen();
  238. } else if (document.webkitExitFullscreen) { // Chrome, Safari and Opera
  239. document.webkitExitFullscreen();
  240. } else if (document.msExitFullscreen) { // IE/Edge
  241. document.msExitFullscreen();
  242. }
  243. }
  244. // 获取全屏和还原按钮
  245. var fullScreenBtn = document.querySelector(".fullScreen");
  246. var exitFullScreenBtn = document.querySelector(".exitFullScreen");
  247. // 监听全屏和还原事件
  248. document.addEventListener("fullscreenchange", function () {
  249. if (document.fullscreenElement) {
  250. fullScreenBtn.style.display = "none";
  251. exitFullScreenBtn.style.display = "block";
  252. } else {
  253. fullScreenBtn.style.display = "block";
  254. exitFullScreenBtn.style.display = "none";
  255. }
  256. });
  257. // 关闭当前窗口
  258. function closeAll() {
  259. window.close();
  260. }
  261. /* 全屏功能与关闭 结束*/
  262. /* 倒计时功能渲染*/
  263. const countdownContainer = document.getElementById('countdownContainer');
  264. function createCountdownElement(id) {
  265. const button = document.createElement('button');
  266. button.textContent = `第${id}个倒计时`;
  267. button.id = `countdownElement${id}`;
  268. button.onclick = () => document.getElementById(`dialogCountdownElement${id}`).showModal();
  269. const dialog = document.createElement('dialog');
  270. dialog.id = `dialogCountdownElement${id}`;
  271. const form = document.createElement('form');
  272. form.method = 'dialog';
  273. const messageDiv = document.createElement('div');
  274. const messageLabel = document.createElement('label');
  275. messageLabel.textContent = '更改提示信息:';
  276. messageLabel.htmlFor = `message${id}`;
  277. const messageInput = document.createElement('input');
  278. messageInput.type = 'text';
  279. messageInput.id = `message${id}`;
  280. messageInput.value = '距离2024年国庆节还有:';
  281. messageDiv.appendChild(messageLabel);
  282. messageDiv.appendChild(messageInput);
  283. const targetDateDiv = document.createElement('div');
  284. const targetDateLabel = document.createElement('label');
  285. targetDateLabel.textContent = '更改目标日期:';
  286. targetDateLabel.htmlFor = `targetDate${id}`;
  287. const targetDateInput = document.createElement('input');
  288. targetDateInput.type = 'datetime-local';
  289. targetDateInput.id = `targetDate${id}`;
  290. targetDateDiv.appendChild(targetDateLabel);
  291. targetDateDiv.appendChild(targetDateInput);
  292. const closeButton = document.createElement('button');
  293. closeButton.type = 'submit';
  294. closeButton.textContent = '关闭';
  295. closeButton.style.display = 'block';
  296. closeButton.style.margin = '5px auto';
  297. form.appendChild(messageDiv);
  298. form.appendChild(targetDateDiv);
  299. form.appendChild(closeButton);
  300. dialog.appendChild(form);
  301. countdownContainer.appendChild(button);
  302. countdownContainer.appendChild(dialog);
  303. }
  304. for (let i = 1; i <= 4; i++) {
  305. createCountdownElement(i);
  306. }
  307. /* 倒计时功能渲染 结束*/
  308. /* 倒计时功能实现*/
  309. const countdownData = {
  310. 1: { message: 'message1', targetDate: 'targetDate1' },
  311. 2: { message: 'message2', targetDate: 'targetDate2' },
  312. 3: { message: 'message3', targetDate: 'targetDate3' },
  313. 4: { message: 'message4', targetDate: 'targetDate4' }
  314. };
  315. // 加载时从localStorage读取数据
  316. window.onload = function () {
  317. for (let id in countdownData) {
  318. const message = localStorage.getItem(countdownData[id].message);
  319. const targetDate = localStorage.getItem(countdownData[id].targetDate);
  320. if (message) {
  321. document.getElementById(countdownData[id].message).value = message;
  322. }
  323. if (targetDate) {
  324. document.getElementById(countdownData[id].targetDate).value = targetDate;
  325. }
  326. }
  327. };
  328. // 保存数据到localStorage
  329. function saveData(id) {
  330. const message = document.getElementById(countdownData[id].message).value;
  331. const targetDate = document.getElementById(countdownData[id].targetDate).value;
  332. localStorage.setItem(countdownData[id].message, message);
  333. localStorage.setItem(countdownData[id].targetDate, targetDate);
  334. }
  335. // 监听输入框的变化并保存数据
  336. for (let id in countdownData) {
  337. document.getElementById(countdownData[id].message).addEventListener('input', () => saveData(id));
  338. document.getElementById(countdownData[id].targetDate).addEventListener('input', () => saveData(id));
  339. }
  340. function showCountdown(id) {
  341. const message = document.getElementById(countdownData[id].message).value;
  342. const targetDateInput = document.getElementById(countdownData[id].targetDate);
  343. const targetDateValue = targetDateInput.value;
  344. const now = new Date();
  345. let targetDate;
  346. if (targetDateValue) {
  347. targetDate = new Date(targetDateValue);
  348. } else {
  349. targetDate = new Date(2024, 9, 1); // 默认值:2024年10月1日
  350. }
  351. const diff = targetDate - now;
  352. const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  353. const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  354. const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  355. const seconds = Math.floor((diff % (1000 * 60)) / 1000);
  356. const countdownElement = document.getElementById(`countdownElement${id}`);
  357. countdownElement.innerHTML = `${message}${days}${hours}小时${minutes}${seconds}秒`;
  358. }
  359. for (let id in countdownData) {
  360. showCountdown(id);
  361. setInterval(() => showCountdown(id), 1000);
  362. }
  363. /* 倒计时功能实现 结束*/
  364. </script>
  365. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/932246
推荐阅读
相关标签
  

闽ICP备14008679号