简体   繁体   English

``overflow-x:hidden''内部的元素仍然具有宽度(Safari)

[英]Elements inside `overflow-x: hidden` still have width (Safari)

The following code demonstrates an element, p , that collapses and expands horizontally with CSS3 (actually webkit) transitions: 以下代码演示了元素p ,该元素通过CSS3(实际上是webkit)过渡进行折叠和水平扩展:

<style>
  div * {
    display: inline-block;
  }

  p {
    overflow-x: hidden;
    white-space: nowrap;
    -webkit-transition: width 1s;
  }​
</style>

<div>
  <a><i>begin</i><p><s>middle</s></p><b>end</b></a>
  <a id="after">after</a>
</div>

<script>
  $('div > a').each(function() {
    var a, p;
    a = $(this);
    p = $('p', a);
    a.toggle((function() {
      p.width(0);
    }), function() {
      p.width(p[0].scrollWidth);
    });
  });
</script>

http://jsfiddle.net/qrf3E/ http://jsfiddle.net/qrf3E/

It has the desired effect when using Chrome: http://www.screenr.com/vJH7 使用Chrome时,它具有预期的效果: http : //www.screenr.com/vJH7

But in Safari (desktop and mobile) the apparent width of the container element, a , snaps back to the expanded width when the inner container, p , has collapsed. 但是在Safari(台式机和移动设备)中,当内部容器p折叠时,容器元素a的视在宽度会恢复到扩展宽度。 This forces the following element, #after , to float out in space... Help! 这将迫使以下元素#after漂浮在太空中……帮助! Is this a Safari bug? 这是Safari错误吗? Can anyone offer a fix? 谁能提供修复程序?

http://www.screenr.com/qJH7 http://www.screenr.com/qJH7

One possible fix that has just occurred to me would be to collapse to a single pixel and then hide: 我刚刚想到的一种可能的解决方法是将其折叠成单个像素然后隐藏:

a.toggle(function() {
  p.one('webkitTransitionEnd', function() {
    p.hide();
  });
  p.width(1);
}, function() {
  p.show();
  p.width(p[0].scrollWidth);
});

http://jsfiddle.net/corin/LsSk2/ http://jsfiddle.net/corin/LsSk2/

Can anyone better that? 谁能更好?

Well, this seems to be a forgotten default value by the safari developers. 好吧,这似乎是野生动物园开发人员忘记的默认值。

If you set a { display: inline; } 如果您设置a { display: inline; } a { display: inline; } it works. a { display: inline; }它起作用。 http://jsfiddle.net/2t388/ http://jsfiddle.net/2t388/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM