WEB 作业 即时内容发布前端交互案例

在这里我做的是喜灰为主题的

代码如下

html 复制代码
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>灰太郎大王新品发布会</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background-color: #ffffff00;
            font-family: "Microsoft Yahei", sans-serif;
        }
        ul {
            list-style: none;
        }
        .w {
            width: 900px;
            margin: 20px auto;
            padding: 0 10px;
        }
        /* 顶部标题 */
        .top-title {
            font-size: 18px;
            color: #4171ff;
            margin-bottom: 15px;
            line-height: 1.2;
        }
        /* 输入框区域 */
        .controls {
            width: 100%;
        }
        .controls textarea {
            width: 100%;
            height: 120px;
            resize: none;
            border: 1px solid #5496ff;
            border-radius: 6px;
            outline: none;
            padding: 10px 15px;
            font-size: 14px;
            color: #333;
            margin-bottom: 10px;
            line-height: 1.5;
        }
        .controls textarea::placeholder {
            color: #909399;
        }
        /* 字数和发布按钮 */
        .count-btn-box {
            float: right;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .count-btn-box span {
            font-size: 14px;
            color: #7ba2ef;
        }
        .count-btn-box .useCount {
            color: #6573f6;
            font-weight: 500;
        }
        .count-btn-box button {
            width: 88px;
            height: 32px;
            border: none;
            outline: none;
            background-color: #0084ff;
            color: #fff;
            font-size: 14px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .count-btn-box button:hover {
            background-color: #0078e7;
        }
        .count-btn-box button:disabled {
            background-color: #a0cfff;
            cursor: not-allowed;
        }
        /* 内容列表 */
        .contentList {
            margin-top: 40px;
            width: 100%;
        }
        .contentList li {
            padding: 20px 0;
            border-bottom: 1px dashed #e6e6e6;
            position: relative;
        }
        .contentList li .info {
            position: relative;
            min-height: 60px;
        }
        .contentList li .info img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            object-fit: cover;
        }
        .contentList li .info .username {
            position: absolute;
            top: 5px;
            left: 65px;
            font-size: 15px;
            font-weight: 500;
            color: #333;
        }
        .contentList li .info .send-time {
            position: absolute;
            top: 30px;
            left: 65px;
            font-size: 12px;
            color: #999;
        }
        .contentList li .content {
            margin-left: 65px;
            margin-top: 10px;
            font-size: 14px;
            color: #666;
            line-height: 1.6;
            word-break: break-all;
        }
        .contentList li .the_del {
            position: absolute;
            right: 0;
            top: 20px;
            font-size: 18px;
            color: #999;
            cursor: pointer;
            width: 20px;
            height: 20px;
            line-height: 20px;
            text-align: center;
        }
        .contentList li .the_del:hover {
            color: #f56c6c;
        }
        /* 清除浮动 */
        .clearfix::after {
            content: "";
            display: block;
            clear: both;
        }
    </style>
</head>
<body>
    <div class="w">
        <!-- 顶部标题 -->
        <div class="top-title">灰太郎大王新品发布会</div>
        
        <!-- 操作的界面 -->
        <div class="controls clearfix">
            <textarea placeholder="请讲" id="area" maxlength="200"></textarea>
            <div class="count-btn-box">
                <span class="useCount" id="useCount">0</span>
                <span>/</span>
                <span>200</span>
                <button id="send" disabled>发布</button>
            </div>
        </div>

        <div class="contentList">
            <ul id="list"></ul>
        </div>
    </div>

    <script>
        let dataArr = [
            { uname: '暖羊羊', imgSrc:'./111/1.jpg'},
            { uname: '沸羊羊', imgSrc: './111/2.jpg' },
            { uname: '美羊羊', imgSrc: './111/3.jpg' },
            { uname: '懒羊羊', imgSrc: './111/4.jpg' },
            { uname: '喜羊羊', imgSrc: './111/5.jpg' },
            { uname: '慢羊羊', imgSrc: './111/6.jpg' },
            { uname: '灰太狼', imgSrc: './111/7.jpg' },
        ];

        // 获取DOM元素
        const area = document.getElementById('area');
        const useCount = document.getElementById('useCount');
        const sendBtn = document.getElementById('send');
        const list = document.getElementById('list');

        // 1. 实时字数统计 + 发布按钮状态控制
        area.addEventListener('input', function () {
            const currentLen = this.value.trim().length;
            useCount.textContent = currentLen;
            // 按钮可用条件:字数>0 且 ≤200
            sendBtn.disabled = currentLen === 0 || currentLen > 200;
        });

        // 2. 发布功能
        sendBtn.addEventListener('click', function () {
            const content = area.value.trim();
            if (!content) return;

            // 随机选一个用户
            const randomIdx = Math.floor(Math.random() * dataArr.length);
            const randomUser = dataArr[randomIdx];

            // 生成当前时间
            const now = new Date();
            const formatTime = (num) => num.toString().padStart(2, '0');
            const year = now.getFullYear();
            const month = formatTime(now.getMonth() + 1);
            const day = formatTime(now.getDate());
            const hour = formatTime(now.getHours());
            const minute = formatTime(now.getMinutes());
            const second = formatTime(now.getSeconds());
            const timeStr = `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;

            // 创建发布项
            const li = document.createElement('li');
            li.innerHTML = `
                <div class="info">
                    <img src="${randomUser.imgSrc}" alt="${randomUser.uname}" />
                    <span class="username">${randomUser.uname}</span>
                    <p class="send-time">发布于 ${timeStr}</p>
                </div>
                <div class="content">${content}</div>
                <span class="the_del">×</span>
            `;

            // 插入到列表顶部
            list.insertBefore(li, list.firstChild);

            // 绑定删除事件
            li.querySelector('.the_del').addEventListener('click', function () {
                li.remove();
            });

            // 清空输入框 + 重置字数 + 禁用按钮
            area.value = '';
            useCount.textContent = '0';
            sendBtn.disabled = true;
        });
    </script>
</body>
</html>

运行结果:

相关推荐
HWL56792 小时前
一个CSS属性will-change: transform
前端·css
比特森林探险记2 小时前
后端开发者快速入门react
开发语言·前端·javascript
李松桃2 小时前
python第三次作业
java·前端·python
一起养小猫2 小时前
Flutter for OpenHarmony 实战:ListView与GridView滚动列表完全指南
开发语言·javascript·flutter
熊猫钓鱼>_>2 小时前
从零到一:打造“抗造” Electron 录屏神器的故事
前端·javascript·ffmpeg·electron·node·录屏·record
晚霞的不甘2 小时前
Flutter for OpenHarmony《智慧字典》 App 主页深度优化解析:从视觉动效到交互体验的全面升级
前端·flutter·microsoft·前端框架·交互
我是伪码农2 小时前
Vue 1.28
前端·javascript·vue.js
Andy&lin2 小时前
【医疗】智慧病房APP原型模板
设计模式·产品运营·人机交互·交互·健康医疗
鹓于3 小时前
Excel一键生成炫彩二维码
开发语言·前端·javascript