Vue實(shí)現(xiàn)搖一搖功能(兼容ios13.3以上)
最近做了個(gè)搖一搖類似的功能,使用的是shake.js,但在ios13.3之前的版本中可以觸發(fā)搖一搖,之后的版本需要兼容,需要制作一個(gè)讓用戶能手動(dòng)點(diǎn)擊的彈框,才能使用戶授權(quán)動(dòng)作與方向的權(quán)限。(需使用https協(xié)議)
<van-popup v-model='isTip' :close-on-click-overlay='false'> <div class='mainBody'> <h3 class='systemTip'>溫馨提示</h3> <div class='confirm'> 由于ios系統(tǒng)需要手動(dòng)獲取訪問動(dòng)作與方向的權(quán)限,為保障游戲的正常進(jìn)行,請(qǐng)?jiān)谠L問提示中點(diǎn)擊允許。 </div> </div> <button @click='handleInit'> 知道了 </button></van-popup>
shake.js
//引入shake.jscreated(){ this.initShake() const isAction = JSON.parse(localStorage.getItem(’getAction’)) var ua = navigator.userAgent.toLowerCase(); if(ua.indexOf('like mac os x') > 0){ var reg = /os [d._]*/gi ; var verinfo = ua.match(reg) ; var version = (verinfo+'').replace(/[^0-9|_.]/ig,'').replace(/_/ig,'.'); if (parseFloat(version) >= 13.3 && !isAction){ localStorage.setItem('getAction',true) this.isTip = true } }},methods:{ initShake(){ this.myShakeEvent = new Shake({ threshold: 15, // 搖動(dòng)閾值 timeout: 1000 // 事件發(fā)生頻率,是可選值 }); this.myShakeEvent.start(); window.addEventListener(’shake’, xx); }, handleInit(){ this.isTip = false this.ios13granted() }, ios13granted() { if (typeof DeviceMotionEvent.requestPermission === ’function’) { DeviceMotionEvent.requestPermission().then(permissionState => { if (permissionState === ’granted’) { this.initShake() //搖一搖 } else if(permissionState === ’denied’){// 打開的鏈接不是https開頭 alert('當(dāng)前IOS系統(tǒng)拒絕訪問動(dòng)作與方向。請(qǐng)退出微信,重新進(jìn)入活動(dòng)頁(yè)面獲取權(quán)限?;蛑苯狱c(diǎn)擊抽簽桶參與活動(dòng)') } }).catch((error) => { alert('請(qǐng)求設(shè)備方向或動(dòng)作訪問需要用戶手勢(shì)來(lái)提示') }) } else { // 處理常規(guī)的非iOS 13+設(shè)備 alert('處理常規(guī)的非iOS 13+設(shè)備') } },}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲2. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能3. HTML5 Canvas繪制圖形從入門到精通4. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解5. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法6. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息7. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式8. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))9. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明
