How to use H5 to calculate the number of times the phone is shaken
This time I will bring you H5 to calculate the number of times the phone is shaken. What are the precautions for H5 to calculate the number of times the phone is shaken. The following is a practical case, let’s take a look.
1. deviceOrientation: an event that encapsulates the orientation sensor data. It can obtain the orientation data of the mobile phone in the static state, such as the angle, orientation, orientation, etc. of the mobile phone. .
2. deviceMotion: an event that encapsulates motion sensor data and can obtain data such as motion acceleration when the mobile phone is in motion.
Not much to say, just go to the code:
var jsonObject=null; // 当页面加载完以后会执行window.onload window.onload = function() { var times = -1; // 记录摇动次数 var last_time = 0; var borderSpeed = 800; // 加速度变化临界值 var x = y = z = last_x = last_y = last_z = 0; if (window.DeviceMotionEvent) { window.addEventListener('devicemotion',shake,false); } else { alert('您的设备不支持摇一摇哦'); } // 每次手机移动的时候都会执行下面shake函数的代码 function shake(eventData) { var acceleration = eventData.accelerationIncludingGravity; var curTime = new Date().getTime(); var diffTime = curTime-last_time; // 每隔100ms进行判断 if (diffTime>100) { x = acceleration.x; y = acceleration.y; z = acceleration.z; var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000; // 判断手机确实发生了摇动而不是正常的移动 if (speed>borderSpeed) { times++; document.getElementById("text").innerHTML=times+" times"; // 用户的微信昵称和头像连接发送一次即可,不需要每次都发送 if (times==0) { document.forms["insertForm"].headimg.value =img ; document.forms["insertForm"].user.value = nickname; } document.forms["insertForm"].time.value = times; } last_time = curTime; last_x = x; last_y = y; last_z = z; } } }
html:
<img src="/static/imghwm/default1.png" data-src="hand.png" class="lazy" alt="How to use H5 to calculate the number of times the phone is shaken" > <br><font></font>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
H5’s window.postMessage and cross-domain
H5 multi-threading (Worker SharedWorker) use Detailed explanation
The above is the detailed content of How to use H5 to calculate the number of times the phone is shaken. For more information, please follow other related articles on the PHP Chinese website!

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
