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

news2024/10/3 6:35:15

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>4个自定义倒计时</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            user-select: none;

            body {
                background: #0b1b2c;
            }
        }

        header {
            width: 100vw;
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #cbcbcb;

            /* logo */
            figure.logo {
                width: 262px;
                height: 50px;
                /* 缩小 */
                transform: scale(0.8);
                margin-top: 2px;
                margin-left: -15px;
                border-radius: 50%;
                background: #f30303;
                box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6),
                    inset -4px -4px 5px rgba(0, 0, 0, 0.6);
                z-index: 99;

                figcaption {
                    background: hsl(0, 0%, 0%);
                    background-clip: text;
                    -webkit-background-clip: text;
                    -webkit-text-fill-color: transparent;
                    font-weight: bold;
                    box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6), inset -4px -4px 5px rgba(0, 0, 0, 0.6);
                    letter-spacing: -3px;
                    line-height: 45px;
                    border-radius: 50%;
                    font-size: 40px;
                    width: 152px;
                }

                .my_name1 {
                    text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.726);
                    clip-path: polygon(0% 0%, 100% 0%, 100% 50%, 0% 50%);
                    transform: translate(35%, 0%);
                }

                .my_name2 {
                    text-shadow: 1px 1px 1px hsla(160, 100%, 37%, 0.555);
                    clip-path: polygon(0% 50%, 100% 50%, 100% 100%, 0% 100%);
                    transform: translate(35%, -93%);
                }

                img {
                    width: 60px;
                    height: 35px;
                    border-radius: 50%;
                    transition: transform 0.3s ease;
                    position: absolute;

                    &:hover {
                        transform: scale(1.3);
                        filter: drop-shadow(0 0 0.3em #ff0202);
                    }
                }

                .kong {
                    margin-top: 7px;
                    margin-left: 200px;
                }

                .ying {
                    top: 7px;
                    margin-left: 2px;
                }
            }

            /* 时间 */
            time {
                background: -webkit-linear-gradient(315deg, #e1ff00 50%, #ff0000);
                background-clip: text;
                -webkit-background-clip: text;
                -webkit-text-fill-color: transparent;
                font-size: 1.5rem;
                font-weight: 900;
                text-shadow: 2px -1px 8px rgba(250, 80, 193, 0.323);

                sub {
                    -webkit-text-fill-color: #cfd601;
                    text-shadow: 1px 1px 1px #100000;
                    font-size: 1rem;
                }
            }

            /* 全屏还原关闭按钮 */
            menu {
                display: flex;

                button {
                    background: none;
                    border: none;
                }

                img {
                    width: 35px;
                    margin-top: 5px;
                    margin-right: 5px;
                    cursor: pointer;

                    &:hover {
                        transform: scale(1.2);
                        padding: 5px;
                        background: #ffd000;
                    }
                }

                button:nth-child(2) {
                    display: none;
                    /* 放大镜效果 */
                    transform: scale(1.16);
                }
            }
        }

        main {
            display: flex;
            flex-direction: column;
            align-items: center;

            button {
                font-size: 1.5rem;
                color: #d6d301;
                text-shadow: 1px 1px 1px #100000;
                font-weight: bold;
                cursor: pointer;
                border-radius: 3px;
                background-color: #f30303;
                border: none;
                margin: 5px;
                padding: 3px;
            }

            dialog {
                transform: translate(180%, 80%);
            }

            dialog::backdrop {
                /* 模糊背景 */
                backdrop-filter: blur(2px);
            }

            div {
                margin-top: 20px;
                color: #67c23a;
                text-shadow: 1px 1px 1px #100000;
            }
        }
    </style>
</head>

<body>
    <header>
        <!-- logo -->
        <figure class="logo">
            <a href="https://blog.csdn.net/lulei5153?spm=1011.2415.3001.5343" title="与妖为邻CSDN博客" target="_blank">
                <img class="kong" src="file:///D:/img/icon.ico" alt="与妖为邻">
            </a>
            <figcaption class="my_name1">与妖为邻</figcaption>
            <figcaption class="my_name2">与妖为邻</figcaption>
            <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"
                title="首页" target="_blank">
                <img class="ying" src="file:///D:/img/ying.png" alt="与妖为邻">
            </a>
        </figure>
        <!-- 时间 -->
        <time datetime="2024-07-10" title="2024-07-10" id="dateTime">2024-07-10</time>
        <!-- 天气 -->
        <iframe class="tianqi" frameborder="0" width="280" height="36" scrolling="no" hspace="0"
            src="https://i.tianqi.com/?c=code&id=99">
        </iframe>
        <!-- 全屏还原关闭按钮 -->
        <menu>
            <button class="fullScreen" type="button"><img src="file:///D:/img/全屏.svg" alt="全屏"
                    onclick="fullScreen()"></button>
            <button class="exitFullScreen" type="button"><img src="file:///D:/img/还原.svg" alt="还原"
                    onclick="exitFullScreen()"></button>
            <button type="button"><img src="file:///D:/img/关闭.svg" alt="关闭" onclick="closeAll()"> </button>
        </menu>
    </header>
    <main id="countdownContainer">
        <div>
            <h2>2025主要节日时间表</h2>
            <ul>
                <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>
        </div>
    </main>
</body>
<script>
    /* 现在时间*/
    var current_time = document.getElementById("dateTime");
    function showTime() {
        var now = new Date();
        var year = now.getFullYear();
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        var day = ("0" + now.getDate()).slice(-2);
        var hour = ("0" + now.getHours()).slice(-2);
        var min = ("0" + now.getMinutes()).slice(-2);
        var second = ("0" + now.getSeconds()).slice(-2);
        var week = [
            "星期日",
            "星期一",
            "星期二",
            "星期三",
            "星期四",
            "星期五",
            "星期六",
        ][now.getDay()];
        var time = `${year}-${month}-${day}<sub id='sub'>${week}</sub> ${hour}:${min}:${second}`;
        current_time.innerHTML = time;
    }
    showTime();
    setInterval(showTime, 1000);
    /* 现在时间 结束*/
    /* 全屏功能与关闭*/
    function fullScreen() {
        var elem = document.documentElement;
        if (elem.requestFullscreen) {
            elem.requestFullscreen();
        } else if (elem.mozRequestFullScreen) { // Firefox
            elem.mozRequestFullScreen();
        } else if (elem.webkitRequestFullscreen) { // Chrome, Safari and Opera
            elem.webkitRequestFullscreen();
        } else if (elem.msRequestFullscreen) { // IE/Edge
            elem.msRequestFullscreen();
        }
    }
    function exitFullScreen() {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.mozCancelFullScreen) { // Firefox
            document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) { // Chrome, Safari and Opera
            document.webkitExitFullscreen();
        } else if (document.msExitFullscreen) { // IE/Edge
            document.msExitFullscreen();
        }
    }
    // 获取全屏和还原按钮
    var fullScreenBtn = document.querySelector(".fullScreen");
    var exitFullScreenBtn = document.querySelector(".exitFullScreen");
    // 监听全屏和还原事件
    document.addEventListener("fullscreenchange", function () {
        if (document.fullscreenElement) {
            fullScreenBtn.style.display = "none";
            exitFullScreenBtn.style.display = "block";
        } else {
            fullScreenBtn.style.display = "block";
            exitFullScreenBtn.style.display = "none";
        }
    });
    // 关闭当前窗口
    function closeAll() {
        window.close();
    }
    /* 全屏功能与关闭 结束*/
    /* 倒计时功能渲染*/
    const countdownContainer = document.getElementById('countdownContainer');
    function createCountdownElement(id) {
        const button = document.createElement('button');
        button.textContent = `第${id}个倒计时`;
        button.id = `countdownElement${id}`;
        button.onclick = () => document.getElementById(`dialogCountdownElement${id}`).showModal();
        const dialog = document.createElement('dialog');
        dialog.id = `dialogCountdownElement${id}`;
        const form = document.createElement('form');
        form.method = 'dialog';
        const messageDiv = document.createElement('div');
        const messageLabel = document.createElement('label');
        messageLabel.textContent = '更改提示信息:';
        messageLabel.htmlFor = `message${id}`;
        const messageInput = document.createElement('input');
        messageInput.type = 'text';
        messageInput.id = `message${id}`;
        messageInput.value = '距离2024年国庆节还有:';
        messageDiv.appendChild(messageLabel);
        messageDiv.appendChild(messageInput);
        const targetDateDiv = document.createElement('div');
        const targetDateLabel = document.createElement('label');
        targetDateLabel.textContent = '更改目标日期:';
        targetDateLabel.htmlFor = `targetDate${id}`;
        const targetDateInput = document.createElement('input');
        targetDateInput.type = 'datetime-local';
        targetDateInput.id = `targetDate${id}`;
        targetDateDiv.appendChild(targetDateLabel);
        targetDateDiv.appendChild(targetDateInput);
        const closeButton = document.createElement('button');
        closeButton.type = 'submit';
        closeButton.textContent = '关闭';
        closeButton.style.display = 'block';
        closeButton.style.margin = '5px auto';
        form.appendChild(messageDiv);
        form.appendChild(targetDateDiv);
        form.appendChild(closeButton);
        dialog.appendChild(form);
        countdownContainer.appendChild(button);
        countdownContainer.appendChild(dialog);
    }
    for (let i = 1; i <= 4; i++) {
        createCountdownElement(i);
    }
    /* 倒计时功能渲染 结束*/
    /* 倒计时功能实现*/
    const countdownData = {
        1: { message: 'message1', targetDate: 'targetDate1' },
        2: { message: 'message2', targetDate: 'targetDate2' },
        3: { message: 'message3', targetDate: 'targetDate3' },
        4: { message: 'message4', targetDate: 'targetDate4' }
    };
    // 加载时从localStorage读取数据
    window.onload = function () {
        for (let id in countdownData) {
            const message = localStorage.getItem(countdownData[id].message);
            const targetDate = localStorage.getItem(countdownData[id].targetDate);
            if (message) {
                document.getElementById(countdownData[id].message).value = message;
            }
            if (targetDate) {
                document.getElementById(countdownData[id].targetDate).value = targetDate;
            }
        }
    };
    // 保存数据到localStorage
    function saveData(id) {
        const message = document.getElementById(countdownData[id].message).value;
        const targetDate = document.getElementById(countdownData[id].targetDate).value;
        localStorage.setItem(countdownData[id].message, message);
        localStorage.setItem(countdownData[id].targetDate, targetDate);
    }
    // 监听输入框的变化并保存数据
    for (let id in countdownData) {
        document.getElementById(countdownData[id].message).addEventListener('input', () => saveData(id));
        document.getElementById(countdownData[id].targetDate).addEventListener('input', () => saveData(id));
    }
    function showCountdown(id) {
        const message = document.getElementById(countdownData[id].message).value;
        const targetDateInput = document.getElementById(countdownData[id].targetDate);
        const targetDateValue = targetDateInput.value;
        const now = new Date();
        let targetDate;
        if (targetDateValue) {
            targetDate = new Date(targetDateValue);
        } else {
            targetDate = new Date(2024, 9, 1); // 默认值:2024年10月1日
        }
        const diff = targetDate - now;
        const days = Math.floor(diff / (1000 * 60 * 60 * 24));
        const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((diff % (1000 * 60)) / 1000);
        const countdownElement = document.getElementById(`countdownElement${id}`);
        countdownElement.innerHTML = `${message}${days}天${hours}小时${minutes}分${seconds}秒`;
    }
    for (let id in countdownData) {
        showCountdown(id);
        setInterval(() => showCountdown(id), 1000);
    }
    /* 倒计时功能实现 结束*/
</script>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1978942.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

(javaweb)maven--Java项目的构建工具

目录 一.Maven概述 二.Idea导入maven项目 三.maven核心功能--依赖管理 四.依赖管理--依赖传递 五.依赖管理--依赖范围 六.依赖管理--生命周期 一.Maven概述 1.管理和构建Java项目的工具 2.Apache开源&#xff08;源代码开放&#xff09; 3. 4. Maven核心配置文件&#xff…

解题 - 左旋字符串的三种解法(思路)

文章目录 前言&#xff1a; 题干&#xff1a; 解题思路&#xff1a; 思路一&#xff1a; 思路二&#xff1a; 思路三&#xff1a; 小结 前言&#xff1a; 路漫漫其修远兮&#xff0c;吾将上下而求索。 题干&#xff1a; 解题思路&#xff1a; 创建变量k &#xff0c;用…

【知识专栏丨python数分实战】关于电商零售客户细分数据分析及可视化

今天这篇文章将给大家介绍关于电商零售客户细分数据分析及可视化的案例分析。 01 数据整理 导入数据 import pandas as pdimport numpy as npfrom pyecharts.charts import *import pyecharts.options as opts import warningswarnings.filterwarnings(ignore) 数据读取及预…

shell的基础介绍

文章目录 shell数组读取数组关联数组获取数组的长度 Shell运算符算术运算符关系运算符布尔运算符逻辑运算符字符串运算符文件测试运算符 Shell echo命令1.显示普通字符串2.显示转义字符3.显示变量4.显示换行5.显示不换行6.显示结果定向至文件7.原样输出字符串&#xff0c;不进行…

Macbook上运行Windows系统工具Parallels Desktop19

亲爱的笔记本小能手们&#xff0c;你们是否也有这样的困扰&#xff1a;在Macbook上运行Windows系统&#xff0c;却发现虚拟机软件要么卡顿&#xff0c;要么操作复杂&#xff1f;别急&#xff0c;今天就给你们种草一款神器——Parallels Desktop 19。 Parallels Desktop最新绿色…

无人机工程师技术高级证书详解

随着无人机技术的飞速发展&#xff0c;其在航拍、农业、测绘、救援、物流等多个领域的应用日益广泛&#xff0c;对无人机工程师的专业技能与综合素质提出了更高要求。无人机工程师技术高级证书&#xff0c;作为对无人机领域高级工程师专业技能的权威认证&#xff0c;不仅是对个…

QT网络编程

Qt 给用户提供了网络编程的接口&#xff0c;包括TCP、UDP、HTTP三种协议的API以及各种类&#xff0c;可以了解一下。 而在 QT 中想要使用网络编程&#xff0c;必须在pro文件中添加 network 模块&#xff0c;否则无法包含网络编程所需的头文件。 UDP UDP是传输层的协议&#…

Animate软件基础:在时间轴中标识动画

FlashASer&#xff1a;AdobeAnimate2021软件零基础入门教程https://zhuanlan.zhihu.com/p/633230084 FlashASer&#xff1a;实用的各种Adobe Animate软件教程https://zhuanlan.zhihu.com/p/675680471 FlashASer&#xff1a;Animate教程及作品源文件https://zhuanlan.zhihu.co…

实战项目导航

目录 1.AI毕设生成器&#xff08;完善中&#xff09;2.计算机设计大赛案例3.c迷宫游戏4.python学生管理系统教学关注持续更新哦 1.AI毕设生成器&#xff08;完善中&#xff09; 文章链接 &#x1f680; 一键启航&#xff0c;编码从未如此简单&#xff01; 探索Python毕业设计生…

vulnhub之serial

这次我们来做这个靶场 项目地址https://download.vulnhub.com/serial/serial.zip 使用vm新建虚拟机 以下为注意事项 第一步&#xff0c;收集资产 扫描靶场ip netdiscover -i eth0 -r 192.168.177.0/24 抓个包 扫描目录 看到了cookie中有一个user Tzo0OiJVc2VyIjoyOntzOj…

【Golang 面试 - 进阶题】每日 3 题(十八)

✍个人博客&#xff1a;Pandaconda-CSDN博客 &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/UWz06 &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 Golang 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞&#x1f44d;收藏…

算法:BFS 解决拓扑排序

目录 拓扑排序 题目一&#xff1a;课程表 题目二&#xff1a;课程表II 题目三&#xff1a;火星词典 拓扑排序 有向无环图(DAG图) 有向无环图也就是有几个点&#xff0c;每个点之间的线段都是有向的&#xff0c;且任意拿出来几个点&#xff0c;都是无环的&#xff0c;这里…

Adaboost集成学习 | Matlab实现基于ELM-Adaboost极限学习机结合Adaboost集成学习故障诊断

目录 效果一览基本介绍程序设计参考资料效果一览 基本介绍 Adaboost集成学习 | Matlab实现基于ELM-Adaboost极限学习机结合Adaboost集成学习故障诊断 ELM(Extreme Learning Machine)和 Adaboost 都是机器学习领域中常见的算法。ELM 是一种单隐层前馈神经网络,具有快速训练、…

使用 1panel面板 部署 springboot 和 vue

代码仓库&#xff1a;还没弄 目录 网站介绍安装步骤1. 准备云服务器2. 准备域名&#xff08;可跳过&#xff09;3. 安装1panel面板4. 服务器开放端口5. 进入1panel面板6. 安装并启动软件&#xff08;服务器和面板开放端口&#xff09;7. 打包并上传项目7.1 打包 Java项目&#…

算力共享中神经网络切片和算力分配策略

目录 神经网络切片 按照算力的分布进行网络层数切片;就是算力越强,运算神经网络层数越多 神经网络切片和算力占比进行映射 算力分配策略 get_current_shard 神经网络切片 按照算力的分布进行网络层数切片;就是算力越强,运算神经网络层数越多 神经网络切片和算力占比进…

基于RK3588+AI支持能源在线监测系统应用的AIOT产品方案

支持能源在线监测系统应用的AIOT产品方案 近年来&#xff0c;智慧能源行业受益于国家政策扶持、市场需求拉动和先进技术支撑呈现出了蓬勃发展态势。助推智慧能源发展&#xff0c;打造了支持能源在线监测系统应用的AIOT产品方案。 能源在线监测系统的市场潜力 随着社会经济的飞…

C++笔记---类和对象(中)

1. 类的默认成员函数 默认成员函数就是用户没有显式实现&#xff0c;编译器会自动生成的成员函数称为默认成员函数。 一个类&#xff0c;我们不写的情况下编译器会默认生成以下6个默认成员函数&#xff0c;分别为&#xff1a;构造函数&#xff0c;析构函数&#xff0c;拷贝构…

【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…

【vulnhub】W1R3S.inc靶机

靶机安装 下载地址&#xff1a;https://download.vulnhub.com/w1r3s/w1r3s.v1.0.1.zip 运行环境&#xff1a;vmware 信息收集 靶机发现IP扫描 nmap 192.168.93.0/24 端口扫描,发现开放21、22、80、3306端口 nmap -A 192.168.93.159 -p- 进行目录扫描 dirsearach -u http…

函数实例讲解(三)

文章目录 常用的三个数学函数1、绝对值函数ABS2、取整数部分INT3、求余数函数MOD 求极值函数max、min1、Max2、Min 附加条件下求平均数1、AVERAGE2、AVERAGEIF3、AVERAGEIFS VLOOKUP与COLUMN1、VLOOKUP2、COLUMN 查找函数LOOKUP1、基础语法2、向量形式3、数组形式 常用的三个数…