通過阻止 touchstart 事件,解決移動端的 BUG

來源:互聯網
上載者:User

標籤:nbsp   hidden   window   efault   移動   dom   pre   tar   function   

在 iOS10 下,即使設定了 meta 標籤的 user-scalable=no 依然不能阻止使用者縮放。

解決此 BUG 的方法是阻止 PC 事件:

document.addEventListener(‘touchstart‘, function(ev) {    ev.preventDefault();});


另外,此方法還能解決 iOS10 中橫向頁面溢出,即使給 body 設定了 overflow:hidden 依然能橫向滑動的問題。

不過添加了此事件後會阻止 iOS10 的橡皮筋效果,另外也會禁止文字選中功能。

通過阻止 touchstart 的預設事件來解決此 BUG,另外阻止此預設事件還可以解決 300ms 延遲的 BUG。

 

當然,由於阻止了預設行為,那麼 a 標籤連結的預設跳轉頁就無法實現了,只能通過手動添加事件:

aDom.addEventListener(‘touchstart‘, function(ev) {    window.location.href = "www.tirion.me";});

輸入框擷取焦點也需要手動解決:

inputDom.addEventListener(‘touchstart‘, function(ev) {    ev.stopPropagation();});

 

通過阻止 touchstart 事件,解決移動端的 BUG

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.