JavaScript (JS) language in the mobile front-end can be said to be essential, many of the effects are related to JS, including the current mobile side of the framework: Jqmobi, Jqtouch, Sencha Touch, jquerymobile and so on. are written based on JS.
Not discuss these frameworks for the time being, mainly discuss the use of some commonly used JS events on the mobile side. For a few examples:
1. Hide the address navigation bar:
<script>//the function is written by Simon Willison, which has only one parameter that represents the called function name (the name of the function that executes when the page is loaded) functionAddloadevent (func) {varOldonload =window.onload; if(typeofWindow.onload! = ' function ') {window.onload=func; } Else{window.onload=function() {oldonload (); Func (); } } } //Add Load Event handlingaddloadevent (Hidemenu); functionHidemenu () {setTimeout ("Window.scrollto (0, 0)", 1); }</script>
The above code is for the mobile side of the iOS system to shrink the address navigation bar (some Andro can also). The core code is one sentence: SetTimeout ("Window.scrollto (0, 0)", 1); Use the timer to move the window to the top of the content side, thus hiding the address navigation bar.
2, judge the status of mobile phone screen:
<script>function hengshuping () { if(window.orientation==180| | Window.orientation==0) { alert ("Vertical screen status! ") }if(window.orientation==90| | Window.orientation==-90) { alert ("Horizontal screen state! ") }}window.addeventlistener (infalse); </script>
Mobile browser generally support window.orientation This parameter, through this parameter can determine whether the phone is in a horizontal screen or vertical screen state, so as to execute the corresponding program according to the actual needs, through the addition of monitoring event onorientationchange, execution can be.
3. Decide whether to enter the website from the main screen icon:
<script>if in navigator &&!navigator.standalone && (/iphone|ipod|ipad/gi). Test (Navigator.platform) && (/safari/i). Test (navigator.appversion) && window.orientation==90| | Window.orientation==-90) { document.write (' <link rel= "stylesheet" href= "Css/add2home.css" > " ); document.write (' <script type= "Application\/javascript" src= "Js/add2home.js" charset= "utf-8" ><\/s ' + ' Cript> '); }</script>
Determine whether to enter the site from the home screen icon by judging the Navigator.standalone. This is mainly used in the WebApp end or web-side access to the site, so that the corresponding operation.
This example is Navigator.standalone equals false, which is a Web site that is accessed from the browser (web side). Then write two pieces of code in the document file.
4. Cancel Browser default behavior:
var function (event) { event.preventdefault (); Window.Scroll (0,0); return false ; }; Document.addeventlistener (false);
The default behavior of the browser is to control some gesture events, so that they can follow the specific requirements of the corresponding operation. The core code is Event.preventdefault ();
When the finger slips (touchmove), the trigger, about the finger event, is discussed in detail next time.
5, Mobile phone acceleration events:
<script>varPage=1; varXx=0; varJishu=0; functionDevicemotionhandler (eventData) {varAcceleration =eventdata.accelerationincludinggravity; varzhuanhoux=Math.Round (acceleration.x); if(xx-zhuanhoux>3| | Xx-zhuanhoux<-3) {xx=Zhuanhoux; Jishu++; } if(jishu>10){ varimgshu=$ ("#yidongDiv"). FIND ("img")). length; varyidongjuli=$ ("#rongqi"). width (); if(!$ ("#yidongDiv"). Is (": animated"))){ if(page==Imgshu) { $("#yidongDiv"). Animate ({left: ' 0px '},500) Page=1; $("#changwenzi"). attr ("src", "images/wenzi1.png")) } Else{ $("#yidongDiv"). Animate ({left: '-= ' +yidongjuli},500) $("#changwenzi"). attr ("src", "Images/wenzi" + (page+1) + ". png") Page++; }} Jishu=0; XX=0; }; } window.addeventlistener (' Devicemotion ', Devicemotionhandler,false);</script>
In the example, an instance of an animation is carried out by acquiring the acceleration of a mobile phone.
The Devicemotion event can already be supported in IOS5 or later. Android4.0 also began to support the event.
Eventdata.accelerationincludinggravity This property gets a set of values that includes an array of x\y\z axes. through to Var zhuanhoux=math.round (acceleration.x); The positive and negative values of the X-value determine whether the phone is in a horizontal or left-shaking state, causing animation.
Here is the introduction of 5 commonly used, and then slowly add other JS methods in the mobile application ...
Mobile End--js (i)