登录  /  注册
首页 > web前端 > js教程 > 正文

使用jquery实现类似于移动端支付宝的支付键盘(代码)

不言
发布: 2018-08-15 10:18:01
原创
1921人浏览过

本篇文章给大家带来的内容是关于使用jquery实现类似于移动端支付宝的支付键盘,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

最近做项目时碰到一个需求,就是在移动端支付页面点击支付按钮弹出一个支付键盘,类似于支付宝的那种。由于项目只是单纯的手机网站,而并非app,所以这个功能得由前端来实现。话不多说,先上图看看效果。

尼玛,这不就是支付宝app那个支付键盘吗? 没错,咱们UI就是参照支付宝做的这个键盘。你可能会问,为什么不直接调用支付宝提供的支付接口呢。额,因为项目需要,这里就不多解释了。

我们先看一下实现后的效果图

           

HTML部分

<!-- 支付键盘 -->
    <divclass="pay-container">
        <divclass="pay-title">
            <spanclass="pay-title-remove">×</span>
            输入支付密码
        </div>
        <divclass="pay-body">
            <divclass="input-container">
                <inputclass="input-item"type="password"readonly>
                <inputclass="input-item"type="password"readonly>
                <inputclass="input-item"type="password"readonly>
                <inputclass="input-item"type="password"readonly>
                <inputclass="input-item"type="password"readonly>
                <inputclass="input-item"type="password"readonly>
            </div>
            <divclass="forgetPwd-container">
                <aclass="forgetPwd"href="">忘记密码?</a>
            </div>
            <divclass="key-container">
                <divclass="key-item">1</div>
                <divclass="key-item">2</div>
                <divclass="key-item">3</div>
                <divclass="key-item">4</div>
                <divclass="key-item">5</div>
                <divclass="key-item">6</div>
                <divclass="key-item">7</div>
                <divclass="key-item">8</div>
                <divclass="key-item">9</div>
                <divclass="key-item empty"></div>
                <divclass="key-item">0</div>
                <divclass="key-item remove"></div>
            </div>
        </div>
    </div>
登录后复制

CSS部分

.pay-container{ width:7.5rem; height:8rem; background-color:#fbf9fb; position:fixed;z-index:999; overflow:hidden;display:none; }
/* .pay-container-show{transform: translate3d(0, -8.9rem, 0);transition: 0.5s ease;transform: translate3d(0, 0, 0); transition: 0.5s ease;} */
.pay-title{ height:0.96rem; line-height:0.96rem; border-bottom:1pxsolid#b3afaf; text-align:center; color:#070707;
position:relative; font-size:0.36rem;}
.pay-title.pay-title-remove{ width:0.24rem; height:0.24rem; position:absolute; top:0.35rem; left:0.33rem; line-height:0.28rem;
font-size:0.45rem;}
.pay-body{ padding-top:0.56rem;position:relative; height:7rem; box-sizing:border-box;}
.pay-body.input-container{ width:6.74rem; height:0.93rem; border:1pxsolid#ebe8eb; overflow:hidden; border-radius:5px;
background-color:#fff; margin:0auto; display:flex;flex-direction:row;align-items:center; 
flex-wrap:wrap; justify-content:center;align-content:center;}
.pay-body.input-container.input-item{ width:1.1rem; height:0.92rem; display:inline-block; margin:0; border-right:1pxsolid#ebe8eb;
text-align:center; line-height:0.92rem; border-radius:0; }
.pay-body.input-container.input-item:nth-last-child(1){ border-right:0;}
.pay-body.forgetPwd-container{width:6.74rem;margin:0.22remauto0; text-align:right;}
.pay-body.forgetPwd-container.forgetPwd{ color:#52bfff; font-size:0.24rem; }
.pay-body.key-container{ width:100%; height:4.56rem; position:absolute; bottom:0; display:flex;flex-direction:row;align-items:center; 
    flex-wrap:wrap; justify-content:center;align-content:center; }
.pay-body.key-container.key-item{ width:2.47rem; height:1.12rem; line-height:1.12rem; text-align:center; border-right:2pxsolid#f3f3f3;
    border-top:2pxsolid#f3f3f3; font-size:0.66rem; color:#1e1d1f;background-color:#fff;}
.pay-body.key-container.key-item:nth-child(3),
.pay-body.key-container.key-item:nth-child(6),
.pay-body.key-container.key-item:nth-child(9),
.pay-body.key-container.key-item:nth-child(12){ border-right:0;}
.pay-body.key-container.key-item.remove,.pay-body.key-container.key-item.empty{ font-size:0.24rem;background-color:#e6e9f1;}
.pay-body.key-container.key-item.remove{ background:url(&#39;../images/pay-remove.png&#39;) centerno-repeat#e6e9f1; background-size:.52rem.32rem; }
.pay-body.key-container.selected{ background-color:#e4e8f4;}
登录后复制

核心JS部分

var arr = [];
        var num =0;

        //响应键盘事件
        $(&#39;.key-item&#39;).on(&#39;touchstart&#39;, function () {
            $(this).addClass(&#39;selected&#39;)
        })
        $(&#39;.key-item&#39;).on(&#39;touchend&#39;, function () {
            $(this).removeClass(&#39;selected&#39;)
        })
        $(&#39;.key-item&#39;).on(&#39;click&#39;, function () {
            var value =$(this).text();
            var inputItem =$(&#39;.layui-m-layercont .input-item&#39;);
            if (!$(this).hasClass(&#39;remove&#39;)) {
                if (num <6) {
                    $(inputItem[num]).val(value);
                    if (num ==5) {
                        var arr = [];
                        for (var i =0; i < inputItem.length; i++) {
                            arr.push(inputItem[i].value)
                        }
                        arr =parseInt(arr.join(&#39;&#39;));
                        if (arr !==123456) {
                            layer.open({
                                content:&#39;支付密码错误请重新输入!&#39;,
                                skin:&#39;msg&#39;,
                                time:2//2秒后自动关闭
                            });
                        } else {
                            layer.open({
                                content:&#39;输入正确!&#39;,
                                skin:&#39;msg&#39;,
                                time:2//2秒后自动关闭
                            });
                        }
                        num++;
                        returnfalse;
                    }
                    num++;
                }
            } else {
                if (num >0) {
                    num--;
                    $(inputItem[num]).val(&#39;&#39;);
                }
            }
        })
登录后复制

代码就这么多,也不复杂。可能写的比较粗陋,但是效果能出来。

相关推荐:

JQuery Mobile支付宝手机支付网页

谁来讲讲移动客户端去支付宝支付然后返回的过程?

android 客户端支付宝 php服务器端编写

以上就是使用jquery实现类似于移动端支付宝的支付键盘(代码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号