JS实现placeholder效果

<input type="text" class="login-input-1" id="usernamePlaceholder" value="用户名" οnfοcus="onfocusPlaceholderInput(this, 'username')" style="color: gray;"/>
<input name="username" type="text" class="login-input-1" id="username" οnblur="onblurRealValueInput(this, 'usernamePlaceholder')" style="display: none;"/>
<input type="text" class="login-input-1" id="passwordPlaceholder" value="密码" οnfοcus="onfocusPlaceholderInput(this, 'password')" style="color: gray; "/>
<input name="password" type="password" class="login-input-1" id="password" οnblur="onblurRealValueInput(this, 'passwordPlaceholder')" style="display: none;"/>

/**
* 点击placeholder框之后,显示真正的输入框.例如点击密码框的placeholder之后显示密码输入框.
* placeholderElement 被点击的元素
* displayElementIdStr 准备显示的元素的id字符串
*/
function onfocusPlaceholderInput(placeholderElement, displayElementIdStr) {
placeholderElement.style.display = "none";
var v_displayElement = document.getElementById(displayElementIdStr);
v_displayElement.style.display = "block";
v_displayElement.value = "";
v_displayElement.focus();
}

/**
*
* 真正有效值的input框,失去焦点时判断是否显示placeholder
* 当密码框失去焦点时,进行判断如果密码为空则显示密码placeholder框,否则显示密码框。
* realValueElement 失去焦点的元素
* placeholderIdStr 准备显示的placeholder的id字符串
*/
function onblurRealValueInput(realValueElement, placeholderIdStr) {
if ("" == realValueElement.value) {
realValueElement.style.display = "none";
var v_placeholderElement = document.getElementById(placeholderIdStr);
v_placeholderElement.style.display = "block";
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现搜索栏placeholder滚动播放效果,可以使用CSS3动画和JavaScript实现。 首先,使用CSS3动画来实现placeholder的滚动效果,可以使用以下代码: ```css .search-input { animation: placeholder 10s linear infinite; } @keyframes placeholder { 0% { opacity: 1; } 20% { opacity: 0; } 40% { content: "Search for..."; opacity: 1; } 60% { content: "Find what you need..."; opacity: 1; } 80% { content: "Explore your interests..."; opacity: 1; } 100% { content: "Try me now..."; opacity: 1; } } ``` 上述代码中,使用了`animation`属性来定义动画效果,`placeholder`是动画名称,`10s`表示动画持续时间为10秒,`linear`表示动画速度为匀速,`infinite`表示动画无限循环。 接下来,使用JavaScript实现placeholder的动态替换,可以使用以下代码: ```javascript const placeholder = document.querySelector('.search-input').getAttribute('placeholder'); const phrases = ['Search for...', 'Find what you need...', 'Explore your interests...', 'Try me now...']; let i = 0; setInterval(() => { document.querySelector('.search-input').setAttribute('placeholder', phrases[i]); i = (i + 1) % phrases.length; }, 10000); ``` 上述代码中,使用`setInterval`函数来定时替换placeholder内容,`10000`表示每隔10秒替换一次。在替换时,将`phrases`数组中的内容依次赋值给placeholder,使用取余的方式实现数组循环。 以上就是实现搜索栏placeholder滚动播放效果的方法,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值