jquery实现表单输入时提示文字滑动向上效果

本文实例讲述了jquery实现表单输入时提示文字滑动向上效果。分享给大家供大家参考。具体如下:

这里基于jQuery实现的表单输入框提示效果,当不输入的时候,提示文字就显示在输入框中,当鼠标点击文本框要输入文字的时候,提示文字向滑出输入框,好像很个性也很智能的样子,用户体验比较不错,运用了CSS3的部分属性,因此在测试时,请尽量要用高版本的IE9或chrome和火狐等网页浏览器。

运行效果截图如下:

具体代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery人性化表单标签提示</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
!function($){
 var defaults = {
 position: "top",
 animationTime: 500,
 easing: "ease-in-out",
 offset: 20,
 hidePlaceholderOnFocus: true
  };
 $.fn.animateLabel = function(settings, btn) {
 var position = btn.data("position") || settings.position,
 posx = 0,
 posy = 0;
 $(this).css({
  "left": "auto",
  "right": "auto",
  "position": "absolute",
  "-webkit-transition": "all " + settings.animationTime + "ms " + settings.easing,
  "-moz-transition": "all " + settings.animationTime + "ms " + settings.easing,
  "-ms-transition": "all " + settings.animationTime + "ms " + settings.easing,
  "transition": "all " + settings.animationTime + "ms " + settings.easing
 });
 switch (position) {
  case 'top':
  posx = 0;
  posy = ($(this).height() + settings.offset) * -1;
  $(this).css({
   "top": "0",
   "opacity": "1",
   "-webkit-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
   "-moz-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
   "-ms-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
   "transform": "translate3d(" + posx + ", " + posy + "px, 0)"
  });
  break;
  case 'bottom':
  posx = 0;
  posy = ($(this).height() + settings.offset);
  $(this).css({
   "bottom": "0",
   "opacity": "1",
   "-webkit-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
   "-moz-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
   "-ms-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
   "transform": "translate3d(" + posx + ", " + posy + "px, 0)"
  });
  break;
  case 'left':
  posx = ($(this).width() + settings.offset) * -1;
  posy = 0;
  $(this).css({
   "left": 0,
   "top": 0,
   "opacity": "1",
   "-webkit-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
   "-moz-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
   "-ms-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
   "transform": "translate3d(" + posx + "px, " + posy + "px, 0)"
  });
  break;
  case 'right':
  posx = $(this).width() + settings.offset;
  posy = 0;
  $(this).css({
   "right": 0,
   "top": 0,
   "opacity": "1",
   "-webkit-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
   "-moz-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
   "-ms-transform": "translate3d(" + posx + "px, " + posy + "px, 0)",
   "transform": "translate3d(" + posx + "px, " + posy + "px, 0)"
  });
  break;
 }
 }
 $.fn.removeAnimate = function(settings, btn) {
 var position = btn.data("position") || settings.position,
 posx = 0,
 posy = 0;
 $(this).css({
  "top": "0",
  "opacity": "0",
  "-webkit-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
  "-moz-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
  "-ms-transform": "translate3d(" + posx + ", " + posy + "px, 0)",
  "transform": "translate3d(" + posx + ", " + posy + "px, 0)"
 });
 }
 $.fn.label_better = function(options){
 var settings = $.extend({}, defaults, options),
  el = $(this),
  triggerIn = "focus",
  triggerOut = "blur";
 if(settings.easing == "bounce") settings.easing = "cubic-bezier(0.175, 0.885, 0.420, 1.310)"
 el.each(function( index, value ) {
  var btn = $(this),
   position = btn.data("position") || settings.position;
  btn.wrapAll("<div class='lb_wrap' style='position:relative; display: inline;'></div>")
  if( btn.val().length > 0) {
  var text = btn.data("new-placeholder") || btn.attr("placeholder");
  $("<div class='lb_label " + position + "'>"+ text + "</div>").css("opacity", "0").insertAfter(btn).animateLabel(settings, btn);
  }
  btn.bind(triggerIn, function() {
  if(btn.val().length < 1) {
   var text = btn.data("new-placeholder") || btn.attr("placeholder"),
   position = btn.data("position") || settings.position;
   $("<div class='lb_label " + position + "'>"+ text + "</div>").css("opacity", "0").insertAfter(btn).animateLabel(settings, btn);
  }
  if (settings.hidePlaceholderOnFocus == true) {
   btn.data("default-placeholder", btn.attr("placeholder"))
   btn.attr("placeholder", "")
  }
  btn.parent().find(".lb_label").addClass("active");
  }).bind(triggerOut, function() {
  if(btn.val().length < 1) {
   btn.parent().find(".lb_label").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ $(this).remove(); }).removeAnimate(settings, btn)
  }
  if (settings.hidePlaceholderOnFocus == true) {
   btn.attr("placeholder", btn.data("default-placeholder"))
   btn.data("default-placeholder", "")
  }
  btn.parent().find(".lb_label").removeClass("active");
  });
 });
 }
}(window.jQuery);
</script>
<style>
html {
 height: 100%;
}
body {
 background: #272D30;
 padding: 0;
 text-align: center;
 font-family: 'open sans';
 position: relative;
 margin: 0;
 height: 100%;
}
.wrapper {
  height: auto !important;
  height: 100%;
  margin: 0 auto;
  overflow: hidden;
}
a {
 text-decoration: none;
}
h1, h2 {
 width: 100%;
 float: left;
}
h1 {
 margin-top: 100px;
 color: #fff;
 text-shadow: 0 1px 5px rgba(0,0,0,0.5);
 margin-bottom: 5px;
 font-size: 70px;
 letter-spacing: -4px;
}
h2 {
 color: #5F7591;
 font-weight: bold;
 text-shadow: 0 1px 5px rgba(0,0,0,0.5);
 margin-top: 0;
 margin-bottom: 10px;
}
.pointer {
 color: #9b59b6;
 font-family: 'Pacifico', cursive;
 font-size: 30px;
 margin-top: 15px;
}
pre {
 margin: 80px auto;
}
pre code {
 padding: 35px;
 border-radius: 5px;
 font-size: 15px;
 background: rgba(0,0,0,0.1);
 border: rgba(0,0,0,0.05) 5px solid;
 max-width: 500px;
}
.main {
 float: left;
 width: 100%;
 margin: 0 auto;
}
.main h1 {
 padding:20px 50px;
 float: left;
 width: 100%;
 font-size: 60px;
 box-sizing: border-box;
 -webkit-box-sizing: border-box;
 -moz-box-sizing: border-box;
 font-weight: 100;
 margin: 0;
 padding-top: 25px;
 font-family: 'Pacifico';
 letter-spacing: 2px;
}
.main h1.demo1 {
 background: #1ABC9C;
}
.reload.bell {
 font-size: 12px;
 padding: 20px;
 width: 45px;
 text-align: center;
 height: 47px;
 border-radius: 50px;
 -webkit-border-radius: 50px;
 -moz-border-radius: 50px;
}
.reload.bell #notification {
 font-size: 25px;
 line-height: 140%;
}
.reload, .btn{
 display: inline-block;
 border: 4px solid #A2261E;
 border-radius: 5px;
 -moz-border-radius: 5px;
 -webkit-border-radius: 5px;
 background: #CC3126;
 display: inline-block;
 line-height: 100%;
 padding: 0.7em;
 text-decoration: none;
 color: #fff;
 width: 100px;
 line-height: 140%;
 font-size: 17px;
 font-family: open sans;
 font-weight: bold;
}
.reload:hover{
 background: #A2261E;
}
.btn {
 width: 200px;
 color: #fff;
 border: none;
 margin-left: 10px;
 background: rgba(255, 255, 255, 0.11);
}
.clear {
 width: auto;
}
.btn:hover, .btn:hover {
 background: rgba(255,255,255,0.3);
}
.btns {
 width: 410px;
 margin: 50px auto;
}
.credit {
 font-style: italic;
 text-align: center;
 color: #fff;
 padding: 10px;
 margin: 0 0 40px 0;
 float: left;
 width: 100%;
}
.credit a {
 color: #ccc;
 text-decoration: none;
 font-weight: bold;
}
.back {
 position: absolute;
 top: 0;
 left: 0;
 text-align: center;
 display: block;
 padding: 7px;
 width: 100%;
 box-sizing: border-box;
 -moz-box-sizing: border-box;
 -webkit-box-sizing: border-box;
 background: rgba(0, 0, 0, 0.65);
 font-weight: bold;
 font-size: 13px;
 color: #fff;
 -webkit-transition: all 200ms ease-out;
 -moz-transition: all 200ms ease-out;
 -o-transition: all 200ms ease-out;
 transition: all 200ms ease-out;
}
.back:hover {
 background: rgba(0, 0, 0, 0.85);
}
.bl_form {
 margin: 150px 0;
}
.bl_form input {
 padding-top: 15px;
 background: rgba(255,255,255,0.10);
 box-shadow: 0 2px 8px rgba(0,0,0,0.2);
 border: none;
 color: white;
 padding: 10px 15px;
 border-radius: 25px;
 font-size: 16px;
 outline: none;
}
.lb_wrap .lb_label.top, .lb_wrap .lb_label.bottom {
 left: 15px !important;
}
.lb_wrap .lb_label.left {
 left: 0;
}
.lb_label {
 font-weight: bold;
 color: #999;
}
.lb_label.active {
 color: #FFF;
}
</style>
<script>
 $(document).ready( function() {
 $(".label_better").label_better({
  easing: "bounce"
 });
 });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
 <div class="wrapper">
   <div class="main">
   <div class="header">
  <h1>jQuery Label Better</h1>
  <h2>Label your form input like a boss</h2>
  <p class="credit">Created by Pete R., Founder of BucketListly</p>
  <div class="btns">
    </div>
   </div>
  <div class="page-container">
  <form class="bl_form">
   <input type="text" class="label_better" data-new-placeholder="Username" placeholder="Username" >
   <input type="email" class="label_better" data-new-placeholder="Email Address" placeholder="Email Address">
   <input type="password" value="abcdefg" class="label_better" data-new-placeholder="Password" placeholder="Password">
   <input type="password" value="abcdefg" class="label_better" data-new-placeholder="Shhh.." placeholder="Confirm Password">
  </form>
  </div>
 </div>
 </div>
<div style="text-align:center;clear:both">
</div>
</body>
</html>

希望本文所述对大家的jquery程序设计有所帮助。

(0)

相关推荐

  • 基于jquery的气泡提示效果

    代码注释已经尽可能的详细了,也不多说了. 初步测试暂未发现大的BUG,总体来说不满意的是鼠标移来移去不断触发气泡时会出现XX为空或不是对象的问题, 虽然不影响效果,但看着IE左下角的黄色警告不爽,暂时不知道如何改进. 加了try/catch解决... 还有就是气泡默认出现在触发对象的正上方,当触发对象在边上时,气泡会有一部分出现在窗口外面......也许这种情况可以让气泡显示在左边或是右边,感觉可能会有些麻烦,就没去弄了,比较懒...... 越用jquery就越喜欢用它... bubble.js

  • 基于jQuery的input输入框下拉提示层(自动邮箱后缀名)

    效果图 代码部分 复制代码 代码如下: // JavaScript Document (function($){ $.fn.extend({ "changeTips":function(value){ value = $.extend({ divTip:"" },value) var $this = $(this); var indexLi = 0; //点击document隐藏下拉层 $(document).click(function(event){ if($(

  • jquery限制输入字数,并提示剩余字数实现代码

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

  • Jquery实现搜索框提示功能示例代码

    博客的前某一篇文章中http://www.jb51.net/article/35175.htm写过一个用Ajax来实现一个文本框输入的提示功能.最近在一个管理项目的项目中,使用后发现,真的反应很慢,数据量很大的情况下使用Ajax去实现真的不合适,于是,我又写了一个使用Jquery来实现方法. 废话不多说,上图上代码:  引用方式: 复制代码 代码如下: <body style="background-color: White;"> <form id="for

  • jQuery表单获取和失去焦点输入框提示效果的实例代码

    复制代码 代码如下: $("#focus .input_txt").each(function(){  var thisVal=$(this).val();  //判断文本框的值是否为空,有值的情况就隐藏提示语,没有值就显示  if(thisVal!=""){  $(this).siblings("span").hide();  }else{  $(this).siblings("span").show();  }  //聚焦

  • jQuery扩展实现text提示还能输入多少字节的方法

    本文实例讲述了jQuery扩展实现text提示还能输入多少字节的方法.分享给大家供大家参考,具体如下: 1.添加jQuery自定义扩展 $(function($){ // tipWrap: 提示消息的容器 // maxNumber: 最大输入字符 $.fn.artTxtCount = function(tipWrap, maxNumber){ var countClass = 'js_txtCount', // 定义内部容器的CSS类名 fullClass = 'js_txtFull', //

  • jquery 实现输入邮箱时自动补全下拉提示功能

    记得去年做某个项目的时候,用到了邮箱输入自动提示功能,于是网上搜了一下,发现了这个写得不错,现在回想起来,转载一下,方便查阅. 邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作为账号名 为了提高用户的体验,很多网站都会实现邮箱输入的自动提示功能. 实现效果如图所示: 核心代码(需要jquery的支持): (function($){ $.fn.mailAutoComplete = function(options){ var defaults = { boxClass: "

  • jQuery插件EnPlaceholder实现输入框提示文字

    用法: 首先在head中分别引入jQuery及本插件 <script type="text/javascript" src="jquery-1.7.2.min.js">script> <script type="text/javascript" src="jquery.enplaceholder.js">script> 然后就可以调用鸟 //通过value模拟placeholder $('i

  • jQuery实现手机号码输入提示功能实例

    本文实例讲述了jQuery实现手机号码输入提示功能的方法.分享给大家供大家参考.具体实现方法如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&g

  • ASP.NET jQuery 实例1(在TextBox里面创建一个默认提示)

    当文本框获得焦点,如果文本框内容跟提示内容一样,提示内容会自然消失. 当文本框没有任何值并失去焦点,文本框内容会重新生成默认提示. 为了实现上面的需求,代码如下: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Recipe1.aspx.cs" Inherits="Recipe1" %> <!DOCTYPE html PUB

  • jquery+ajax+text文本框实现智能提示完整实例

    本文实例讲述了jquery+ajax+text文本框实现智能提示的方法.分享给大家供大家参考,具体如下: 模仿百度查询的智能提示 先看看效果图: 代码部分: CSS代码: <style type="text/css"> #searchresult { width: 130px; position: absolute; z-index: 1; overflow: hidden; left: 130px; top: 71px; background: #E0E0E0; bord

  • 基于jQuery的弹出警告对话框美化插件(警告,确认和提示)

    前不久在官方网站是看见这个插件,所以今天趁有空就看了一下,随便给大家共享一下.也许你早已知道了 ,如果是这样那请跳过,不要拍砖. 这个Jquery插件的目的是替代JavaScript的标准函数alert(),confirm(),和 prompt().这个插件有 如下这些特点: 1:这个插件可以使你可以支持你自己的css制定.使你的网站看起来更专业. 2:允许你自定义对话框的标题. 3:在IE7中,可以使你避免使用JavaScript 的prompt()函数带来的页面重新加载. 4:这些方法都模拟

随机推荐