HTML5 uses DeviceOrientation to implement the shake function _ jquery

Source: Internet
Author: User
This article describes how to use DeviceOrientation to implement the shake function in HTML5. For more information, see the important feature of HTML5: DeviceOrientation, it encapsulates the underlying direction and motion sensors in advanced mode, which enables us to easily implement interesting functions such as gravity sensing and compass. This article will introduce how to use HTML5 gravity motion and orientation sensors to shake mobile phones.

DeviceOrientation includes two events:

1. deviceOrientation: encapsulates the event of the direction sensor data to obtain the direction data in the mobile phone's static state, such as the angle, orientation, and orientation of the mobile phone.
2. deviceMotion: events that encapsulate motion sensor data can be used to obtain Motion Acceleration and other data in the mobile phone's motion status.

HTML

There is a p # hand on the page, which is used to hold a hand shake image, and p # result is used to display the result information after shaking.

We can use CSS3 to enhance the page effect, and use the-webkit-animation effect to achieve the dynamic effect of the hand image. For details, please download the source code to view.

Javascript

The "Shake" action is "the device has been in a certain distance for a certain period of time". Therefore, the devicemotion listens to the x, y, the change rate of the Z axis value within a certain period of time, that is, to determine whether the device is shaking. To prevent misjudgment of normal movement, a critical value must be set for the change rate.
In HTML5, we use the encapsulated shake. js code to determine device shaking. The project address is https://github.com/alexgibson/shake.js.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.