网页记事本

网页记事本工具简单易用,一个直观的笔记编辑体验。用户可以在文本区域中输入笔记,并通过点击“保存”按钮将笔记保存为 .txt 文件,文件名包含当前的时间以便于管理。点击“重置”按钮会清空文本区域,方便开始新的笔记。点击“打开”按钮则可以选择本地 .txt 文件内容到文本区域。

开发技术:
HTML: 用于构建页面结构。
CSS: 通过 Flexbox 布局和样式提升用户界面体验。
JavaScript: 实现动态功能,如文件保存、读取和时间更新。利用 Blob 和 FileReader API 处理文件操作,并通过定时器每秒更新时间戳。
网页记事本_第1张图片

DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>记事本工具title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #e0e0e0;
        }

        #notepad {
            width: 90%;
            max-width: 600px;
            height: 80%;
            max-height: 500px;
            display: flex;
            flex-direction: column;
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        textarea {
            flex: 1;
            border: none;
            padding: 20px;
            font-size: 16px;
            line-height: 1.6;
            resize: none;
            outline: none;
            box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
        }

        button {
            padding: 15px;
            border: none;
            color: #ffffff;
            font-size: 18px;
            cursor: pointer;
            transition: opacity 0.3s ease;
            flex: 1;
            margin: 5px;
            border-radius: 5px;
        }

        button:hover {
            opacity: 0.8;
        }

        #saveButton {
            background-color: #28a745;
        }

        #resetButton {
            background-color: #dc3545;
        }

        #openButton {
            background-color: #007bff;
        }

        #timestamp {
            padding: 10px;
            background-color: #f1f1f1;
            border-bottom: 1px solid #ddd;
            text-align: center;
            font-size: 16px;
            font-weight: bold;
        }

        .button-container {
            display: flex;
            padding: 10px;
        }
    style>
head>
<body>
    <div id="notepad">
        <div id="timestamp">div>
        <textarea id="notes" placeholder="在这里输入笔记...">textarea>
        <div class="button-container">
            <button id="saveButton" onclick="saveNotes()">保存button>
            <button id="resetButton" onclick="resetNotes()">重置button>
            <button id="openButton" onclick="openFile()">打开button>
        div>
    div>

       <input type="file" id="fileInput" style="display: none;" accept=".txt" onchange="loadFile(event)">

    <script>
        function updateTimestamp() {
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0');
            const day = String(now.getDate()).padStart(2, '0');
            const weekday = now.toLocaleDateString('zh-CN', { weekday: 'long' });
            const hour = String(now.getHours()).padStart(2, '0');
            const minute = String(now.getMinutes()).padStart(2, '0');
            const second = String(now.getSeconds()).padStart(2, '0');
            const timestamp = `${year}${month}${day}${weekday} ${hour}:${minute}:${second}`;
            document.getElementById('timestamp').textContent = timestamp;
        }

        function saveNotes() {
            const notes = document.getElementById('notes').value;
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0');
            const day = String(now.getDate()).padStart(2, '0');
            const hour = String(now.getHours()).padStart(2, '0');
            const minute = String(now.getMinutes()).padStart(2, '0');
            const second = String(now.getSeconds()).padStart(2, '0');
            const filename = `${year}-${month}-${day}-${hour}-${minute}-${second}.txt`;

            const blob = new Blob([notes], { type: 'text/plain;charset=utf-8' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            setTimeout(() => {
                URL.revokeObjectURL(url);
                document.body.removeChild(a);
            }, 0);
               document.getElementById('notes').value = '';
        }

        function resetNotes() {
            document.getElementById('notes').value = '';
        }

        function openFile() {
            document.getElementById('fileInput').click();
        }

        function loadFile(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('notes').value = e.target.result;
                };
                reader.readAsText(file);
            }
        }

        updateTimestamp();
        setInterval(updateTimestamp, 1000);
    script>
body>
html>

你可能感兴趣的:(css,前端)