溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

解決vue 界面在蘋果手機(jī)上滑動(dòng)點(diǎn)擊事件等卡頓問題

發(fā)布時(shí)間:2020-09-04 13:56:21 來源:腳本之家 閱讀:368 作者:honey緣木魚 欄目:web開發(fā)

用vue編寫項(xiàng)目接近尾聲,需要集成到移動(dòng)端中,在webstorm上界面,運(yùn)行效果都很完美,但是在蘋果手機(jī)上各種問題都出現(xiàn)了,原生項(xiàng)目一向滑動(dòng)流暢,事件響應(yīng)迅速,可是蘋果手機(jī)打開這個(gè)項(xiàng)目有兩個(gè)問題,

(1).滑動(dòng)頁(yè)面卡頓,

(2).點(diǎn)擊事件響應(yīng)緩慢,百度才發(fā)現(xiàn)在蘋果手機(jī)上有300ms的延遲。

一.滑動(dòng)頁(yè)面卡頓

//頁(yè)面布局
<template>
 <div class='content'>
  頁(yè)面內(nèi)容
 </div>
</template>

在對(duì)應(yīng)的組件的最外層div上加上這樣的樣式:

.content{
  -webkit-overflow-scrolling: touch;
  }

-webkit-overflow-scrolling: touch;這句代碼最好可在公共的樣式中添加,防止很多頁(yè)面都需要寫的麻煩。這句代碼雖然可以解決滑動(dòng)不流暢的問題,但是可能會(huì)引起幾個(gè)小問題:

(1).在滑動(dòng)界面之中使用的position:fixed 無法固定下來,會(huì)隨著界面進(jìn)行一起滾動(dòng)

解決方法:將使用的position:fixed(頭部導(dǎo)航)寫在滑動(dòng)部位外部,在使用絕對(duì)定位進(jìn)行布局,以此解決問題

(2).vue中使用v-if導(dǎo)致的界面第一次無法滑動(dòng)

解決方法:將v-if改成v-show進(jìn)行展示,解決界面進(jìn)入之后不能滑動(dòng)的問題

二.點(diǎn)擊事件響應(yīng)緩慢

(1).安裝fastclick (npm install fastclick -S)

(2).在main.js中設(shè)置方法

import FastClick from 'fastclick'
FastClick.attach(document.body);

在引入fastclick之后,雖然頁(yè)面事件快了很多,但是會(huì)有一個(gè)副作用:input輸入框需要連續(xù)點(diǎn)擊兩次或者長(zhǎng)按才能獲取焦點(diǎn),真是到處是坑?。?br />

解決方法:在main.js中添加下面的代碼

FastClick.prototype.focus = function(targetElement) {
 var length;
// Issue #160: on iOS 7, some input elements (e.g. date datetime month) throw a vague TypeError on setSelectionRange. These elements don't have an integer value for the selectionStart and selectionEnd properties, but unfortunately that can't be used for detection because accessing the properties also throws a TypeError. Just check the type instead. Filed as Apple bug #15122724.
 if (deviceIsIOS&& targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
  length = targetElement.value.length;
  targetElement.focus();
  targetElement.setSelectionRange(length, length);
 } else {
  targetElement.focus();
}
};

總結(jié)

以上所述是小編給大家介紹的解決vue 界面在蘋果手機(jī)上滑動(dòng)點(diǎn)擊事件等卡頓問題,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI