JS 仿支付宝input文本输入框放大组件的实例


Posted in Javascript onNovember 14, 2017

input输入的时候可以在后边显示数字放大镜

<!doctype html>
<html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>JS 仿支付宝input文本输入框放大组件</title>
 <script src="js/jquery.min.js"></script>
 <style>
  * { margin: 0; padding: 0; border-width: 1px; }
  .parentCls {margin:5px 60px 0;}
  .js-max-input {border: solid 1px #ffd2b2; position:relative;background: #fffae5;padding: 0 10px 0 10px;font-size:20px;color: #ff4400}
  .inputElem4{ width: 300px; height: 36px; border: 1px solid #E0E0E0; padding-left: 10px; line-height: 36px; font-size: 14px; }
 </style>
 </head>
 <body>
 <div class="parentCls">
  <input type="text" class="inputElem4" autocomplete = "off" maxlength="18"/>
 </div>
  <script src="js/jq22.js"></script>
  <script>
   // 初始化
   $(function(){
   new TextMagnifier({
    inputElem: '.inputElem4',
    align: 'bottom',
    splitType: [6,4,4,4]
   });
   });
  </script>
 </body>
</html>
/**
 * JS 仿支付宝的文本输入框放大组件
 */ 


 function TextMagnifier(options) {

  this.config = {
  
  inputElem   :  '.inputElem',  // 输入框目标元素
  parentCls   :  '.parentCls',  // 目标元素的父类
  align    :  'right',   // 对齐方式有 ['top','bottom','left','right']四种 默认为top
  splitType   :  [3,4,4],   // 拆分规则
  delimiter   :  '-'    // 分隔符可自定义
  };

  this.cache = {
   isFlag : false
  };
  this.init(options);
 }

 TextMagnifier.prototype = {
  
  constructor: TextMagnifier,

  init: function(options) {
  this.config = $.extend(this.config,options || {});
  var self = this,
   _config = self.config,
   _cache = self.cache;
  
  self._bindEnv();
  
  
  },
  /*
  * 在body后动态添加HTML内容
  * @method _appendHTML
  */
  _appendHTML: function($this,value) {
   var self = this,
    _config = self.config,
    _cache = self.cache;

   var html = '',
    $parent = $($this).closest(_config.parentCls);

    if($('.js-max-input',$parent).length == 0) {
    html += '<div class="js-max-input"></div>';
    $($parent).append(html);
    }
    var value = self._formatStr(value);
    $('.js-max-input',$parent).html(value);
  },
  /*
  * 给目标元素定位
  * @method _position
  * @param target
  */
  _position: function(target){
  var self = this,
   _config = self.config;
  var elemWidth = $(target).outerWidth(),
   elemHeight = $(target).outerHeight(),
   elemParent = $(target).closest(_config.parentCls),
   containerHeight = $('.js-max-input',elemParent).outerHeight(); 
  
  $(elemParent).css({"position":'relative'});
  
  switch(true){
   
   case _config.align == 'top':
    
    $('.js-max-input',elemParent).css({'position':'absolute','top' :-elemHeight - containerHeight/2,'left':0});
    break;
   
   case _config.align == 'left':

    $('.js-max-input',elemParent).css({'position':'absolute','top' :0,'left':0});
    break;
   
   case _config.align == 'bottom':

    $('.js-max-input',elemParent).css({'position':'absolute','top' :elemHeight + 4 + 'px','left':0});
    break;
   
   case _config.align == 'right':

    $('.js-max-input',elemParent).css({'position':'absolute','top' :0,'left':elemWidth + 2 + 'px'});
    break;
  }
  },
  /**
  * 绑定事件
  * @method _bindEnv
  */
  _bindEnv: function(){
  var self = this,
   _config = self.config,
   _cache = self.cache;

  // 实时监听输入框值的变化
  $(_config.inputElem).each(function(index,item){

   $(item).keyup(function(e){
    var value = $.trim(e.target.value),
     parent = $(this).closest(_config.parentCls);
    if(value == '') {
     self._hide(parent);
    }else {

     var html = $.trim($('.js-max-input',parent).html());

     if(html != '') {
      self._show(parent);
     }
    }
    self._appendHTML($(this),value);
    self._position($(this));
   });

   $(item).unbind('focusin');
   $(item).bind('focusin',function(){
    var parent = $(this).closest(_config.parentCls),
     html = $.trim($('.js-max-input',parent).html());

    if(html != '') {
     self._show(parent);
    }
   });

   $(item).unbind('focusout');
   $(item).bind('focusout',function(){
    var parent = $(this).closest(_config.parentCls);
    self._hide(parent);
   });
  });
  },
  /**
  * 格式化下
  * @method _formatStr
  */
  _formatStr: function(str){
  var self = this,
   _config = self.config,
   _cache = self.cache;
  var count = 0,
   output = [];
  for(var i = 0, ilen = _config.splitType.length; i < ilen; i++){
   var s = str.substr(count,_config.splitType[i]);
   if(s.length > 0){
    output.push(s);
   }
   count+= _config.splitType[i];
  }
  return output.join(_config.delimiter);
  },
  /*
  * 显示 放大容器
  * @method _show
  */
  _show: function(parent) {
  var self = this,
   _config = self.config,
   _cache = self.cache;
  if(!_cache.isFlag) {
   $('.js-max-input',parent).show();
   _cache.isFlag = true;
  }
  },
  /*
  * 隐藏 放大容器
  * @method hide
  * {public}
  */
  _hide: function(parent) {
  var self = this,
   _config = self.config,
   _cache = self.cache;
  if(_cache.isFlag) {
   $('.js-max-input',parent).hide();
   _cache.isFlag = false;
  }
  }
 };

效果图

JS 仿支付宝input文本输入框放大组件的实例

以上这篇JS 仿支付宝input文本输入框放大组件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript中的float运算精度实例分析
Aug 21 Javascript
myeclipse安装jQuery插件的方法
Mar 29 Javascript
Javascript 倒计时源代码.(时.分.秒) 详细注释版
May 09 Javascript
Jquery原生态实现表格header头随滚动条滚动而滚动
Mar 18 Javascript
22点关于jquery性能优化的建议
May 28 Javascript
JavaScript运动减速效果实例分析
Aug 04 Javascript
JQuery中Ajax()的data参数类型实例分析
Dec 15 Javascript
jQuery dataTables与jQuery UI 对话框dialog的使用教程
Sep 02 Javascript
Node.js 实现简单小说爬虫实例
Nov 18 Javascript
详解Vue中添加过渡效果
Mar 20 Javascript
Angular6笔记之封装http的示例代码
Jul 27 Javascript
JS+HTML实现自定义上传图片按钮并显示图片功能的方法分析
Feb 12 Javascript
js实现数组内数据的上移和下移的实例
Nov 14 #Javascript
vue router使用query和params传参的使用和区别
Nov 13 #Javascript
vue+vue-validator 表单验证功能的实现代码
Nov 13 #Javascript
jQuery除指定区域外点击任何地方隐藏DIV功能
Nov 13 #jQuery
Angular实现表单验证功能
Nov 13 #Javascript
mescroll.js上拉加载下拉刷新组件使用详解
Nov 13 #Javascript
Vue.js用法详解
Nov 13 #Javascript
You might like
十天学会php之第四天
2006/10/09 PHP
如何在symfony中导出为CSV文件中的数据
2011/10/06 PHP
一些php项目中比较通用的php自建函数的详解
2013/06/06 PHP
ThinkPHP多表联合查询的常用方法
2020/03/24 PHP
PHP防止表单重复提交的几种常用方法汇总
2014/08/19 PHP
PHP实现图片旋转效果实例代码
2014/10/01 PHP
php实现的简易扫雷游戏实例
2015/07/09 PHP
PHP实现文件上传功能实例代码
2017/05/18 PHP
PHP基于mcript扩展实现对称加密功能示例
2019/02/21 PHP
jquery的map与get方法详解
2013/11/04 Javascript
jquery.form.js用法之清空form的方法
2014/03/07 Javascript
JQuery表格拖动调整列宽效果(自己动手写的)
2014/09/01 Javascript
js兼容火狐获取图片宽和高的方法
2015/05/21 Javascript
javascript实现很浪漫的气泡冒出特效
2020/09/05 Javascript
JQuery ztree 异步加载实例讲解
2016/02/25 Javascript
Javascript将JSON日期格式化
2016/08/23 Javascript
anime.js 实现带有描边动画效果的复选框(推荐)
2017/12/24 Javascript
jQuery图片查看插件Magnify开发详解
2017/12/25 jQuery
深入理解Node module模块
2018/03/26 Javascript
vue-cli 3.x 修改dist路径的方法
2018/09/19 Javascript
微信小程序学习笔记之获取位置信息操作图文详解
2019/03/29 Javascript
jquery实现的分页显示功能示例
2019/08/23 jQuery
Vue3.x源码调试的实现方法
2019/10/13 Javascript
vue实现导航标题栏随页面滚动渐隐渐显效果
2020/03/12 Javascript
design vue 表格开启列排序的操作
2020/10/28 Javascript
深入理解Python中装饰器的用法
2016/06/28 Python
python网络爬虫 Scrapy中selenium用法详解
2019/09/28 Python
Python列表元素常见操作简单示例
2019/10/25 Python
python操作gitlab API过程解析
2019/12/27 Python
如何在windows下安装配置python工具Ulipad
2020/10/27 Python
Html5与App的通讯方式详解
2019/10/24 HTML / CSS
美国工业用品采购网站:Zoro.com
2020/10/27 全球购物
计算机应用毕业生自荐信
2013/10/23 职场文书
市场总监岗位职责
2015/02/11 职场文书
2015年汽车销售经理工作总结
2015/04/27 职场文书
技术转让协议书
2016/03/19 职场文书