简体   繁体   English

如何使一组图像超出容器div的宽度?

[英]How to make a set of images extend beyond width of container div?

I have a set of images that I would like to be aligned next to one another and extend beyond the width of the container div. 我有一组图像,这些图像要彼此对齐并延伸到容器div的宽度之外。 The images are added dynamically so the width of each image is unknown. 动态添加图像,因此每个图像的宽度都是未知的。 The outer DIV would have a width of something specific ie 790px, and the inner DIV's width should match the width of its contents. 外部DIV的宽度应为790px,内部DIV的宽度应与其内容的宽度匹配。 How would I go about doing this? 我将如何去做呢?

Here's a bit of a quick sketch: 这是一个简单的草图:

+---------------------------------+
|-+---+------+--+-----+----+------|
| |   |      |  |     |    |      |
| |   |      |  |     |    |      |
| |   |      |  |     |    |      |
| |   |      |  |     |    |      |
|-+---+------+--+-----+----+------|
+---------------------------------+
|<   |||||||||                   >|
+---------------------------------+

Here's the code: 这是代码:

<style type="text/css">
<!--
#sortableContainer {
    width: 790px;
    overflow: scroll;
    height:auto;
}
#sortableScroller {
    width: auto;
}
.sortableItem {
    float: left;
    height: 460px;
    padding: 0;
    margin: 0;
}
-->
</style>
<div id="sortableContainer">
    <div id="sortableScroller">
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
        <img src='<url>' class='sortableItem' />
    </div>
</div>

If you don't mind getting rid of the float:left property, I tested the following code in Chrome and it seems to match your requirement: 如果您不介意摆脱float:left属性,则在Chrome中测试了以下代码,它似乎符合您的要求:

<style type="text/css">
<!--
#sortableContainer {
    width: 590px;
    overflow: scroll;
    height:auto;
}
#sortableScroller {
    width: auto;
white-space:nowrap;
}
.sortableItem {
/*  float:left;*/
    height: 460px;
    padding: 0;
    margin: 0;
background-color:black;
width:200px;
display:inline-block;
}
-->
</style>
<div id="sortableContainer">
    <div id="sortableScroller">
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
        <div class="sortableItem"></div>
    </div>
</div>

Properties that have no indention is added by me. 我添加了没有缩进的属性。

I changed the width of the container a little bit to match my screen, and used div{display:inline-block;width:200px;} to simulate the imgs but I think this should work on real images. 我稍微改变了容器的width以匹配我的屏幕,并使用div{display:inline-block;width:200px;}模拟img,但是我认为这应该适用于真实图像。

I added this code and it seems to work, but is there a better way? 我添加了这段代码,它似乎可以正常工作,但是有更好的方法吗?

$(window).load( function() {
    var accum_width = 0;
    $('.sortableItem').each(function() {
        accum_width += $(this).width();
    });
    $('#sortableScroller').width(accum_width);
});

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

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