当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


JQuery Mobile orientationchange event用法及代码示例

orientationchange event

添加的版本:1.0

说明:设备纵向/横向事件

  • jQuery( window ).on( "orientationchange", function( event ) { ... } )

    event object 上的其他属性:

    • orientation
      类型:String
      设备的新方向。可能的值为 "portrait""landscape"

jQuery Mobile orientationchange 事件在设备方向改变时触发,无论是垂直还是水平转动设备。当绑定到此事件时,回调函数具有事件对象。事件对象包含一个等于 "portrait""landscape"orientation 属性。

请注意,当本机不支持 orientationchange$.mobile.orientationChangeEnabled 设置为 false 时,我们会绑定到浏览器的调整大小事件。

方向改变时机

orientationchange 事件与客户端高度和宽度变化相关的时间在浏览器之间是不同的,尽管当前的实现将为您提供从 window.orientation 派生的 event.orientation 的正确值。这意味着如果您的绑定依赖于高度和宽度值,您可能希望与 $.mobile.orientationChangeEnabled = false 一起禁用 orientationChange 以让后备调整大小代码触发您的绑定。

例子:

从您的定向设备访问它以查看它的实际效果!

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>orientationchange demo</title>
  <link rel="stylesheet" href="//code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.css">
  <script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="//code.jquery.com/mobile/1.5.0-alpha.1/jquery.mobile-1.5.0-alpha.1.min.js"></script>
</head>
<body>
 
<h1 id="orientation">orientationchange Not Supported on this Device.</h1>
 
<script>
// Bind an event to window.orientationchange that, when the device is turned,
// gets the orientation and displays it to on screen.
$( window ).on( "orientationchange", function( event ) {
  $( "#orientation" ).text( "This device is in " + event.orientation + " mode!" );
});
 
// You can also manually force this event to fire.
$( window ).orientationchange();
</script>
 
</body>
</html>

演示:

相关用法


注:本文由纯净天空筛选整理自jquerymobile.com大神的英文原创作品 orientationchange event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。