前端小案例——输入框提示词(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函数,以更新占位符文本。

二、项目运行效果 

三、全部代码 

<!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的小案例,各位小伙伴可以自行更改样式和内容,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主。

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

博主VX:18884281851

谢谢各位的支持~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花花´◡`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值