简体   繁体   English

使用overflow-x进行水平滚动

[英]Horizontal scrolling with overflow-x

I was wondering if there still isn't a good way to get a div to stretch and scroll horizontally, according to the images inside. 根据里面的图像,我想知道是否仍然没有一个好的方法让div伸展和水平滚动。 After all, it is 2011 now! 毕竟,现在是2011年!

The mega-width does the trick, but leaves an empty mega-space if not filled with images. mega-width可以解决问题,但如果没有填充图像,则留下空的mega-space
If filled too much, images don't display. 如果填充太多,则不显示图像。 Same for the jQuery. 对于jQuery也一样。 The situation below is the best I can do after hours of Googling, but it isn't reliable enough. 以下情况是我在谷歌搜索后可以做的最好的情况,但它不够可靠。

Thanks for your time. 谢谢你的时间。


* {
  margin:0;
  padding:0;
}

#one {
  width: 200px;    
  height: 250px;
  overflow-x: auto;
  overflow-y: hidden;
}

#two {
  width: 10000em;
}

#two img {
  float: left;
}

$(document).ready(function() {
  var total = 0;
  $(".calc").each(function() {
    total += $(this).outerWidth(true);
  });
  $("#two").css('width', total);
  alert(total);
});
<div id="one">
  <div id="two">
    <img src="images/testimage3.jpg" width="480" height="192" class="calc">
    <img src="images/testimage3.jpg" width="480" height="192" class="calc">
    <img src="images/testimage3.jpg" width="480" height="192" class="calc">
    <img src="images/testimage3.jpg" width="480" height="192" class="calc">
  </div>
</div>

It's easy. 这很简单。 display:inline-block on the images and white-space:nowrap on the parent. display:inline-block图像上的display:inline-blockwhite-space:nowrap父对象上的white-space:nowrap

Edit: I forgot that images are by default inline, anyway :) That means that white-space:nowrap is all you need. 编辑:我忘了图像默认是内联的,无论如何:)这意味着white-space:nowrap就是你所需要的。

div { 
    overflow-x:scroll;
    overflow-y:hidden;
    white-space:nowrap;
}

Live demo: http://jsfiddle.net/U4RsQ/2/ 现场演示: http //jsfiddle.net/U4RsQ/2/

See Make CSS Div Width Equal To Contents , I think, for stretching. 请参阅Make CSS Div Width Equal To Contents ,我认为,用于拉伸。 Then apply a max-width to constrain width to not be super-huge. 然后应用max-width来约束宽度,使其不会超大。 (And overflow: scroll if necessary.) (并overflow: scroll必要时overflow: scroll 。)

What worked for me with IE8: IE8对我有用:

msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx msdn.microsoft.com/en-us/library/cc351024(v=vs.85).aspx

You can ensure your page displays with the latest rendering by including the following meta tag in your page's head section: 您可以通过在页面的head部分中包含以下元标记来确保您的页面显示最新的渲染:

<meta http-equiv="X-UA-Compatible" content="IE=9" />

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

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