UIWebvView 解决onClick 延迟相应问题

本文讨论了在使用UIWebView过程中遇到的onClick触发延迟问题,并提供了通过使用onTouchStart替代onClick以及引入开源库fastclick来解决此问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用 UIWebView 的过程中, 发现 onClick 触发需要等待300-500ms, Google了一下, 发现是因为ScrollView 在等待doubleTap, 所以有延迟

使用如下代码是无效的.

[webView.scrollView setDelaysContentTouches:NO]

目前的解决方案是使用onTouchStart代替onClick, 找到了如下一段js代码

function NoClickDelay(el) {
    this.element = typeof el == 'object' ? el : document.getElementById(el);
    if( window.Touch ) this.element.addEventListener('touchstart', this, false);
}

NoClickDelay.prototype = {
    handleEvent: function(e) {
        switch(e.type) {
            case 'touchstart': this.onTouchStart(e); break;
            case 'touchmove': this.onTouchMove(e); break;
            case 'touchend': this.onTouchEnd(e); break;
        }
    },

    onTouchStart: function(e) {
        e.preventDefault();
        this.moved = false;

        this.theTarget = document.elementFromPoint(e.targetTouches[0].clientX, e.targetTouches[0].clientY);
        if(this.theTarget.nodeType == 3) this.theTarget = theTarget.parentNode;
        this.theTarget.className+= ' pressed';

        this.element.addEventListener('touchmove', this, false);
        this.element.addEventListener('touchend', this, false);
    },

    onTouchMove: function(e) {
        this.moved = true;
        this.theTarget.className = this.theTarget.className.replace(/ ?pressed/gi, '');
    },

    onTouchEnd: function(e) {
        this.element.removeEventListener('touchmove', this, false);
        this.element.removeEventListener('touchend', this, false);

        if( !this.moved && this.theTarget ) {
            this.theTarget.className = this.theTarget.className.replace(/ ?pressed/gi, '');
            var theEvent = document.createEvent('MouseEvents');
            theEvent.initEvent('click', true, true);
            this.theTarget.dispatchEvent(theEvent);
        }

        this.theTarget = undefined;
    }
};

使用方法

new NoClickDelay(document.getElementById('element'));

jQuery 版本:

(function( $ ) {
    $.fn.noClickDelay = function() {
        var $wrapper = this;
        var $target = this;
        var moved = false;
        $wrapper.bind('touchstart mousedown',function(e) {
            e.preventDefault();
            moved = false;
            $target = $(e.target);
            if($target.nodeType == 3) {
                $target = $($target.parent());
            }
            $target.addClass('pressed');
            $wrapper.bind('touchmove mousemove',function(e) {
                moved = true;
                $target.removeClass('pressed');
            });
            $wrapper.bind('touchend mouseup',function(e) {
                $wrapper.unbind('mousemove touchmove');
                wrapper.unbind('mouseup touchend');
                if(!moved && $target.length) {
                    $target.removeClass('pressed');
                    $target.trigger('click');
                    $target.focus();
                }
            });
        });
    };
})( jQuery );    
$('#wrapperElement').noClickDelay();

----------------------------update. 找到一更完美的开源库

fastclick

 

转载于:https://www.cnblogs.com/duwei/p/5145686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值