PageRenderTime 191ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/api/events.html

https://github.com/xiaogongzhu/jquery-mobile
HTML | 194 lines | 149 code | 45 blank | 0 comment | 0 complexity | ea93f4b2c66abe5e63568a574db900b6 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>jQuery Mobile Docs - Events</title>
  7. <link rel="stylesheet" href="../../themes/default/" />
  8. <link rel="stylesheet" href="../_assets/css/jqm-docs.css"/>
  9. <script src="../../js/jquery.js"></script>
  10. <script src="../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
  11. <script src="../_assets/js/jqm-docs.js"></script>
  12. <script src="../../js/"></script>
  13. </head>
  14. <body>
  15. <div data-role="page" class="type-interior">
  16. <div data-role="header" data-theme="f">
  17. <h1>Events</h1>
  18. <a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
  19. </div><!-- /header -->
  20. <div data-role="content">
  21. <div class="content-primary">
  22. <p>jQuery Mobile offers several custom events that build upon native events to create useful hooks for development. Note that these events employ various touch, mouse, and window events, depending on event existence, so you can bind to them for use in both handheld and desktop environments. You can bind to these events like you would with other jQuery events, using <code>live()</code> or <code>bind()</code>.</p>
  23. <div class="ui-body ui-body-e">
  24. <h4 style="margin:.5em 0">Important: Use pageCreate(), not $(document).ready()</h4>
  25. The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the <code><strong>pagecreate</strong></code> event. This event is explained in detail at the bottom of this page.</div>
  26. <h2>Touch events</h2>
  27. <dl>
  28. <dt><code>tap</code></dt>
  29. <dd>Triggers after a quick, complete touch event.</dd>
  30. <dt><code>taphold</code></dt>
  31. <dd>Triggers after a held complete touch event (close to one second).</dd>
  32. <dt><code>swipe</code></dt>
  33. <dd><p>Triggers when a horizontal drag of 30px or more (and less than 20px vertically) occurs within 1 second duration but these can be configured:</p>
  34. <ul>
  35. <li><code>scrollSupressionThreshold</code> (default: 10px) More than this horizontal displacement, and we will suppress scrolling</li>
  36. <li><code>durationThreshold</code> (default: 1000ms) More time than this, and it isnt a swipe</li>
  37. <li><code>horizontalDistanceThreshold</code> (default: 30px) Swipe horizontal displacement must be more than this.</li>
  38. <li><code>verticalDistanceThreshold</code> (default: 75px) Swipe vertical displacement must be less than this.</li>
  39. </ul>
  40. </dd>
  41. <dt><code>swipeleft</code></dt>
  42. <dd>Triggers when a swipe event occurred moving in the left direction.</dd>
  43. <dt><code>swiperight</code></dt>
  44. <dd>Triggers when a swipe event occurred moving in the right direction.</dd>
  45. </dl>
  46. <h2>Orientation change event</h2>
  47. <dl>
  48. <dt><code>orientationchange</code></dt>
  49. <dd>Triggers when a device orientation changes (by turning it vertically or horizontally). When bound to this event, your callback function can leverage a second argument, which contains an <code>orientation</code> property equal to either "portrait" or "landscape". These values are also added as classes to the HTML element, allowing you to leverage them in your CSS selectors. Note that we currently bind to the resize event when orientationChange is not natively supported.</dd>
  50. </dl>
  51. <h2>Scroll events</h2>
  52. <dl>
  53. <dt><code>scrollstart</code></dt>
  54. <dd>Triggers when a scroll begins. Note that iOS devices freeze DOM manipulation during scroll, queuing them to apply when the scroll finishes. We're currently investigating ways to allow DOM manipulations to apply before a scroll starts.</dd>
  55. </dl>
  56. <dl>
  57. <dt><code>scrollstop</code></dt>
  58. <dd>Triggers when a scroll finishes.</dd>
  59. </dl>
  60. <h2>Page show/hide events</h2>
  61. <p>Whenever a page is shown or hidden in jQuery Mobile, two events are triggered on that page. The events triggered depend on whether that page is being shown or hidden, so when a page transition occurs, there are actually 4 events triggered: 2 for each page. </p>
  62. <dl>
  63. <dt><code>pagebeforeshow</code></dt>
  64. <dd>Triggered on the page being shown, before its transition begins.</dd>
  65. <dt><code>pagebeforehide</code></dt>
  66. <dd>Triggered on the page being hidden, before its transition begins.</dd>
  67. <dt><code>pageshow</code></dt>
  68. <dd>Triggered on the page being shown, after its transition completes.</dd>
  69. <dt><code>pagehide</code></dt>
  70. <dd>Triggered on the page being hidden, after its transition completes.</dd>
  71. </dl>
  72. <p>Note that all four of these events expose a reference to either the next page (<code>nextPage</code>) or previous page (<code>prevPage</code>), depending on whether the page is being shown or hidden, and whether that next or previous page exists (the first ever page shown does not have a previous page to reference, but an empty jQuery object is provided just the same). You can access this reference via the second argument of a bound callback function. For example: </p>
  73. <pre><code>
  74. $('div').live('pageshow',function(event, ui){
  75. alert('This page was just hidden: '+ ui.prevPage);
  76. });
  77. $('div').live('pagehide',function(event, ui){
  78. alert('This page was just shown: '+ ui.nextPage);
  79. });
  80. </code></pre>
  81. <p>Also, for these handlers to be invoked during the initial page load, you must bind them before jQuery Mobile executes. This can be done in the <code>mobileinit</code> handler, as described on the <a href="globalconfig.html">global config</a> page.
  82. <h2>Page initialization events</h2>
  83. <p>Internally, jQuery Mobile auto-initializes plugins based on the markup conventions found in a given "page". For example, an <code>input</code> element with a <code>type</code> of <code>range</code> will automatically generate a custom slider control.</p>
  84. <p>This auto-initialization is controlled by the "page" plugin, which dispatches events before and after it executes, allowing you to manipulate a page either pre-or-post initialization, or even provide your own intialization behavior and prevent the auto-initializations from occuring. Note that these events will only fire once per "page", as opposed to the show/hide events, which fire every time a page is shown and hidden.</p>
  85. <dl>
  86. <dt><code>pagebeforecreate</code></dt>
  87. <dd>
  88. <p>Triggered on the page being initialized, before most plugin auto-initialization occurs.</p>
  89. <pre><code>
  90. $('#aboutPage').live('pagebeforecreate',function(event){
  91. alert('This page was just inserted into the dom!');
  92. });
  93. </code></pre>
  94. <p>Note that by binding to <code>pagebeforecreate</code>, you can manipulate markup before jQuery Mobile's default widgets are auto-initialized. For example, say you want to add data-attributes via JavaScript instead of in the HTML source, this is the event you'd use.</p>
  95. <pre><code>
  96. $('#aboutPage').live('pagebeforecreate',function(event){
  97. // manipulate this page before its widgets are auto-initialized
  98. });
  99. </code></pre>
  100. </dd>
  101. <dt><code>pagecreate</code></dt>
  102. <dd>
  103. <p>Triggered on the page being initialized, after initialization occurs. We recommend binding to this event instead of DOM ready() because this will work regardless of whether the page is loaded directly or if the content is pulled into another page as part of the Ajax navigation system.</p>
  104. <pre><code>
  105. $('#aboutPage').live('pagecreate',function(event){
  106. alert('This page was just enhanced by jQuery Mobile!');
  107. });
  108. </code></pre>
  109. </dd>
  110. </dl>
  111. <h2>Virtual mouse events</h2>
  112. <p>We provide a set of "virtual" click events that normalize mouse and touch events. This allows the developer to register listeners for the basic mouse events, such as mousedown, mousemove, mouseup, and click, and the plugin will take care of registering the correct listeners behind the scenes to invoke the listener at the fastest possible time for that device. This still retains the order of event firing in the traditional mouse environment, should multiple handlers be registered on the same element for different events.</p>
  113. <dl>
  114. <dt><code>vmouseover</code></dt>
  115. <dd>Normalized event for handling touch or mouse <code>mouseover</code> events</dd>
  116. <dt><code>vmousedown</code></dt>
  117. <dd>Normalized event for handling touch or mouse <code>mousedown</code> events</dd>
  118. <dt><code>vmousemove</code></dt>
  119. <dd>Normalized event for handling touch or mouse <code>mousemove</code> events</dd>
  120. <dt><code>vmouseup</code></dt>
  121. <dd>Normalized event for handling touch or mouse <code>mouseup</code> events</dd>
  122. <dt><code>vclick</code></dt>
  123. <dd>Normalized event for handling touch or mouse <code>click</code> events</dd>
  124. <dt><code>vmousecancel</code></dt>
  125. <dd>Normalized event for handling touch or mouse <code>mousecancel</code> events</dd>
  126. </dl>
  127. <h2>Animation Events</h2>
  128. <p>jQuery Mobile exposes the <code>animationComplete</code> plugin, which you can utilize after adding or removing a class that applies a CSS transition.</p>
  129. </div><!--/content-secondary -->
  130. <div class="content-secondary">
  131. <div data-role="collapsible" data-collapsed="true" data-theme="b">
  132. <h3>More in this section</h3>
  133. <ul data-role="listview" data-theme="c" data-dividertheme="d">
  134. <li data-role="list-divider">API</li>
  135. <li><a href="../../docs/api/globalconfig.html">Configuring defaults</a></li>
  136. <li data-theme="a"><a href="../../docs/api/events.html">Events</a></li>
  137. <li><a href="../../docs/api/methods.html">Methods &amp; Utilities</a></li>
  138. <li><a href="../../docs/api/mediahelpers.html">Responsive Layout</a></li>
  139. <li><a href="../../docs/api/themes.html">Theme framework</a></li>
  140. </ul>
  141. </div>
  142. </div>
  143. </div><!-- /content -->
  144. <div data-role="footer" class="footer-docs" data-theme="c">
  145. <p>&copy; 2011 The jQuery Project</p>
  146. </div>
  147. </div><!-- /page -->
  148. </body>
  149. </html>