移動端的長按事件的實現


代碼

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title></title>
        <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
    </head>
    <body>
        <div style="width:100%;">
            <div id="touchArea" style="width:90%; height:200px; background-color:#CCC;font-size:100px">長按我</div> 
        </div>
        <script>
        var timeOutEvent=0;
        $(function(){
            $("#touchArea").on({
                touchstart: function(e){
                    timeOutEvent = setTimeout("longPress()",500);
                    e.preventDefault();
                },
                touchmove: function(){
                    clearTimeout(timeOutEvent); 
                    timeOutEvent = 0; 
                },
                touchend: function(){
                    clearTimeout(timeOutEvent);
                    if(timeOutEvent!=0){ 
                        alert("你這是點擊,不是長按"); 
                    } 
                    return false; 
                }
            })
        });
        function longPress(){ 
            timeOutEvent = 0; 
            alert("長按事件觸發發"); 
        } 
        </script>
    </body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM