热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

溢出:在ios6中被隐藏在身体上-overflow:hiddenonbodyisbrokeninios6

IhavedonesometestingandfromwhatIcanseethereisabuginmobileSafarionios6.我做了一些测试,从我

I have done some testing and from what I can see there is a bug in mobile Safari on ios6.

我做了一些测试,从我可以看到ios6上的移动Safari中存在一个错误。

When adding overflow:hidden on the body tag and moving an element out of the body using transform:translateX(100%); It creates an extra scrollable space for that element. On all desktop browsers it is "hidden".

添加溢出时:隐藏在body标签上并使用transform:translateX(100%)将元素移出正文;它为该元素创建了一个额外的可滚动空间。在所有桌面浏览器上,它都是“隐藏”的。

Here is a demo: http://jsfiddle.net/mUB5d/1 . Open that in Mobile safari and you will see what is wrong.

这是一个演示:http://jsfiddle.net/mUB5d/1。在Mobile safari中打开它,你会看到有什么问题。

Could anyone take a look at safari 6 on Mac OS to see if the bug is present there too? Does anybody know of any workaround besides creating another parent around my element?

任何人都可以在Mac OS上查看safari 6,看看那里是否还有bug?除了在我的元素周围创建另一个父级之外,有没有人知道任何解决方法?

Thanks for your feedback!

感谢您的反馈意见!

4 个解决方案

#1


9  

Nope. Safari 6 on Mac does not present with the bug. Scrollbars are not present.

不。 Mac上的Safari 6不会出现错误。滚动条不存在。

I ran it on OSX Mountain Lion (10.8.2)

我在OSX Mountain Lion上运行它(10.8.2)

enter image description here

To further answer your question, the reason this is happening probably has more to do with Mobile Safari's zoom rendering than an overflow hidden bug. The element is in fact being hidden off screen (notice below where I have scrolled over to the right all the way, it still doesn't show me the full 100% width element - 90% of it is in fact being hidden.

为了进一步回答你的问题,发生这种情况的原因可能与Mobile Safari的缩放渲染有关,而不是溢出隐藏的bug。该元素实际上是隐藏在屏幕之外(注意下面我一直向右滚动的地方,它仍然没有显示完整的100%宽度元素 - 其中90%实际上是隐藏的。

It likely has something to do with iframes, and page zoom. Still looks like a bug though.

它可能与iframe和页面缩放有关。尽管如此仍然看起来像一个bug。

I'm assuming you're demonstrating in JSFiddle from a real life example. If you go back to your real life example (apart from iframe territory), try adding this meta tag to the head if you don't already have it, and see it this helps:

我假设你是从一个真实的例子中在JSFiddle中展示的。如果你回到现实生活中的例子(除了iframe领域),尝试将这个元标记添加到头部,如果你还没有它,并看到它有帮助:

enter image description here

#2


7  

This is normal behaviour on iOS (and iOS only). You can work around it by declaring overflow: hidden on both html and body element. In addition, you should set the body to position: relative.

这是iOS(仅限iOS)上的正常行为。您可以通过声明溢出来解决它:隐藏在html和body元素上。此外,您应该将主体设置为position:relative。

Overflow behaviour

There are several things at play here. To understand why the fix works, we first need to have a look at how the overflow of the viewport is set.

这里有几件事情在玩。要了解修复工作的原因,我们首先需要了解视口的溢出是如何设置的。

  • The overflow of the viewport is determined by the overflow setting of the html element.
  • 视口的溢出由html元素的溢出设置决定。
  • But as long as you leave the overflow of the html element at its default (visible), the overflow setting of the body gets applied to the viewport, too. Ie, you can set either html or body to overflow: hidden when you target the viewport. The overflow behaviour of the body element itself is unaffected - so far.
  • 但只要您将html元素的溢出保留为默认值(可见),主体的溢出设置也会应用于视口。即,您可以将html或body设置为overflow:当您定位视口时隐藏。到目前为止,身体元素本身的溢出行为不受影响。
  • Now, if you set the overflow of the htmlelement to anything other than visible, the transfer from body to viewport does no longer happen. In your particular case, if you set both overflows to hidden, the setting of the html element gets applied to the viewport, and the body element hides its overflow as well.
  • 现在,如果将htmlelement的溢出设置为除可见之外的任何其他内容,则不会再发生从body到viewport的转移。在您的特定情况下,如果将两个溢出都设置为隐藏,则html元素的设置将应用于视口,并且body元素也会隐藏其溢出。

That's actually the case in every reasonably modern browser out there, and not specific to iOS.

实际情况就是每个合理的现代浏览器,而不是iOS特有的。

iOS quirks

Now, iOS ignores overflow: hidden on the viewport. The browser reserves the right to show the content as a whole, no matter what you declare in the CSS. This is intentional and not a bug, and continues to be the case in iOS 7 and 8. There is nothing anyone can do about it, either - it can't be turned off.

现在,iOS忽略了溢出:隐藏在视口上。无论您在CSS中声明什么,浏览器都保留整体显示内容的权利。这是有意的,而不是一个错误,并且在iOS 7和8中仍然如此。任何人都无法做到这一点 - 它无法关闭。

But you can work around it by making the body element itself, not the viewport, hide its overflow. To make it happen, you must first set the overflow of the html element to anything other than visible, e.g. to auto or hidden (in iOS, there is no difference between the two). That way, the body overflow setting doesn't get transferred to the viewport and actually sticks to the body element when you set it to overflow: hidden.

但是你可以通过使body元素本身而不是视口来隐藏它的溢出来解决它。要实现这一点,您必须首先将html元素的溢出设置为除可见之外的任何内容,例如自动或隐藏(在iOS中,两者之间没有区别)。这样,当您将其设置为overflow:hidden时,正文溢出设置不会传递到视口并实际粘贴到body元素。

With that in place, most content is hidden. But there still is an exception: elements which are positioned absolutely. Their ultimate offset parent is the viewport, not the body. If they are positioned somewhere off screen, to the right or to the bottom, you can still scroll to them. To guard against that, you can simply set the body element to position: relative, which makes it the offset parent of positioned content and prevents those elements from breaking out of the body box.

有了这个,大多数内容都被隐藏了。但仍有一个例外:绝对定位的元素。他们最终的偏移父母是视口,而不是身体。如果它们位于屏幕之外,右侧或底部的某个位置,您仍然可以滚动到它们。为了防范这种情况,您可以简单地将body元素设置为position:relative,这使得它成为定位内容的偏移父元素,并防止这些元素突破正文框。

Answering in code

There is one final gotcha to watch out for: the body itself must not be larger than the viewport.

最后需要注意的是:身体本身不得大于视口。

So the body needs to be set to 100% of the viewport width and height. (The credit for a CSS-only way to achieve it goes to this SO answer.) Margins on the html and body elements have to be 0, and the html must not have padding or a border, either.

因此,需要将主体设置为视口宽度和高度的100%。 (实现它的唯一方法的功劳归功于这个SO答案。)html和body元素的边距必须为0,并且html也不能有填充或边框。

Finally, in order to deal with body padding, and in case you ever want to set a border on the body, make the math work with box-sizing: border-box for the body.

最后,为了处理身体填充,并且如果您想要在身体上设置边框,请使用box-sizing:body-border为身体进行数学运算。

So here goes.

所以这里。

html {
  overflow: hidden;

  height: 100%;
  margin: 0;
  padding: 0;
  border: none;
}

body {
  overflow: hidden;
  position: relative;

  box-sizing: border-box;
  margin: 0;
  height: 100%;
}

NB You can set body padding and border as you please.

注意您可以根据需要设置身体填充和边框。

#3


1  

After struggling with this for a while I've found that both html and body tags need overflow hidden to actually hide the overflowing contents. On elements inside body overflow hidden works fine, so our choice is an extra css rule or a wrapper element.

经过一段时间的努力,我发现html和body标签都需要隐藏溢出来实际隐藏溢出的内容。在body body里面的元素溢出隐藏的工作正常,所以我们选择的是额外的css规则或包装元素。

#4


0  

for me it works

对我来说它有效

I have implemented in the left side menu

我在左侧菜单中实现了

if($('.left-menu-panel').is(':visible')) {$("body").addClass('left-menu-open');$("html").css('overflow-y','hidden'); $('body').click(function() {$("body").removeClass("left-menu-open") ;$("html").css('overflow-y','visible'); });$('#off-canvas-left').click(function(event){event.stopPropagation();}); }

推荐阅读
author-avatar
赵春柱_626
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有