input_max.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * 文本输入框放大组件
  3. */
  4. function TextMagnifier(options) {
  5. this.config = {
  6. inputElem : '.inputElem', // 输入框目标元素
  7. parentCls : '.parentCls', // 目标元素的父类
  8. align : 'right', // 对齐方式有 ['top','bottom','left','right']四种 默认为top
  9. splitType : [3,4,4], // 拆分规则
  10. delimiter : '-' // 分隔符可自定义
  11. };
  12. this.cache = {
  13. isFlag : false
  14. };
  15. this.init(options);
  16. }
  17. TextMagnifier.prototype = {
  18. constructor: TextMagnifier,
  19. init: function(options) {
  20. this.config = $.extend(this.config,options || {});
  21. var self = this,
  22. _config = self.config,
  23. _cache = self.cache;
  24. self._bindEnv();
  25. },
  26. /*
  27. * 在body后动态添加HTML内容
  28. * @method _appendHTML
  29. */
  30. _appendHTML: function($this,value) {
  31. var self = this,
  32. _config = self.config,
  33. _cache = self.cache;
  34. var html = '',
  35. $parent = $($this).closest(_config.parentCls);
  36. if($('.js-max-input',$parent).length == 0) {
  37. html += '<div class="js-max-input"></div>';
  38. $($parent).append(html);
  39. }
  40. var value = self._formatStr(value);
  41. $('.js-max-input',$parent).html(value);
  42. },
  43. /*
  44. * 给目标元素定位
  45. * @method _position
  46. * @param target
  47. */
  48. _position: function(target){
  49. var self = this,
  50. _config = self.config;
  51. var elemWidth = $(target).outerWidth(),
  52. elemHeight = $(target).outerHeight(),
  53. elemParent = $(target).closest(_config.parentCls),
  54. containerHeight = $('.js-max-input',elemParent).outerHeight();
  55. $(elemParent).css({"position":'relative'});
  56. switch(true){
  57. case _config.align == 'top':
  58. $('.js-max-input',elemParent).css({'position':'absolute','top' :-elemHeight - containerHeight/2,'left':0});
  59. break;
  60. case _config.align == 'left':
  61. $('.js-max-input',elemParent).css({'position':'absolute','top' :0,'left':0});
  62. break;
  63. case _config.align == 'bottom':
  64. $('.js-max-input',elemParent).css({'position':'absolute','top' :elemHeight + 4 + 'px','left':0});
  65. break;
  66. case _config.align == 'right':
  67. $('.js-max-input',elemParent).css({'position':'absolute','top' :0,'left':elemWidth + 2 + 'px'});
  68. break;
  69. }
  70. },
  71. /**
  72. * 绑定事件
  73. * @method _bindEnv
  74. */
  75. _bindEnv: function(){
  76. var self = this,
  77. _config = self.config,
  78. _cache = self.cache;
  79. // 实时监听输入框值的变化
  80. $(_config.inputElem).each(function(index,item){
  81. $(item).keyup(function(e){
  82. var value = $.trim(e.target.value),
  83. parent = $(this).closest(_config.parentCls);
  84. if(value == '') {
  85. self._hide(parent);
  86. }else {
  87. var html = $.trim($('.js-max-input',parent).html());
  88. if(html != '') {
  89. self._show(parent);
  90. }
  91. }
  92. self._appendHTML($(this),value);
  93. self._position($(this));
  94. });
  95. $(item).unbind('focusin');
  96. $(item).bind('focusin',function(){
  97. var parent = $(this).closest(_config.parentCls),
  98. html = $.trim($('.js-max-input',parent).html());
  99. if(html != '') {
  100. self._show(parent);
  101. }
  102. });
  103. $(item).unbind('focusout');
  104. $(item).bind('focusout',function(){
  105. var parent = $(this).closest(_config.parentCls);
  106. self._hide(parent);
  107. });
  108. });
  109. },
  110. /**
  111. * 格式化下
  112. * @method _formatStr
  113. */
  114. _formatStr: function(str){
  115. var self = this,
  116. _config = self.config,
  117. _cache = self.cache;
  118. var count = 0,
  119. output = [];
  120. for(var i = 0, ilen = _config.splitType.length; i < ilen; i++){
  121. var s = str.substr(count,_config.splitType[i]);
  122. if(s.length > 0){
  123. output.push(s);
  124. }
  125. count+= _config.splitType[i];
  126. }
  127. return output.join(_config.delimiter);
  128. },
  129. /*
  130. * 显示 放大容器
  131. * @method _show
  132. */
  133. _show: function(parent) {
  134. var self = this,
  135. _config = self.config,
  136. _cache = self.cache;
  137. if(!_cache.isFlag) {
  138. $('.js-max-input',parent).show();
  139. _cache.isFlag = true;
  140. }
  141. },
  142. /*
  143. * 隐藏 放大容器
  144. * @method hide
  145. * {public}
  146. */
  147. _hide: function(parent) {
  148. var self = this,
  149. _config = self.config,
  150. _cache = self.cache;
  151. if(_cache.isFlag) {
  152. $('.js-max-input',parent).hide();
  153. _cache.isFlag = false;
  154. }
  155. }
  156. };