【前端】使用 HTML、CSS 和 JavaScript 创建一个数字时钟和搜索功能的网页

文章目录


标题 详情
作者 JosieBook
头衔 CSDN博客专家资格、阿里云社区专家博主、软件设计工程师
博客内容 开源、框架、软件工程、全栈(,NET/Java/Python/C++)、数据库、操作系统、大数据、人工智能、工控、网络、程序人生
口号 成为你自己,做你想做的
欢迎三连 👍点赞、✍评论、⭐收藏

⭐前言

在本篇博客中,我们将介绍如何使用 HTML、CSS 和 JavaScript 创建一个包含数字时钟和搜索功能的简单网页。这个网页不仅具有现代的外观,还能实时显示当前时间,并允许用户输入搜索内容。

⭐一、项目结构

我们的项目结构如下所示:

复制代码
test
    ├── index.html
    ├── myCss.css
    └── myJsp.js
  • index.html:主页面文件,包含 HTML 结构和基本样式。
  • myCss.css:自定义样式文件,用于美化页面。
  • myJsp.js:JavaScript 文件,负责实现时钟和搜索功能的逻辑。

⭐二、HTML 结构

index.html 文件中,我们首先定义了基本的 HTML 结构。我们使用了 Bootstrap 框架来快速构建响应式布局。以下是主要的 HTML 代码片段:

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="XX科技公司 - 领先的软件开发解决方案提供商">
    <title>XX科技公司 | 创新驱动未来</title>

    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" type="text/css">

    <link rel="stylesheet" href="myCss.css">
</head>
<body>
<i class="fas fa-ellipsis-v uil uil-ellipsis-v dot-menu-btn" style="color:#fff" id="active-menu"></i>
<ul class="dot-menu" id="active-menu">
    <li class="menu-item" id="active-menu">
        <span class="clock-format-text" id="active-menu">24-Hour Format</span>
        <div class="format-switch-btn" id="active-menu" date-format="12"></div>
    </li>
</ul>

<div class="container">
    <div class="row height d-flex justify-content-center align-items-center">
        <div class="col-md-8">
            <div class="search">
                <i class="fa fa-search"></i>
                <input type="text" class="form-control" id="search-input" placeholder="Have a question? Ask Now">
                <button class="btn btn-primary" id="search-button">Search</button>
            </div>
        </div>
    </div>
</div>

<div class="time">
    <span class="hours">00</span>
    <span class="dots">:</span>
    <span class="minutes">00</span>
    <div class="right-side">
        <span class="period">AM/PM</span>
        <span class="seconds">00</span>
    </div>
</div>

<div class="calender">
    <span class="month-name">Month</span>,
    <span class="day-name">Day</span>
    <span class="day-num">00</span>
</div>

<script src="myJsp.js"></script>
</body>
</html>

⭐三、CSS 样式

myCss.css 文件中,我们使用了一些 CSS 来美化页面的外观。以下是一些关键的样式:

css 复制代码
@import url("https://fonts.googleapis.com/css2?family=Poppins:weight@100;200;300;400;500;600;700;800&display=swap");


* {
    font-family: 'Poppins', sans-serif;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #2e2e44;
}

.height {
    height: 100vh
}

.search {
    position: relative;
    box-shadow: 0 0 40px rgba(51, 51, 51, .1)
}

.search input {
    height: 60px;
    text-indent: 25px;
    border: 2px solid #d6d4d4
}

.search input:focus {
    box-shadow: none;
    border: 2px solid blue
}

.search .fa-search {
    position: absolute;
    top: 20px;
    left: 16px
}

.search button {
    position: absolute;
    top: 5px;
    right: 5px;
    height: 50px;
    width: 110px;
    background: blue
}




.digital-clock {
    position: relative;
    color: #fff;
    background: #2e2e44;
    width: 425px;
    padding: 20px 45px;
    box-shadow: 0 5px 25px rgba(14, 21, 37, 0.8);
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.digital-clock:before {
    content: '';
    position: absolute;
    background: linear-gradient(45deg, #24ff6d, #2f93f1, #ff5e9a);
    background-size: 200% 200%;
    top: -5px;
    left: -5px;
    bottom: -5px;
    right: -5px;
    z-index: -1;
    filter: blur(40px);
    animation: glowing 10s ease infinite;
}

@keyframes glowing {
    0% {
        background-position: 0 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0 50%;
    }
}

.time {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;

}

.hours,
.dots,
.minutes {
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 600;
    padding: 0 10px;
    line-height: 125px;
}

.hours,
.minutes {
    font-size: 6.5em;
    width: 125px;
}

.dots {
    font-size: 5em;
    color: #929292;
}

.hours {
    background: -webkit-linear-gradient(90deg, #634dff, #5fd4ff);
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
}


.minutes {
    background: -webkit-linear-gradient(90deg, #ff5e9e, #ffb960);
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
}

.right-side {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    margin-left: 10px;
}


.period,
.seconds {
    font-size: 1.2em;
    font-weight: 500;
}

.period {
    transform: translateY(-20px);
    background: -webkit-linear-gradient(90deg, #f7b63f, #faf879);
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
}


.seconds {
    transform: translateY(16px);
    background: -webkit-linear-gradient(90deg, #24ff6d, #2f93f1);
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
}

.calender {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.3em;
    font-weight: 500;
    margin-bottom: 5px;
    background: -webkit-linear-gradient(90deg, #ae4af6, #ff98d1);
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
}

.day-name,
.day-num,
.year {
    margin-left: 8px;
}

.dot-menu-btn {
    position: absolute;
    top: 0;
    right: 0;
    margin: 10px;
    color: #efefef;
    font-size: 1.5em;
    cursor: pointer;
}

.dot-menu {
    z-index: 999;
    position: absolute;
    top: 7px;
    right: 5px;
    list-style: none;
    background: #353e54;
    padding: 10px 20px;
    border-radius: 5px;
    box-shadow: 0 5px 25px rgba(0, 0, 0, 0.5);
    visibility: hidden;
    opacity: 0;
    transition: 0.4s ease;
}

.dot-menu.active {
    visibility: visible;
    opacity: 1;
}

.menu-item {
    display: flex;
    justify-content: center;
    align-items: center;
}


.clock-format-text {
    color: #efefef;
    font-size: 0.9em;
    margin-right: 20px;
}

.format-switch-btn {
    width: 35px;
    height: 15px;
    background: #485470;
    border-radius: 75px;
    box-shadow: inset 2px 2px 4px rgba(255, 255, 255, 0.1),
    inset -2px -2px 4px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
}

.format-switch-btn:before {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    background: #ff5e9a;
    border-radius: 50%;
    box-shadow: 0 5px 25px #ff5e9a;
    transform: translateX(-10px);
    transition: 0.4s ease;
    transition-property: background, transform;
}

.format-switch-btn.active:before {
    background: #0bff8d;
    box-shadow: 0 5px 25px #0bff8d;
    transform: translateX(10px);
}

⭐四、JavaScript 功能

myJsp.js 文件中,我们实现了数字时钟和搜索功能。以下是相关的 JavaScript 代码:

javascript 复制代码
//js for switch clock format

const formatSwitchBtn = document.querySelector(".format-switch-btn");

formatSwitchBtn.addEventListener("click", () => {
    formatSwitchBtn.classList.toggle("active");

    var formatValue = formatSwitchBtn.getAttribute("date-format");

    if (formatValue === "12") {
        formatSwitchBtn.setAttribute("date-format", "24");
    }
    else {
        formatSwitchBtn.setAttribute("date-format", "12");
    }
});


//Get Current Time In Javascript
function clock() {
    var today = new Date();

    var hours = today.getHours();
    var minutes = today.getMinutes();
    var seconds = today.getSeconds();
    let period = "AM";

    //set the time period
    if (hours >= 12) {
        period = "PM";
    }

    //set 12 hour clock format
    var formatValue = formatSwitchBtn.getAttribute("date-format");

    if (formatValue === "12") {
        hours = hours > 12 ? hours % 12 : hours;
    }




    //add 0 for the value lower than 10
    if (hours < 10) {
        hours = "0" + hours;
    }

    if (minutes < 10) {
        minutes = "0" + minutes;
    }

    if (seconds < 10) {
        seconds = "0" + seconds;
    }


    document.querySelector(".hours").innerHTML = hours;
    document.querySelector(".minutes").innerHTML = minutes;
    document.querySelector(".period").innerHTML = period;
    document.querySelector(".seconds").innerHTML = seconds;
}
var updateClock = setInterval(clock, 1000);

// get the date in js

var today = new Date();
const dayNum = today.getDate();
const year = today.getFullYear();
const dayName = today.toLocaleString("default", { weekday: "long" });
const monthName = today.toLocaleString("default", { month: "short" });

document.querySelector(".month-name").innerHTML = monthName;
document.querySelector(".day-name").innerHTML = dayName;
document.querySelector(".day-num").innerHTML = dayNum;
// document.querySelector(".year").innerHTML = year;

//js for dot menu toglle

const dotmenuBtn = document.querySelector(".dot-menu-btn");
const dotMenu = document.querySelector(".dot-menu");
dotmenuBtn.addEventListener("click", () => {
    dotMenu.classList.toggle("active");
});

document.addEventListener("click", (e) => {
    if (e.target.id !== "active-menu") {
        dotMenu.classList.remove("active");
    }
});

// 添加搜索按钮的点击事件
document.getElementById("search-button").addEventListener("click", function() {
    var searchInput = document.getElementById("search-input").value;
    if (searchInput.trim() === "") {
        alert("请输入搜索内容!");
    } else {
        alert("您搜索的内容是: " + searchInput);
    }
});

⭐五、运行效果

调整时间格式:

按钮提交提示:

⭐总结

通过以上步骤,我们成功构建了一个包含数字时钟和搜索功能的网页。用户可以在输入框中输入问题并点击搜索按钮,页面会弹出提示框显示用户输入的内容。同时,数字时钟会实时更新,显示当前的时间。

这个项目展示了如何结合 HTML、CSS 和 JavaScript 创建一个简单而实用的网页应用。希望这个示例能为你的网页开发之旅提供灵感!

相关推荐
要天天开心啊2 分钟前
vue3的v-model
前端·javascript·vue.js
纸鸾1 小时前
异步容错实战:基于promise的请求重试
前端·javascript
岭子笑笑1 小时前
yocto-queue 队列 链表源码学习
前端
猫说要有光1 小时前
理解Javascript面向对象
前端·javascript
nppe61 小时前
nuxt3 搭建基础模板
前端
朝与暮1 小时前
Promise详解(包含手写promise)
前端·promise
HHHHHY1 小时前
js写了个鼠标进入卡片3D效果的demo
前端·javascript
曹瑞曹瑞1 小时前
dify+vue+java接入大模型流式输出
java·前端·vue.js
小姐姐呀~2 小时前
使用react 引入相对路径文件
前端·react.js·前端框架
树上有只程序猿2 小时前
MQ 如何保证数据一致性?
前端