【问题标题】:Dynamically assign height to jquery mobile page based on device基于设备为jquery移动页面动态分配高度
【发布时间】:2013-10-25 14:02:47
【问题描述】:

我是 jquery mobile 的新手。我正在编写一个移动应用程序,我希望它能够动态适应 data-role="page" 的内容 到所有设备上的屏幕尺寸。 什么是有效的方法? 我读到了必须在标题中设置的视口。它在调整页面高度和宽度时起什么作用?

谢谢。

【问题讨论】:

标签: jquery jquery-mobile


【解决方案1】:

这是一个 jsFiddle:http://jsfiddle.net/ezanker/Tx4kF/

在 Ved 提到的元视口之后,使用 javascript 计算内容窗格的可用高度:

function ScaleContentToDevice() {
   scroll(0, 0);
   var headerHeight = $("#jqmHeader:visible").outerHeight();
   var footerHeight = $("#jqmFooter:visible").outerHeight();
   var viewportHeight = $(window).height();

   var content = $("#jqmContent:visible");
   var contentMargins =  content.outerHeight() - content.height();

   var contentheight = viewportHeight - headerHeight - footerHeight - contentMargins;

   content.height(contentheight);
}

这假设您在 body/html 上没有边距或内边距:

html, body {
   margin: 0;
   padding : 0;
}

对 pageshow 事件执行缩放以及方向更改/调整大小。

$(document).on("pageshow", function(){
   ScaleContentToDevice();
});
$(window).on('resize orientationchange', function(){ ScaleContentToDevice() });

【讨论】:

  • 视口描述说,在标题中设置视口会自动处理高度和宽度。但它不起作用。那么它的实际意义是什么?
  • 元标记不处理设置 HTML 元素高度以填充屏幕,而是帮助确定页面在移动设备上的外观:应该缩小以适应,还是应该仅部分可见等。有关更多信息,请参阅javascriptkit.com/dhtmltutors/cssmediaqueries3.shtml。我的回答假设你试图让内容 div 完全填充页面的可用高度,也许这不是你想要的......
  • 对我来说效果很好。但这在 iPad 上不起作用。如果我在这种情况下更改方向,它会显示放大的页面并且不适合页面。
  • 你能看到orientationchange事件是否触发了吗?或者添加警报以调试它是否正在触发以及您获得的屏幕尺寸值......
  • 事件被触发。缩放在 android 设备上运行良好,上述问题仅适用于 iO。
猜你喜欢
  • 2012-02-14
  • 1970-01-01
  • 2020-01-04
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
相关资源
最近更新 更多