将 iPad 从横向旋转为纵向时页面向左移动

发布于 2024-12-09 20:53:52 字数 511 浏览 1 评论 0原文

我正在使用 CSS 媒体查询创建一个具有响应式设计的网站。当我在 iPad 上以横向或纵向打开测试页时,它看起来不错。

但是,当我从横向模式切换到纵向模式时,页面会向左移动。我可以判断正在加载正确的 CSS,因为页面上的其他内容发生了变化。我还可以将页面拖动到右侧,它的显示效果与我最初以纵向打开页面时的效果完全相同。

我的视口设置为: meta id="view" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0"

我添加了 JavaScript 来修复 iOS 视口缩放错误,该错误曾经导致页面从纵向切换到横向时放大。 (我使用了此处描述的解决方案: https://gist.github.com/901295

我从横向切换到纵向时,无法找到我遇到的错误的名称。有其他人看到过这个或知道如何解决吗?

I am using CSS media queries to create a web site with responsive design. When I open my test page on the iPad in either landscape or in portrait orientation, it looks fine.

However, when I switch from landscape to portrait mode, the page is shifted to the left. I can tell that the correct CSS is loading because other things on the page change. I can also drag the page to the right and it appears exactly as it does if I had opened the page in portrait initially.

I have my viewport set to:
meta id="view" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0"

I added JavaScript to fix the iOS viewport scaling bug which used to cause the page to be zoomed in when switching from portrait to landscape. (I used the solution described here: https://gist.github.com/901295 )

I'm having problems finding the name for the bug I'm experiencing when switching from landscape to portrait. Has anyone else seen this or know how to fix?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心清如水 2024-12-16 20:53:52

问题所有者表示,她“还可以将页面拖动到右侧,并且它的显示效果与我最初以纵向打开页面时的效果完全相同。”
这让我认为,由于某种未知的原因(错误?),页面在方向更改为纵向模式时向左滚动(否则您将无法将其拖回)。

我遇到了类似的问题,并使用以下 JavaScript 解决方法解决了它:

// ...
// Optionally add a conditional here to check whether we are in Mobile Safari.
// ...
window.addEventListener('orientationchange', function() {
    if (window.orientation == 0 || window.orientation == 180) {
        // Reset scroll position if in portrait mode.
        window.scrollTo(0, 0);
    }
}, false);

也许这也适用于其他人。

The problem owner says that she "can also drag the page to the right and it appears exactly as it does if I had opened the page in portrait initially."
This makes me think that, for some unknown reason (a bug?), the page is scrolled to the left at an orientation change to portrait mode (otherwise you wouldn't be able to drag it back).

I had a similar issue and solved it with the following JavaScript workaround:

// ...
// Optionally add a conditional here to check whether we are in Mobile Safari.
// ...
window.addEventListener('orientationchange', function() {
    if (window.orientation == 0 || window.orientation == 180) {
        // Reset scroll position if in portrait mode.
        window.scrollTo(0, 0);
    }
}, false);

Maybe this will work for others too.

天煞孤星 2024-12-16 20:53:52

我设法解决了类似的问题 - 也许这对你有用?

您需要通过删除/恢复不同的位并重新测试页面来确定是否是特定的 div 或其他元素导致的。一旦你解决了这个问题,请尝试在 CSS 中的该元素中添加一个 overflow:hidden 属性 - 我使用了 overflow-x:hidden 因为我的问题是水平滚动,但你可能需要改变它。

希望这有用...祝你好运!

I managed to sort my similar issue out - perhaps this will work for you?

You'll need to work out if it's a particular div or other element that's causing it by deleting/reinstating different bits and retesting the page. Once you've worked it out try adding an overflow: hidden property to that element in your CSS - I used overflow-x: hiddensince my issue was horizontal scrolling but you may need to vary it.

Hope this is of use... good luck!

ˉ厌 2024-12-16 20:53:52

Jereon,你的 JavaScript 对我有用。我的视口是:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,          minimum-scale=1, user-scalable=no" />

我正在使用 Drupal Corporate Clean 响应式主题。我使用 Omega 响应式主题框架没有遇到这个问题。

Jereon, your JavaScript worked for me. My viewport is:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,          minimum-scale=1, user-scalable=no" />

I'm using the Drupal Corporate Clean responsive theme. I have not had this problem using the Omega responsive theme framework.

烂柯人 2024-12-16 20:53:52

解决方案是由 @ellawson 提出的。

问题是由于旋转设备时浏览器未正确缩放某些元素造成的。找到该元素并应用溢出:隐藏;或如他所说的overflow-x:hidden;

The solution for this is as proposed by @ellawson

Problem is caused by some element not being scaled correctly by the browser when rotating the device. Find that element and apply overflow: hidden; or overflow-x: hidden; as he says.

抱着落日 2024-12-16 20:53:52

注意:这个问题是重复。我将在这里发布我的答案的要点。

2015 更新

不幸的是,所有其他答案都是不正确的、过时的或误导的。这是有效的:

  window.addEventListener('orientationchange', function () {
    var originalBodyStyle = getComputedStyle(document.body).getPropertyValue('display');
    document.body.style.display='none';
    setTimeout(function () {
      document.body.style.display = originalBodyStyle;
    }, 10);
  });

该代码监听 orientationchange 事件并强制重新通过隐藏 body 元素并在 10 毫秒后显示它来控制其流程。它不依赖于任何标签或媒体查询。

你说,

当我在 iPad 上以横向或纵向打开测试页时,它看起来不错。但是,当我从横向模式切换到纵向模式时,页面会向左移动

这是关键。您只需强制重新绘制 body 即可。

从 Safari 7 开始,建议添加 或其变体的答案不再令人担忧。这是一个演示。为了确保您明白它是如何不起作用的,请从 iPad 的横向模式开始,加载页面,然后旋转。请注意,尽管一直使用 Flexbox,但页面并未扩展到完整高度。

此页面进行比较,我们在生产中使用隐藏/显示正文技术。

Note: this question is a duplicate. I'll post the gist of my answer here.

2015 update

All the other answers are unfortunately incorrect, outdated, or misguided. Here's what works:

  window.addEventListener('orientationchange', function () {
    var originalBodyStyle = getComputedStyle(document.body).getPropertyValue('display');
    document.body.style.display='none';
    setTimeout(function () {
      document.body.style.display = originalBodyStyle;
    }, 10);
  });

The code listens to the orientationchange event and forced a re-flow of the body element by hiding it and showing it 10 miliseconds later. It does not depend on any <meta> tags or media queries.

You said,

When I open my test page on the iPad in either landscape or in portrait orientation, it looks fine. However, when I switch from landscape to portrait mode, the page is shifted to the left

That is key. You just need to force a re-paint of the body.

Answers that suggest adding <meta name="viewport" content="width=device-width, initial-scale=1.0"> or variations thereof, as of Safari 7, no longer wors. Here's a demo. To make sure you see how it doesn't work, start with the iPad in landscape mode, load the page, then rotate. Notice the page doesn't expand to full height, despite using flexbox all the way.

Compare that to this page, where we use the hide/show body technique in production.

甜尕妞 2024-12-16 20:53:52

我在 iPad 上遇到了这个问题,并应用了 html { Overflow-x:hidden; } 。这似乎已经解决了问题。

I came across this problem with an iPad and applied html { overflow-x:hidden; } . That seems to have resolved the issue.

匿名的好友 2024-12-16 20:53:52

尝试将以下设置添加到您的内容属性:maximum-scale=1

或尝试以下操作:user-scalable=no

这里是 ios 文档

try adding the following setting to your content properties: maximum-scale=1

or try this: user-scalable=no

here is the ios documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文