Vue implements the shake function (compatible with ios13.3 and above)

Vue implements the shake function (compatible with ios13.3 and above)

Recently, I made a function similar to shake, using shake.js. However, the shake function can be triggered in versions before ios13.3, and later versions need to be compatible. It is necessary to create a pop-up box that users can click manually so that users can authorize permissions for actions and directions. (https protocol is required)

<van-popup v-model="isTip" class="popInfo" :close-on-click-overlay="false">
 <div class="mainBody">
 <h3 class="systemTip">Warm Tips</h3>
 <div class="confirm">
 Since the iOS system needs to manually obtain permission to access actions and directions, to ensure the normal progress of the game, please click Allow in the access prompt.
 </div>
 </div>
 <button class="bottomButton" @click="handleInit">
 Got it</button>
</van-popup>

shake.js

//Introduce shake.js
created(){
 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, // shake threshold timeout: 1000 // event frequency, optional value});
 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() //Shake} else if(permissionState === 'denied'){// The link you opened does not begin with https alert("The current IOS system denies access to actions and directions. Please exit WeChat and re-enter the event page to obtain permissions. Or directly click on the lottery bucket to participate in the event")
 }
 }).catch((error) => {
 alert("Requesting device orientation or motion access requires user gestures to prompt")
 })
 } else {
 // Handle regular non-iOS 13+ device alert("Handle regular non-iOS 13+ devices")
 }
 },
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript+H5 to implement WeChat shake function
  • Analysis of the principle of WeChat shake using JS
  • Implementation of the shake function in javascript html5

<<:  Detailed explanation of nginx optimization in high concurrency scenarios

>>:  What to do if you forget the initial password of MySQL on MAC

Recommend

How to run JavaScript in Jupyter Notebook

Later, I also added how to use Jupyter Notebook i...

Node quickly builds the backend implementation steps

1. First install node, express, express-generator...

Analysis of Linux kernel scheduler source code initialization

Table of contents 1. Introduction 2. Basic Concep...

JavaScript design pattern learning proxy pattern

Table of contents Overview Implementation Protect...

What is HTML?

History of HTML development: HTML means Hypertext...

Docker and portainer configuration methods under Linux

1. Install and use Docer CE This article takes Ce...

JavaScript Objects (details)

Table of contents JavaScript Objects 1. Definitio...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

Linux kernel device driver kernel debugging technical notes collation

/****************** * Kernel debugging technology...

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...

MySql multi-condition query statement with OR keyword

The previous article introduced the MySql multi-c...

How to create a file system in a Linux partition or logical volume

Preface Learn to create a file system on your sys...