简体   繁体   English

如何使用user-scalable = yes在android,iphone中为布局视口保持固定宽度; 使用jQuery Mobile

[英]How do I maintain fixed-width for layout viewport in android, iphone, with user-scalable=yes; using jQuery Mobile

The site should have a fluid-layout, adjusting to the width of the device, in both portrait and landscape orientations. 该位置应具有纵向布局,并根据设备的宽度调整其纵向和横向方向。 Here's my meta viewport tag: 这是我的meta视口标签:

<meta name="viewport" id="viewportid" content="width=device-width, minimum-scale=1.0, maximum-scale=10.0, user-scalable=yes">

The problem is when the user zooms, the content flows to adjust to the visual viewport. 问题是,当用户缩放时,内容会流动以调整为可视视口。 I want to ensure the content remains static. 我想确保内容保持静态。 That is, the layout viewport is unaffected by the visual viewport. 即,布局视口不受视觉视口的影响。

How can I prevent the content from flowing when zooming? 缩放时如何防止内容流动?

Thank you very much for any help. 非常感谢您的帮助。 This has been aggravating as hell. 这真是令人沮丧。

Here is an example of the page zoomed out (scale=1) (Android 2.1 Emulator SDK) 这是缩小页面(比例= 1)的示例(Android 2.1 Emulator SDK) 缩小图像

Here is an example of the same page, zoomed in (Android 2.1 Emulator SDK) 这是放大的同一页面的示例(Android 2.1 Emulator SDK) 放大图像

I tried fiddling around with photoswipe and iScroll4, but I want the user to pinch-zoom and iScroll4 doesn't seem to work with Android Ver <=1.6, according to testing I did on the Android Emulator SDK. 根据我在Android Emulator SDK上进行的测试,我尝试摆弄photowipe和iScroll4,但我希望用户捏一下,并且iScroll4似乎不适用于<= 1.6的Android Ver。 I thought I could try something better suited for my needs. 我以为我可以尝试一些更适合自己需求的东西。

So, AFAIK, and I could be very wrong, there's no way to track the zoom level, per the meta viewport tag, on Android devices. 因此,AFAIK,我可能是非常错误的,无法通过meta视口标签在Android设备上跟踪缩放级别。 jQuery Mobile doesn't have such a feature; jQuery Mobile没有这种功能。 maybe Sencha, Phonegap, or some Android Java libraries facilitate this and I'd be more than happy to be corrected! 也许Sencha,Phonegap或某些Android Java库可以帮助实现这一点,并且我很乐意得到纠正!

Now then, it's not the best method, but I figured out a way that seems to be accurate within 1 or 2 pixels, at worst. 现在,这不是最好的方法,但我想出了一种方法,在最坏的情况下,这种方法似乎在1或2像素内是准确的。 I wasn't sure if Android changes it's zoom level innately on an orientation change event, but I gave my method a shot and it worked. 我不确定Android是否会在方向更改事件上天生改变其缩放级别,但是我给了我一个方法一个机会,它是否有效。

Here's the code: 这是代码:

$(document).ready(function(){
  var ViewportWidth = document.documentElement.clientWidth;
  var LayoutWidth = document.documentElement.clientWidth;

  $("#Index").css("width", LayoutWidth);

  var SetLayoutWidth = function() {
    LayoutWidth = Math.round((1/(ViewportWidth/LayoutWidth)) * document.documentElement.clientWidth);
    $("#Index").css("width", LayoutWidth);
  }

  $(window).resize(function() {
    ViewportWidth = document.documentElement.clientWidth;
  });

  $(window).bind("orientationchange", function(event) {
    if (navigator.userAgent.match(/android/i))
      if (Math.abs(window.orientation) == 90 && event.orientation == "landscape")
        SetLayoutWidth();
      else if (!window.orientation && event.orientation == "portrait")
        SetLayoutWidth();
  });
});

When the user zooms, the visual viewport width changes, whose value is stored. 当用户缩放时,可视视口的宽度会改变,其值将被存储。 The ratio of visual viewport to layout viewport width is, say, 0.8, after the user zooms. 用户缩放后,可视视口与布局视口宽度的比率为0.8。 Now, when the user changes the device's orientation, the ratio is divided by 1. To realize what the layout viewport width should be at full screen, in this example, I need to take 125% of the visual viewport width. 现在,当用户更改设备的方向时,该比率除以1。要实现全屏布局视口宽度,在此示例中,我需要使用可视视口宽度的125%。

The one problem I know that may creep up is the resize event might fire before the orientation change event. 我知道一个可能爬上来的问题是调整大小事件可能会在方向更改事件之前触发。 I haven't seen this occur in testing, but I'll need to determine how to tackle that, if and when it does occur. 我没有在测试中看到这种情况的发生,但是我需要确定如何解决这种情况(如果发生以及何时发生)。

Thank you. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android视口设置“user-scalable = no”会中断视口的宽度/缩放级别 - Android viewport setting “user-scalable=no” breaks width / zoom level of viewport 在Android上使用user-scalable = no的视口最大比例 - viewport maximum-scale with user-scalable=no on android Cordova插件VS2015元视口user-scalable = yes无法正常工作 - Cordova Plugin VS2015 meta viewport user-scalable=yes not working 使用固定宽度视口元标记时,Android会忽略最大比例 - Android ignores maximum-scale when using fixed-width viewport meta-tag 在webview中覆盖视口元数据属性“user-scalable = no”以允许缩放 - Overriding Viewport metadata property “user-scalable=no” in webview to allow zooming 当'user-scalable'设置为yes时,检测夹点缩放 - Detect pinch to zoom when 'user-scalable' is set to yes 如何让我的固定宽度移动网站始终显示“完全放大”? - how can I get my fixed-width mobile website to always appear “fully zoomed in”? 即使我设置user-scalable = no,用户也可以在android 4.3上缩放网站 - User can scale website with pinch on android 4.3 even if I set user-scalable=no 如何等待Android用户准备好元标记“ user-scalable = no”? - How to wait for meta-tag “user-scalable=no” being ready on Android? 尽管在视口meta中设置了user-scalable = no,但仍允许在Web视图中进行捏缩放 - Allow pinch zoom in a webview in spite of user-scalable=no is set in viewport meta
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM