1. 程式人生 > >H5自定義金額鍵盤,改良後ios體驗效果流暢

H5自定義金額鍵盤,改良後ios體驗效果流暢

下載的別人的外掛改良一下,原始碼地址:https://github.com/XieTongXue/how-to/tree/master/pay-h5

沒有外掛,直接來程式碼

<div class="pay-input-money">
        <div class="input-left">
            <span>輸入消費金額</span>
        </div>
        <div class="input-right">
            <span></span>
<label id="paymoney" type="text"></label> </div> </div> <div class="payinfo"> <table cellspacing="0" cellpadding="0"> <tr> <td class="paynum">1</td> <td class="paynum">2</td> <
td class="paynum">3</td> <td id="pay-return"> <div class="keybord-return"></div> </td> </tr> <tr> <td class="paynum">4</td> <td class="paynum">5</td> <
td class="paynum">6</td> <td rowspan="3" class="pay"> <a href="javascript:return false;"> <div class="a-pay"> <p>確認</p> <p>支付</p> </div> </a> </td> </tr> <tr> <td class="paynum">7</td> <td class="paynum">8</td> <td class="paynum">9</td> </tr> <tr> <td id="pay-zero" colspan="2" class="payzero">0</td> <td id="pay-float">.</td> </tr> </table> </div>

css程式碼可以參考原始碼

js程式碼:

$(".paynum").each(function(){
        
            $(this).on("touchstart",function(e){
                e.preventDefault();
                if(($paymoney.text()).indexOf(".") != -1 && ($paymoney.text()).substring(($paymoney.text()).indexOf(".") + 1, ($paymoney.text()).length).length == 2){
                    return;
                }
                if($.trim($paymoney.text()) == "0"){
                    return;
                }
                if (parseInt($paymoney.text()) > 10000 && $paymoney.text().indexOf(".") == -1) {
                    return;
                }
                // $('.pay').removeClass('pay-disabled').find('a').attr('href','pay-result.html');
                $paymoney.text($paymoney.text() + $(this).text());
                // upperCaseMoney.text(digitUppercase($paymoney.text()));
            });
        });
        
        $("#pay-return").on("touchstart",function(e){
            e.preventDefault();
            $paymoney.text(($paymoney.text()).substring(0, ($paymoney.text()).length - 1));
            upperCaseMoney.text(digitUppercase($paymoney.text()));
//             if (!$paymoney.text()) {
//                 upperCaseMoney.text('');
//                 $('.pay').addClass('pay-disabled').find('a').attr('href', 'javascript:return false;');
//             }
        });
        
        $("#pay-zero").on("touchstart",function(e){
            e.preventDefault();
            if(($paymoney.text()).indexOf(".") != -1 && ($paymoney.text()).substring(($paymoney.text()).indexOf(".") + 1, ($paymoney.text()).length).length == 2){
                return;
            }
            if($.trim($paymoney.text()) == "0"){
                return;
            }
            if (parseInt($paymoney.text()) > 10000 && $paymoney.text().indexOf(".") == -1) {
                return;
            }
            $paymoney.text($paymoney.text() + $(this).text());
        });
        
        $("#pay-float").on("touchstart",function(e){
            e.preventDefault();
            if($.trim($paymoney.text()) == ""){
                return;
            }
        
            if(($paymoney.text()).indexOf(".") != -1){
                return;
            }
            
            if(($paymoney.text()).indexOf(".") != -1){
                return;
            }
            
            $paymoney.text($paymoney.text() + $(this).text());
        });

將touchstart事件替換click時間,iOS變的較為流暢,程式碼僅做參考