Share the code of an instance and the code of the instance.
If you don't talk much about it, paste the Code directly. The specific code is as follows:
Var Shaker = function (f) {// SHAKE: it is detected that three shakes are calculated as one shake. After shaking, the handler is called, do not detect this. // f. callback = f; this. status = 0; // 0: listening has not started. 1: listening has started. this. speed = 15; this. lastX = this. lastY = this. larats = 0; this. num = 0; // The number of times this is triggered. minNum = 3; // minimum number of detection triggers this. beginSecond = 0; // The number of seconds to start detection this. maxSecond = 3; // The maximum interval of seconds this. handlerWrap = function () {};} Shaker. prototype. listen = function () {// listen to shake var that = this; if (this. status = 0 & window. deviceMotionEvent) {this. status = 1; this. handlerWrap = function (event) {that. handler (event)} window. addEventListener ('deviceid', this. handlerWrap, false) ;}} Shaker. prototype. release = function () {// stop listening if (this. status = 1) {if (window. deviceMotionEvent) {window. removeEventListener ('deviceid', this. handlerWrap);} this. status = 0; this. num = 0 ;}} Shaker. prototype. reset = function () {// reset Detection if (this. status = 1) {this. num = 0 ;}} Shaker. prototype. handler = function (event) {// sensor event processing if (this. status = 1) {var acceleration = event. accelerationIncludingGravity; var x = acceleration. x; var y = acceleration. y; var z = acceleration. z; if (Math. abs (x-this.lastX)> this. speed | Math. abs (y-this.lastY)> this. speed | Math. abs (z-this.lastZ)> this. speed) {if (this. num = 0) {this. beginSecond = Date. parse (new Date ()/1000} this. num + = 1;} this. lastX = x; this. lastY = y; this. las-1 = z; if (this. num> = this. minNum) {var now = Date. parse (new Date ()/1000; if (now-this. beginSecond <= this. maxSecond) {this. release (); if (this. callback) {this. callback () ;}} this. reset ();}}}
Usage:
Var s = new Shaker (function () {/* callback after shaking */}); s. listen ();
Well, this article will introduce you here, and will be updated continuously in the future. I hope this article will share with you the knowledge about the web page detection and shake instance code, which will be helpful to you.
Articles you may be interested in:
- Implementation ideas and code of the similar shake function of android
- JQuery + HTML5 implement the special effect of shaking your cell phone and changing your clothes
- HTML5 uses DeviceOrientation to implement the shake Function
- How to shake a screen in Android
- Official php interface (payment, red envelopes, shake, store)
- Use PHP to shake the surrounding red envelope