重新修改倒计时

2024-08-21 20:52
文章标签 倒计时 修改 重新

本文主要是介绍重新修改倒计时,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>倒计时重新布局</title><style></style>
</head><body><div id="countdownContainer"></div><script>// 创建倒计时容器的HTML内容const countdownContainer = document.getElementById('countdownContainer');countdownContainer.innerHTML = `<div id="countdowns"><!--动态倒计时区 --></div><!-- 用户区编辑区 --><button onclick="addCountdown.show()">添加</button><dialog id="addCountdown"><form><button id="cancelAdd" type="submit">取消添加</button><br><input type="text" id="promptInput" placeholder="提示信息"><br><input type="datetime-local" id="datetimeInput"><button id="saveButton" onclick="saveCountdown()">保存</button></form><p>主要节日时间表</p><ul><li>星尘兑换纠缠之缘重置时间:2024年09月01日04点00分</li><li>幻想真境剧诗重置时间:2024年09月01日04点00分</li><li>渊月螺旋重置时间:2024年09月16日04点00分</li><li>4.8版本下半场限时祈愿结束:2024年08月27日15点00分</li><li>元旦:2025年1月1日</li><li>春节:2025年2月12日</li><li>清明节:2025年4月4日</li><li>劳动节:2025年5月1日</li><li>端午节:2025年6月2日</li><li>中秋节:2025年9月21日</li><li>国庆节:2025年10月1日</li>     </ul></dialog><button class="edit" onclick="editSelected.show()" style="display: none;">修改选中</button><button class="delete" onclick="deleteSelected()" style="display: none;">删除选中</button>`;function saveCountdown() {const promptInput = document.getElementById('promptInput').value.trim();const datetimeInput = document.getElementById('datetimeInput').value;if (promptInput && datetimeInput) {const countdownDiv = document.getElementById('countdowns');const timestamp = Date.now();const countdownElement = document.createElement('div');countdownElement.innerHTML = `<strong>${promptInput}</strong>: <span id="countdown_${timestamp}"></span><input type="checkbox" class="countdownCheckbox" value="${timestamp}">`;countdownDiv.appendChild(countdownElement);updateCountdown(`countdown_${timestamp}`, new Date(datetimeInput));// 存储倒计时信息到 localStoragelocalStorage.setItem(`countdown_${timestamp}`, JSON.stringify({prompt: promptInput,datetime: datetimeInput}));} else {alert('请填写提示信息和目标日期');}}function updateCountdown(elementId, targetDate) {const countdownElement = document.getElementById(elementId);function update() {// 计算剩余时间function getTimeRemaining(endDate) {const total = Date.parse(endDate) - Date.parse(new Date());const absTotal = Math.abs(total);const seconds = Math.floor((absTotal / 1000) % 60);const minutes = Math.floor((absTotal / 1000 / 60) % 60);const hours = Math.floor((absTotal / (1000 * 60 * 60)) % 24);const days = Math.floor(absTotal / (1000 * 60 * 60 * 24));const timeStr = `${days}天 ${padZero(hours)}小时 ${padZero(minutes)}分钟 ${padZero(seconds)}秒`;return total <= 0 ? `已过去${timeStr}` : timeStr;}// 补零function padZero(num) {return num < 10 ? `0${num}` : num;}// 更新倒计时显示countdownElement.textContent = getTimeRemaining(targetDate);}update();setInterval(update, 1000);}// 页面加载时从 localStorage 中恢复倒计时window.onload = function () {for (let i = 0; i < localStorage.length; i++) {const key = localStorage.key(i);if (key.startsWith('countdown_')) {const data = JSON.parse(localStorage.getItem(key));const timestamp = key.split('_')[1];const countdownDiv = document.getElementById('countdowns');const countdownElement = document.createElement('div');countdownElement.innerHTML = `<strong>${data.prompt}</strong>: <span id="countdown_${timestamp}"></span><input type="checkbox" class="countdownCheckbox" value="${timestamp}">`;countdownDiv.appendChild(countdownElement);updateCountdown(`countdown_${timestamp}`, new Date(data.datetime));}}};// 监听复选框状态变化document.addEventListener('change', function (event) {const checkboxes = document.querySelectorAll('.countdownCheckbox:checked');const editButton = document.querySelector('.edit');const deleteButton = document.querySelector('.delete');if (checkboxes.length > 0) {editButton.style.display = 'inline-block';deleteButton.style.display = 'inline-block';} else {editButton.style.display = 'none';deleteButton.style.display = 'none';}});// 删除选中的倒计时document.querySelector('.delete').addEventListener('click', function () {// 弹出确认对话框const confirmDelete = confirm("确定要删除选中的倒计时吗?");if (confirmDelete) {const checkboxes = document.querySelectorAll('.countdownCheckbox:checked');checkboxes.forEach(checkbox => {const timestamp = checkbox.value;localStorage.removeItem(`countdown_${timestamp}`);checkbox.parentNode.remove();});// 清空所有复选框的选中状态,并隐藏编辑和删除按钮document.querySelectorAll('.countdownCheckbox').forEach(checkbox => {checkbox.checked = false;});document.querySelector('.edit').style.display = 'none';document.querySelector('.delete').style.display = 'none';}// 如果用户选择取消,则不执行任何操作});// 编辑选中的倒计时document.querySelector('.edit').addEventListener('click', function () {const checkboxes = document.querySelectorAll('.countdownCheckbox:checked');if (checkboxes.length === 0) return; // 如果没有选中的复选框,直接返回checkboxes.forEach(checkbox => {const timestamp = checkbox.value;const data = JSON.parse(localStorage.getItem(`countdown_${timestamp}`));// 弹出对话框或表单,让用户修改提示信息和目标日期const newPrompt = prompt('请输入新的提示信息', data.prompt);const newDatetime = prompt('请输入新的目标日期(格式:YYYY-MM-DD HH:MM,例如:2023-10-05 04:00)', data.datetime.replace('T', ' '));if (newPrompt && newDatetime) {// 更新 localStorage 中的数据localStorage.setItem(`countdown_${timestamp}`, JSON.stringify({prompt: newPrompt,datetime: newDatetime}));// 更新页面上的显示const countdownElement = document.getElementById(`countdown_${timestamp}`).parentNode;countdownElement.innerHTML = `<strong>${newPrompt}</strong>: <span id="countdown_${timestamp}"></span><input type="checkbox" class="countdownCheckbox" value="${timestamp}">`;updateCountdown(`countdown_${timestamp}`, new Date(newDatetime));}});// 清空所有复选框的选中状态,并隐藏编辑和删除按钮document.querySelectorAll('.countdownCheckbox').forEach(checkbox => {checkbox.checked = false;});document.querySelector('.edit').style.display = 'none';document.querySelector('.delete').style.display = 'none';});</script>
</body></html>

 

这篇关于重新修改倒计时的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1094257

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Oracle修改端口号之后无法启动的解决方案

《Oracle修改端口号之后无法启动的解决方案》Oracle数据库更改端口后出现监听器无法启动的问题确实较为常见,但并非必然发生,这一问题通常源于​​配置错误或环境冲突​​,而非端口修改本身,以下是系... 目录一、问题根源分析​​​二、保姆级解决方案​​​​步骤1:修正监听器配置文件 (listener.

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Linux修改pip临时目录方法的详解

《Linux修改pip临时目录方法的详解》在Linux系统中,pip在安装Python包时会使用临时目录(TMPDIR),但默认的临时目录可能会受到存储空间不足或权限问题的影响,所以本文将详细介绍如何... 目录引言一、为什么要修改 pip 的临时目录?1. 解决存储空间不足的问题2. 解决权限问题3. 提