前端小案例——输入框提示词(HTML+CSS+JS, 附源码)

一、前言

实现功能:

这个小案例实现了一个具有动态占位符的文本输入框。它会在输入框中循环显示一组预定义的占位符文本,并且每隔5秒钟就会更换一个占位符文本。这可以用于为用户提供一些提示或引导,让他们知道他们可以在输入框中输入什么内容。

实现逻辑:

  1. 首先,通过document.querySelector('input')获取到页面中的input元素。

  2. 定义了一个currentIndex变量,用于追踪当前占位符的索引。同时,定义了一个placeholders数组,其中包含了一组预定义的占位符文本。

  3. 调用updatePlaceholder函数来更新输入框的占位符文本。

  4. updatePlaceholder函数会将currentIndex对应的占位符文本赋值给输入框的placeholder属性,并将currentIndex自增1。如果currentIndex超过了placeholders数组的长度,则将其重置为0,实现循环显示占位符文本的效果。

  5. 使用setInterval函数每隔5秒钟调用一次updatePlaceholder函数,以更新占位符文本。

二、项目运行效果

三、全部代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>输入框提示词</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
            list-style: none;
            background-repeat: no-repeat;
        }
        .input-box{
            width: 500px;
            height: 50px;
            position: relative;
            left: 50%;
            transform: translate(-50%, 500%);
        }
        .input-box input{
            width: 100%;
            height: 100%;
            padding-left: 25px;
            box-sizing: border-box;
            border-radius: 25px;
            border: 2px solid #89ceb5;
            font-size: 18px;
            line-height: 50px;
        }
        .input-box input:focus { 
            border: 2px solid #5dbea4;
            outline: none; 
            caret-color: #65c4aa; 
        }
        .chakra-button {
            width: 30px;
            height: 30px;
            position: absolute;
            top: 52%;
            right: 3%;
            margin-top: -15px;
            background-color: transparent;
            color: #89ceb5;
            border: none;
            cursor: pointer;
            z-index: 22;
        }
    </style>
</head>
<body>
    <div class="input-box">
        <input type="text">
        <button type="button" class="chakra-button">
            <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32"
                focusable="false" class="chakra-icon css-1cmji0l" aria-hidden="true">
                <path
                    d="M27.71 4.29a1 1 0 0 0-1.05-.23l-22 8a1 1 0 0 0 0 1.87l9.6 3.84l3.84 9.6a1 1 0 0 0 .9.63a1 
                    1 0 0 0 .92-.66l8-22a1 1 0 0 0-.21-1.05zM19 24.2l-2.79-7L21 12.41L19.59 11l-4.83 4.83L7.8 13l17.53-6.33z"
                    fill="currentColor">
                </path>
            </svg>
        </button>
    </div>
</body>
<script>
    const input = document.querySelector('input');
    let currentIndex = 0;
    let placeholders = ['欢迎来到「博主的文章」...', '1月推荐 | 盛冬时节...', '那些涤荡心灵的瞬间...'];
    updatePlaceholder();
    setInterval(updatePlaceholder, 5000);

    function updatePlaceholder() {
        input.placeholder = placeholders[currentIndex];
        currentIndex = (currentIndex + 1) % placeholders.length;
    }
</script>
</html>

四、答疑解惑

这是一个非常适合练习JavaScript的小案例,各位小伙伴可以自行更改样式和内容,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主QQ。

还多请各位小伙伴多多点赞支持,你们的支持是我最大的动力。

博主QQ:1196094293

谢谢各位的支持~~

相关推荐
疯狂动物城在逃flash4 分钟前
数据库入门:SQL学习路线图与实战技巧
前端
前端小巷子9 分钟前
跨域问题解决方案:开发代理
前端·javascript·面试
前端_逍遥生10 分钟前
Chrome 插件开发到发布完整指南:从零开始打造 TTS 朗读助手
前端·chrome
JohnYan10 分钟前
Bun技术评估 - 07 S3
javascript·后端·bun
Mintopia10 分钟前
Three.js 材质与灯光:一场像素级的光影华尔兹
前端·javascript·three.js
天涯学馆11 分钟前
JavaScript 跨域、事件循环、性能优化面试题解析教程
前端·javascript·面试
掘金一周20 分钟前
别再用 100vh 了!移动端视口高度的终极解决方案| 掘金一周7.3
前端·后端
晴殇i22 分钟前
CSS 迎来重大升级:Chrome 137 支持 if () 条件函数,样式逻辑从此更灵活
前端·css·面试
咚咚咚ddd24 分钟前
cursor mcp实践:网站落地页性能检测报告(browser-tools)
前端
MiyueFE25 分钟前
让我害怕的 TypeScript 类型 — — 直到我学会了这 3 条规则
前端·typescript