numberInput.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. jQuery.fn.extend({
  2. numInput: function(opt){
  3. var defaultOptions = {
  4. width: 100,
  5. wrapperClass: 'num-input-wrap',
  6. inputClass: 'num-input',
  7. addClass: 'add',
  8. subtractClass: 'subtract',
  9. positive: true,
  10. negative: true,
  11. floatCount: 2
  12. };
  13. var options = jQuery.extend(defaultOptions, opt);
  14. this._initNumDom(options);
  15. },
  16. _initNumDom: function(opt){
  17. for (var i = 0,len = this.length; i < len; i++) {
  18. var wrapper = $('<div class="'+opt.wrapperClass+'"></div>');
  19. wrapper.css({"position": "relative", "display": "inline-block", "vertical-align": "top", "height": 32, "width": opt.width, "border": "1px solid #ccc", "border-radius": 6, "box-sizing": "border-box", "overflow": "hidden"});
  20. console.log;
  21. $(this[i]).append(wrapper);
  22. var inputN = $('<input type="text" class="'+opt.inputClass+'"/>');
  23. inputN.css({"height": 30, "width": "100%", "padding": "0 25px 0 12px", "font-size": "14px", "line-height": "30px", "background": "#fff", "box-shadow": "inset 0 1px 1px rgba(0,0,0,.075)", "box-sizing": "border-box", "border": "none"});
  24. var addBtn = $('<span class="'+opt.addClass+'"></span>');
  25. addBtn.css({"position": "absolute", 'right': 0, 'top': 0, 'width': 25, "height": 15, "border-left": "1px solid #ccc", "box-sizing": "border-box", "cursor": "pointer"});
  26. var subtractBtn = $('<span class="'+opt.subtractClass+'"></span>');
  27. subtractBtn.css({"position": "absolute", 'right': 0, 'bottom': 0, 'width': 25, "height": 15, "border-left": "1px solid #ccc", "box-sizing": "border-box", "cursor": "pointer"});
  28. wrapper.append(inputN).append(addBtn).append(subtractBtn);
  29. this._initNumEvent(inputN, addBtn, subtractBtn, opt)
  30. }
  31. $('<style type="text/css">.add:hover,.subtract:hover{background: #d8d8d8;}.add.deep,.subtract.deep{background: #b3b3b3;}.add::after{position: absolute;left: 8px;top: 5px;content: "";border-left: 4px solid transparent;border-right: 4px solid transparent;border-bottom: 6px solid #333;}.subtract::after{position: absolute;left: 8px;bottom: 5px;content: "";border-left: 4px solid transparent;border-right: 4px solid transparent;border-top: 6px solid #333;}</style>').appendTo('head');
  32. },
  33. _initNumEvent: function(it, ab, sb, opt){
  34. var countDown,quickChange;
  35. ab.on('mousedown', function(){
  36. $(this).addClass('deep');
  37. var val = parseFloat($(this).prevAll('input').val());
  38. val = isNaN(val) ? 0 : val;
  39. val++;
  40. if(val > 0 && !opt.positive){
  41. val = 0;
  42. }else if(val < 0 && !opt.negative){
  43. val = 0;
  44. }
  45. val = Math.round(val*Math.pow(10, opt.floatCount))/Math.pow(10, opt.floatCount);
  46. it.val(val);
  47. countDown = setTimeout(function(){
  48. quickChange = setInterval(function(){
  49. var val = parseFloat(it.val());
  50. val++;
  51. if(val > 0 && !opt.positive){
  52. clearTimeout(countDown);
  53. clearInterval(quickChange);
  54. val = 0;
  55. }
  56. val = Math.round(val*Math.pow(10, opt.floatCount))/Math.pow(10, opt.floatCount);
  57. it.val(val);
  58. },30)
  59. },500)
  60. });
  61. ab.on('mouseup', function(){
  62. $(this).removeClass('deep');
  63. if(countDown){
  64. clearTimeout(countDown);
  65. }
  66. if(quickChange){
  67. clearInterval(quickChange);
  68. }
  69. });
  70. sb.on('mousedown', function(){
  71. $(this).addClass('deep');
  72. var val = parseFloat($(this).prevAll('input').val());
  73. val = isNaN(val) ? 0 : val;
  74. val--;
  75. if(val < 0 && !opt.negative){
  76. val = 0;
  77. }else if(val > 0 && !opt.positive){
  78. val = 0;
  79. }
  80. val = Math.round(val*Math.pow(10, opt.floatCount))/Math.pow(10, opt.floatCount);
  81. it.val(val);
  82. countDown = setTimeout(function(){//长按半秒触发快速加减
  83. quickChange = setInterval(function(){
  84. var val = parseFloat(it.val());
  85. val--;
  86. if(val < 0 && !opt.negative){
  87. clearTimeout(countDown);
  88. clearInterval(quickChange);
  89. val = 0;
  90. }
  91. val = Math.round(val*Math.pow(10, opt.floatCount))/Math.pow(10, opt.floatCount);
  92. it.val(val);
  93. },30)
  94. },500)
  95. });
  96. sb.on('mouseup', function(){
  97. $(this).removeClass('deep');
  98. if(countDown){
  99. clearTimeout(countDown);
  100. }
  101. if(quickChange){
  102. clearInterval(quickChange);
  103. }
  104. });
  105. it.on('keyup',function(){
  106. var val = $(this).val();
  107. if((opt.positive&&opt.negative)||(!opt.positive&&!opt.negative)){
  108. var reg = new RegExp('^\D*(-?(([1-9]\\d*)?|([0]))?(\\.\\d{0,'+opt.floatCount+'})?)?.*$', 'g');
  109. }else if(!opt.positive&&opt.negative){
  110. var reg = new RegExp('^(-(([1-9]\\d*)?|([0]))?(\\.\\d{0,'+opt.floatCount+'})?)?.*$', 'g');
  111. }else if(opt.positive&&!opt.negative){
  112. var reg = new RegExp('^\D*(([1-9]\d*)?|([0]))?(\.\d{0,'+opt.floatCount+'})?)?.*$', 'g');
  113. }
  114. val = val.replace(reg,'$1');
  115. if((val.indexOf('.')==-1||(val.indexOf('.')+1) < val.length)&&val.indexOf('-')==-1){
  116. val = parseFloat(val);
  117. val = isNaN(val) ? '' : val;
  118. }
  119. $(this).val(val) ;
  120. })
  121. }
  122. });