长按触发(PC端和移动端)


$.fn.longPress = function(fn) {
                var timeout = undefined;
                var $this = this;
                for(var i = 0;i<$this.length;i++){
                    $this[i].addEventListener('touchstart', function(e) {
                        e.preventDefault();
                        timeout = setTimeout(fn, 800);
                        }, false);
                    $this[i].addEventListener('mousedown', function(e) {
                        e.preventDefault();
                        timeout = setTimeout(fn, 800);
                    }, false);
                    $this[i].addEventListener('touchend', function(e) {
                        e.preventDefault();
                        clearTimeout(timeout); 
                    }, false);
                    $this[i].addEventListener('mouseup', function(e) {
                        e.preventDefault();
                        clearTimeout(timeout);
                    }, false);
                }
            }
            $('button').longPress(function(){
                alert('长按')
            })

可用于多个元素


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM