Html5 code optimization, including DeviceMotionEvent and so on _ html5 tutorial skills-

Source: Internet
Author: User
Optimize DeviceMotionEvent. In addition to useless code, re-encapsulate DeviceMotionEven. In addition, you cannot continue to execute the DeviceMotionEvent event after the animation is executed. Therefore, optimize the DeviceMotionEvent first;

Remove useless code and re-encapsulate DeviceMotionEven

The Code is as follows:


If (window. DeviceMotionEvent ){
Var speed = 25; // define a value
Var x = y = z = lastX = lastY = lati = 0; // reset all values
Window. addEventListener ('deviceid', function (){
Var acceleration = event. accelerationIncludingGravity; // assign the sensing value to acceleration
X = acceleration. x;
Y = acceleration. y;
Z = acceleration. z;
If (Math. abs (x-lastX)> speed | Math. abs (y-lastY)> speed ){
// TODO: Here, you can perform Logical Data operations after a shake.
Donghua ();
}
LastX = x;
LastY = y;
Las-1 = z;
}, False );
}


Because there are many requirements in the actual project,

For example, you cannot continue to execute the DeviceMotionEvent event when the animation is not completed;

So we made further optimization;

The Code is as follows:


Var f = 1;
Function donghua (){
// Animation events
$ (". Img"). animate ({left: '0', opacity: '1'}, 700, function () {f = 1 ;});
});
If (window. DeviceMotionEvent ){
Var speed = 25; // define a value
Var x = y = z = lastX = lastY = lati = 0; // reset all values
Window. addEventListener ('deviceid', function (){
Var acceleration = event. accelerationIncludingGravity; // assign the sensing value to acceleration
X = acceleration. x;
Y = acceleration. y;
Z = acceleration. z;
If (Math. abs (x-lastX)> speed | Math. abs (y-lastY)> speed ){
// TODO: Here, you can perform Logical Data operations after a shake.
If (f = 1 ){
Donghua ();
F = 0;
}
}
LastX = x;
LastY = y;
Las-1 = z;
}, False );
}


Now it's perfect.

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.