欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-26 09:05:17
项目需求,使用ng写一个密码框格子支付模块,一开始使用一个input+letter-spacing来分割字符,但是发现间距非常不好控制,随着字符的输入文本框字符串间距还会自...

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

效果如下:

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

完整代码如下:

<!doctype html> 
<html> 
<head lang="en"> 
 <meta charset="utf-8"> 
 <meta name="viewport" 
    content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 
 <meta name="format-detection" content="telephone=no"/> 
 <title>使用ng仿写支付宝密码框</title> 
 <style> 
  *{ margin: 0; padding: 0;} 
  .t{ margin-left: 100px;} 
  .pass-form{position:relative;top:20px; left: 50px; width:100%;} 
  .pass-form .pass-input{position:absolute;top:0;height:75px;line-height:75px;font-size:14px;color:#000;opacity:0;box-shadow:none} 
  .pass-form .pass-border-box{position:absolute;top:0;font-size:0} 
  .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} 
  .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} 
  .pass-form .pass-border-box .pass-border:first-child{border-left:solid 1px #dcdcdc} 
  .pass-form .pass-border-box .pass-border.active{background:url(../img/icons/icon_guangbiao.gif) no-repeat center center #fff} 
  .pass-form .pass-border-box .pass-border i{display:block;margin:0 auto;margin-top:22px;width:20px;height:20px;border-radius:100%} 
 </style> 
</head> 
<body ng-app="demo" ng-controller="pagectrl"> 
 
<div class="t">ng仿写支付宝密码框</div> 
 
<form class="pass-form" name="pass_form" novalidate pass-form> 
 <label for="pass"> 
  <input class="pass-input jpass" type="tel" name="pass" id="pass" autocomplete="off" ng-model="pass" required maxlength="6" /> 
 
  <div class="pass-border-box"> 
   <span class="pass-border"><i>dot</i></span> 
   <span class="pass-border"><i>dot</i></span> 
   <span class="pass-border"><i>dot</i></span> 
   <span class="pass-border"><i>dot</i></span> 
   <span class="pass-border"><i>dot</i></span> 
   <span class="pass-border"><i>dot</i></span> 
   <div class="faguang jfaguang"></div> 
  </div> 
 </label> 
</form> 
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
<script> 
 var app=angular.module('demo', []); 
 app.controller('pagectrl', function($scope, $interval, $http, $q){ 
  $scope.pass=''; 
//  $interval(function(){ 
//   console.log('定时检查:'+$scope.pass); 
//  }, 5000); 
 }) 
  .directive('passform', function($http){ 
   return { 
    restrict: 'ea', 
    link: function(scope, ele, attr){ 
     var inputdom=angular.element(ele[0].queryselector('.jpass'));//密码框 
     var spandoms=ele.find('span');//光标span 
     var faguang=angular.element(ele[0].queryselector('.jfaguang'));//发光外框 
     var that=this; 
     inputdom.on('focus blur keyup', function(e){ 
      e=e? e : window.event; 
      e.stoppropagation(); 
 
      console.log('value len:'+this.value.length); 
      console.log(e.type); 
      if(e.type==='focus'){ 
       var _currfocusinputlen=this.value.length===6? 5 : this.value.length; 
       spandoms.eq(_currfocusinputlen).addclass('active'); 
       faguang.css({left: _currfocusinputlen * 75+'px', opacity: 1}); 
      }else if(e.type==='blur'){ 
       var _currblurinputlen = this.value.length; 
       spandoms.eq(_currblurinputlen).removeclass('active'); 
       faguang.css({opacity: 0}); 
      }else if(e.type==='keyup'){ 
       //console.log(this.value); 
       //键盘上的数字键按下才可以输入 
       if(e.keycode == 8 || (e.keycode >= 48 && e.keycode <= 57) || (e.keycode >= 96 && e.keycode <= 105)){ 
        var curinputlen = this.value.length;//输入的文本内容长度 
        for (var j = 0; j < 6; j++) { 
         spandoms.eq(j).removeclass('active'); 
         spandoms.eq(curinputlen).addclass('active'); 
         spandoms.eq(curinputlen - 1).next().find('i').css({backgroundcolor: 'transparent'}); 
         spandoms.eq(curinputlen - 1).find('i').css({backgroundcolor: '#000'}); 
         faguang.css({ 
          left: curinputlen * 75 + 'px' 
         }); 
        } 
        if (curinputlen === 0) { 
         spandoms.find('i').css({backgroundcolor: 'transparent'}); 
        } 
        if (curinputlen === 6) { 
         spandoms.eq(5).addclass('active'); 
         faguang.css({ 
          left: '375px' 
         }); 
         //直接发起密码验证 
         var dosubmitcallback=function(){ 
          scope.pass=''; 
          spandoms.find('i').css({backgroundcolor: 'transparent'}); 
          spandoms.removeclass('active').eq(0).addclass('active'); 
          faguang.css({ 
           left: '0' 
          }); 
         }; 
//         $http.get('http://xxxx/test.php?pass='+this.value) 
//          .success(function(res){ 
//           console.log(res); 
//           if(res.status){ 
//            dosubmitcallback(); 
//            console.log(that.value+'-----'); 
//           }else{ 
//            dosubmitcallback(); 
 
//           } 
//          }); 
        } 
       }else{ 
        this.value = this.value.replace(/\d/g,''); 
       } 
 
      } 
     }); 
    } 
   } 
  }); 
</script> 
</body> 
</html> 

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