zoukankan      html  css  js  c++  java
  • UIWebvView 解决onClick 延迟相应问题

    在使用 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

    转载请注明出处:http://duwei.cnblogs.com/
  • 相关阅读:
    NOIP201310华容道
    NOIP201110观光公交
    markdown
    【NOIP2016】愤怒的小鸟
    【NOIP2016】组合数问题
    [TJOI2019]唱,跳,rap,篮球(生成函数,组合数学,NTT)
    CF1217E Sum Queries? (线段树)
    CF1178F Short/Long Colorful Strip(DP)
    ZROI 暑期高端峰会2019 总结
    [HNOI2012]集合选数(构造,状态压缩,DP)
  • 原文地址:https://www.cnblogs.com/duwei/p/5145686.html
Copyright © 2011-2022 走看看