当前位置:Gxlcms > JavaScript > angular仿支付宝密码框输入效果

angular仿支付宝密码框输入效果

时间:2021-07-01 10:21:17 帮助过:8人阅读

项目需求,使用ng写一个密码框格子支付模块,一开始使用一个input+letter-spacing来分割字符,但是发现间距非常不好控制,随着字符的输入文本框字符串间距还会自动调整。最终从网上查找到一款jq仿支付宝密码输入框,于是我模仿编写了个指令模块。

效果如下:

完整代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta name="format-detection" content="telephone=no"/>
  8. <title>使用ng仿写支付宝密码框</title>
  9. <style>
  10. *{ margin: 0; padding: 0;}
  11. .t{ margin-left: 100px;}
  12. .pass-form{position:relative;top:20px; left: 50px; width:100%;}
  13. .pass-form .pass-input{position:absolute;top:0;height:75px;line-height:75px;font-size:14px;color:#000;opacity:0;box-shadow:none}
  14. .pass-form .pass-border-box{position:absolute;top:0;font-size:0}
  15. .pass-form .pass-border-box .faguang{position:absolute;top:0;left:0;z-index:9;box-shadow:0 0 8px rgba(60,100,100,.6);width:75px;height:75px;background:#fff;opacity:0}
  16. .pass-form .pass-border-box .pass-border{display:inline-block;position:relative;z-index:10;width:75px;height:75px;border:solid 1px #dcdcdc;border-left:none;-webkit-box-sizing:border-box;box-sizing:border-box}
  17. .pass-form .pass-border-box .pass-border:first-child{border-left:solid 1px #dcdcdc}
  18. .pass-form .pass-border-box .pass-border.active{background:url(../img/icons/icon_guangbiao.gif) no-repeat center center #fff}
  19. .pass-form .pass-border-box .pass-border i{display:block;margin:0 auto;margin-top:22px;width:20px;height:20px;border-radius:100%}
  20. </style>
  21. </head>
  22. <body ng-app="demo" ng-controller="pageCtrl">
  23. <div class="t">ng仿写支付宝密码框</div>
  24. <form class="pass-form" name="pass_form" novalidate pass-form>
  25. <label for="pass">
  26. <input class="pass-input Jpass" type="tel" name="pass" id="pass" autocomplete="off" ng-model="pass" required maxlength="6" />
  27. <div class="pass-border-box">
  28. <span class="pass-border"><i>dot</i></span>
  29. <span class="pass-border"><i>dot</i></span>
  30. <span class="pass-border"><i>dot</i></span>
  31. <span class="pass-border"><i>dot</i></span>
  32. <span class="pass-border"><i>dot</i></span>
  33. <span class="pass-border"><i>dot</i></span>
  34. <div class="faguang Jfaguang"></div>
  35. </div>
  36. </label>
  37. </form>
  38. <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
  39. <script>
  40. var app=angular.module('demo', []);
  41. app.controller('pageCtrl', function($scope, $interval, $http, $q){
  42. $scope.pass='';
  43. // $interval(function(){
  44. // console.log('定时检查:'+$scope.pass);
  45. // }, 5000);
  46. })
  47. .directive('passForm', function($http){
  48. return {
  49. restrict: 'EA',
  50. link: function(scope, ele, attr){
  51. var inputDom=angular.element(ele[0].querySelector('.Jpass'));//密码框
  52. var spanDoms=ele.find('span');//光标span
  53. var faguang=angular.element(ele[0].querySelector('.Jfaguang'));//发光外框
  54. var that=this;
  55. inputDom.on('focus blur keyup', function(e){
  56. e=e? e : window.event;
  57. e.stopPropagation();
  58. console.log('value len:'+this.value.length);
  59. console.log(e.type);
  60. if(e.type==='focus'){
  61. var _currFocusInputLen=this.value.length===6? 5 : this.value.length;
  62. spanDoms.eq(_currFocusInputLen).addClass('active');
  63. faguang.css({left: _currFocusInputLen * 75+'px', opacity: 1});
  64. }else if(e.type==='blur'){
  65. var _currBlurInputLen = this.value.length;
  66. spanDoms.eq(_currBlurInputLen).removeClass('active');
  67. faguang.css({opacity: 0});
  68. }else if(e.type==='keyup'){
  69. //console.log(this.value);
  70. //键盘上的数字键按下才可以输入
  71. if(e.keyCode == 8 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)){
  72. var curInputLen = this.value.length;//输入的文本内容长度
  73. for (var j = 0; j < 6; j++) {
  74. spanDoms.eq(j).removeClass('active');
  75. spanDoms.eq(curInputLen).addClass('active');
  76. spanDoms.eq(curInputLen - 1).next().find('i').css({backgroundColor: 'transparent'});
  77. spanDoms.eq(curInputLen - 1).find('i').css({backgroundColor: '#000'});
  78. faguang.css({
  79. left: curInputLen * 75 + 'px'
  80. });
  81. }
  82. if (curInputLen === 0) {
  83. spanDoms.find('i').css({backgroundColor: 'transparent'});
  84. }
  85. if (curInputLen === 6) {
  86. spanDoms.eq(5).addClass('active');
  87. faguang.css({
  88. left: '375px'
  89. });
  90. //直接发起密码验证
  91. var doSubmitCallback=function(){
  92. scope.pass='';
  93. spanDoms.find('i').css({backgroundColor: 'transparent'});
  94. spanDoms.removeClass('active').eq(0).addClass('active');
  95. faguang.css({
  96. left: '0'
  97. });
  98. };
  99. // $http.get('http://xxxx/test.php?pass='+this.value)
  100. // .success(function(res){
  101. // console.log(res);
  102. // if(res.status){
  103. // doSubmitCallback();
  104. // console.log(that.value+'-----');
  105. // }else{
  106. // doSubmitCallback();
  107. // }
  108. // });
  109. }
  110. }else{
  111. this.value = this.value.replace(/\D/g,'');
  112. }
  113. }
  114. });
  115. }
  116. }
  117. });
  118. </script>
  119. </body>
  120. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行