sdc-alibaba / SUI-Mobile

SUI Mobile (MSUI)是由阿里巴巴国际UED前端出品的移动端UI库,轻量精美
http://m.sui.taobao.org/components/
MIT License
6.1k stars 1.58k forks source link

解决IOS下多个 select ,点击某一个,焦点不停变换的bug #581

Open bigbencat opened 8 years ago

bigbencat commented 8 years ago

一个tab下面多个select,点击其中一个,有时候焦点会跳过好几个,页面越长,越难选择。 解决:如果是select,不使用fastclick。就不提交pull了,放在这里,如果有人碰到这样的问题,参考一下。

`/* * On touch start, record the position and scroll offset. * @param {Event} event * @returns {boolean} */ FastClick.prototype.onTouchStart = function(event) { var targetElement, touch, selection;

    // Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
    if (event.targetTouches.length > 1) {
        return true;
    }

    targetElement = this.getTargetElementFromEventTarget(event.target);
    touch = event.targetTouches[0];

    if (deviceIsIOS) {
        //add by 我 解决select 点击老跳转的问题 begin
        var nodeName = targetElement.nodeName.toLowerCase();
        var typeAttribute = targetElement.getAttribute('type');
        if (nodeName === "select"){
            return false;
        }
        //add by 我 解决select 点击老跳转的问题  end`
bobhfut commented 8 years ago

我碰到了。。。谢谢楼主

qizhanyu commented 8 years ago

请问具体如何使用呢?

bigbencat commented 8 years ago

修改fastclick

qizhanyu commented 8 years ago

谢谢,已经在源码中找到了。

mingyili commented 8 years ago

@bigbencat 这个办法是直接禁止了select的fastclcik,最近找到了不用禁掉fastclick就能防止这个问题的方法: 在源码里的onTouchEnd事件下有一段判断是否需要needsFocus的代码。。问题的根源啊,

// Select elements need the event to go through on iOS 4, otherwise the selector menu won't open.
// Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others)
if (!deviceIsIOS || targetTagName !== 'select') {
     this.targetElement = null;
     event.preventDefault();
}

这断代码大致是为了在ios 下 select 时 this.targetElement 不置空继续执行原生选择事件,好打开select menu,但是却导致了二次触发。也就是当ios下页面过长,触发select导致页面滑动的情况下发生二次触发,焦点错位的原因。

所以要做的就是把这段判断改了就好

// Select elements need the event to go through on iOS 4, otherwise the selector menu won't open.
// Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others)
// if (!deviceIsIOS || targetTagName !== 'select') {}
this.targetElement = null;
event.preventDefault();

这样就能在ios下fastclick select 同时不引起焦点错位。

yuxizhe commented 7 years ago

改了之后。点击select,响应很慢怎么解决呢

buildnewapp commented 6 years ago

good $('select').on('touchstart',function(e) { e.preventDefault(); });