jquery - 评估orientationEvent并设置一个值?

标签 jquery orientation screen-orientation device-orientation

如果在方向发生变化时触发 onChange 函数,如何在 onChange 中设置一个值来更新 jquery 选择器。例如:

  $(document).ready(function(){    
    var onChanged = function() {
            if(window.orientation == 90 || window.orientation == -90){
                image = '<img src="images/land_100.png">';
            }else{
                image = '<img src="images/port_100.png">';
            }
     }
        $(window).bind(orientationEvent, onChanged).bind('load', onChanged);
        $('#bgImage').html(image); //won't update image
   });

最佳答案

您需要将对图像的更新放入 onChanged 函数中,以便每次方向更改时,图像 HTML 都会更改。

$(document).ready(function(){   

   // The event for orientation change
   var onChanged = function() {

      // The orientation
      var orientation = window.orientation,

      // If landscape, then use "land" otherwise use "port"
      image = orientation == 90 || orientation == -90 ? "land" : "port";

      // Insert the image
      $('#bgImage').html('<img src="images/'+image+'_100.png">');

   };

   // Bind the orientation change event and bind onLoad
   $(window).bind(orientationEvent, onChanged).bind('load', onChanged);

});

关于jquery - 评估orientationEvent并设置一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5195247/

相关文章:

javascript - 使用基于ajax的客户端上传PHP文件不起作用

ios - 在 Swift 中,如何在启动后立即正确获取设备方向?

Android:自动旋转在 setRequestedOrientation 后不起作用

ios - UIImagePickerController 在缩放时显示黑条 – 这是 iOS 中的错误吗?

javascript - jQuery Ajax PHP 问题

javascript - 使用进度 slider 更改工具提示

javascript - 获取数组内的元素并与数字进行比较

Android 应用程序在方向更改时崩溃 java.lang.IllegalArgumentException : Wrong state class -- expecting View State

android自动测试方向改变

android - Android 设备旋转时如何保留警报对话框