现在的位置: 首页 > 综合 > 正文

输入框提示特效

2013年08月25日 ⁄ 综合 ⁄ 共 1460字 ⁄ 字号 评论关闭

输入框提示在很多提示都要用到,比如搜索提示用户输入关键字,比如登录提示用户输入用户名和密码等等。比较常见的提示方式是通过onFocus和onBlur事件来监听Input的值来给出相应的提示,例:

  1. <input onfocus="if(this.value=='设计蜂巢')this.value = ''" onblur="if(this.value=='') this.value = '设计蜂巢'" type="text" />

我们再来看看twitter是怎么会理这个问题的!
首先来看三张图:
(1)点击前

(2)点击后

(3)键盘按下后

从上面的三张图片可以看出 当input获得焦点后提示文字成半透明状态,只要当输入文字以后提示语才渐渐消失。这种输入提示特效是不是很酷!下面来看看这个特效是怎么实现的!
看twitter的源代码
点击前和点击后:

  1. <div class="holding name">
  2. <input type="text" name="user[name]" value="" maxlength="20" />
  3. <span class="holder">Full name</span>
  4. </div>

输入文字后:

  1. <div class="holding name hasome">
  2. <input type="text" name="user[name]" value="" maxlength="20" />
  3. <span class="holder">Full name</span>
  4. </div>

看完上面的代码是不是已经明白实现的原理了呢? 没错就是通过javascript和CSS来实现的, 首先用CSS将span.holder定位到input上面来,当鼠标点击(focus)input的时候span.holder的透明度()设为0.5 当输入文字(keydown)的时候span.holder透明度()设为0,input失焦(blur)的时候判断input的值是否为空。

  1. .holding{position:relative; z-index:0;}
  2. .holding .holder {
  3. line-height: 28px;
  4. position: absolute;
  5. top: 0;
  6. z-index: 1;
  7. left: 8px;
  8. white-space: nowrap;
  9. cursor: text;
  10. color: #999;
  11. -webkit-transition: opacity .1s,font-size .1s;
  12. -moz-transition: opacity .1s,font-size .1s;
  13. -o-transition: opacity .1s,font-size .1s;
  14. z-index:1;
  15. }
  16. .hasome input {
  17. color:#333;
  18. }
  19. .hasome .holder {
  20. opacity:0;
  21. filter:alpha(opacity=0);
  22. font-size:0!important;
  23. }
  1. $(document).ready(function() {
  2. $(".holding input").keydown(function() {
  3. $(this).parent().addClass("hasome")
  4. }),
  5. $(".holding input").blur(function() {
  6. $.trim(a(this).val()) == "" && $(this).parent().removeClass("hasome")
  7. }),
  8. $(".holding .holder").click(function() {
  9. $(this).prev().focus()
  10. })
  11. })

抱歉!评论已关闭.