angular仿支付寶密碼框輸入效果
項(xiàng)目需求,使用ng寫(xiě)一個(gè)密碼框格子支付模塊,一開(kāi)始使用一個(gè)input+letter-spacing來(lái)分割字符,但是發(fā)現(xiàn)間距非常不好控制,隨著字符的輸入文本框字符串間距還會(huì)自動(dòng)調(diào)整。最終從網(wǎng)上查找到一款jq仿支付寶密碼輸入框,于是我模仿編寫(xiě)了個(gè)指令模塊。
效果如下:

完整代碼如下:
<!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仿寫(xiě)支付寶密碼框</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仿寫(xiě)支付寶密碼框</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('定時(shí)檢查:'+$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');//光標(biāo)span
var faguang=angular.element(ele[0].querySelector('.Jfaguang'));//發(fā)光外框
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);
//鍵盤(pán)上的數(shù)字鍵按下才可以輸入
if(e.keyCode == 8 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)){
var curInputLen = this.value.length;//輸入的文本內(nèi)容長(zhǎng)度
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'
});
//直接發(fā)起密碼驗(yàn)證
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼
- Angularjs修改密碼的實(shí)例代碼
- AngularJS前端頁(yè)面操作之用戶修改密碼功能示例
- 在 Angular2 中實(shí)現(xiàn)自定義校驗(yàn)指令(確認(rèn)密碼)的方法
- AngularJs驗(yàn)證重復(fù)密碼的方法(兩種)
- angularjs 表單密碼驗(yàn)證自定義指令實(shí)現(xiàn)代碼
- Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能示例
- Angular實(shí)現(xiàn)點(diǎn)擊按鈕后在上方顯示輸入內(nèi)容的方法
- AngularJS實(shí)現(xiàn)根據(jù)不同條件顯示不同控件
- AngularJS實(shí)時(shí)獲取并顯示密碼的方法
相關(guān)文章
angular4應(yīng)用中輸入的最小值和最大值的方法
這篇文章主要介紹了angular4應(yīng)用中輸入的最小值和最大值的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
測(cè)試IE瀏覽器對(duì)JavaScript的AngularJS的兼容性
這篇文章主要介紹了測(cè)試IE瀏覽器對(duì)JavaScript的AngularJS的兼容性的方法,盡管隨著Windows10的近期上市,IE瀏覽器即將成為歷史...需要的朋友可以參考下2015-06-06
AngularJS教程 ng-style 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-style 指令,這里對(duì)ng-style 指令做了詳細(xì)的基礎(chǔ)資料整理,并附有代碼示例,有需要的朋友可以參考下2016-08-08
Angular.Js中ng-include指令的使用與實(shí)現(xiàn)
ng-include 指令用于包含外部的 HTML 文件。包含的內(nèi)容將作為指定元素的子節(jié)點(diǎn)。下面這篇文章主要給大家介紹了Angular.Js中ng-include指令的使用與實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。2017-05-05
關(guān)于AngularJS中ng-repeat不更新視圖的解決方法
今天小編就為大家分享一篇關(guān)于AngularJS中ng-repeat不更新視圖的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例
這篇文章主要介紹了angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
AngularJS 雙向數(shù)據(jù)綁定詳解簡(jiǎn)單實(shí)例
這篇文章主要介紹了AngularJS 雙向數(shù)據(jù)綁定詳解簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10
angularjs請(qǐng)求數(shù)據(jù)的方法示例
這篇文章主要給大家介紹了關(guān)于angularjs請(qǐng)求數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用angularjs具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

