jQuery Mobile - 方向事件



簡介

使用 `orientationchange` 事件,當用戶垂直或水平旋轉裝置時,你可以觸發方向事件。它使用 `window.orientation` 屬性來指定視窗或裝置是否設定為垂直或水平方向。

示例

以下示例介紹了在 jQuery Mobile 框架中使用 `orientation event`。

<!DOCTYPE html>
   <head>
      <title>Orientation Event</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
      <script src = "https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
      
      <script>
         $(document).on("pagecreate","#page1",function(event) {
            $(window).on("orientationchange",function() {
               if(window.orientation == 0) {
                  $("p").text("portrait orientation!!!");
               } else {
                  $("p").text("landscape orientation!!!");
               }
            });
         });
      </script>
   </head>
   
   <body>
      <div data-role = "page" id = "page1">
         <div data-role = "header">
            <h2>Header</h2>
         </div>

         <div data-role = "main" class = "ui-content">
            <p>To see the orientation effect, you must rotate your window...</p>
         </div>

         <div data-role = "footer">
            <h2>Footer</h2>
         </div>
      
      </div>
   </body>
</html>

輸出

讓我們執行以下步驟來檢視上面程式碼如何工作 -

  • 將上面 html 程式碼另存為伺服器根資料夾中的 `jqm_orientation_event.html` 檔案。

  • 將此 HTML 檔案開啟為 `https:///jqm_orientation_event.html`,將顯示以下輸出。

jquery_mobile_events.htm
廣告