Mobile phone-shaking function code implementation

Source: Internet
Author: User

The Web page of the mobile phone shake effect, JS code as follows

var speed;//to determine if the phone has a shake function if (window. Devicemotionevent) {//Bind Devicemotion event Window.addeventlistener (' Devicemotion ', Devicemotionhandler, false);}    else{alert (' Your device does not support shake-shake function, the system will automatically shake your phone ^_^ ');    Speed = 888;  Do not support shake when shaking, modify speed value trigger Shake Phone event}//initialize var shake_threshold = 800;var last_update = 0;var x, y, Z, last_x, last_y, last_z;function    Devicemotionhandler (eventData) {var acceleration =eventdata.accelerationincludinggravity;    var curtime = new Date (). GetTime ();        if ((curtime-last_update) >) {var difftime = curtime-last_update;        Last_update = Curtime;        x = acceleration.x;        y = acceleration.y;        z = acceleration.z;        Speed = Math.Abs (x +y + z-last_x-last_y-last_z)/difftime * 10000;        Speed = 888;        if (Speed > Shake_threshold) {alert ("shaked!") when the velocity is higher than the threshold value;        } last_x = x;        last_y = y;    Last_z = Z; }}

  

Mobile phone-shaking function code implementation

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.