简体   繁体   中英

Align DIVs horizontally within a scrolling div

I'm working on a photography website. One of the things we're trying to aim for is a 'film strip' type of display for the images, as opposed to the usual thumbnail or 'tabulated' formation.

It works with tables. No problemo. The only thing that makes me not want to use a table is the fact that I'm not showing data, there's no need for columns and rows.

Another thing that is a slight spanner in the gears is the fact that I'm putting the images as backgrounds of divs. This is for basic 'copy protection', and also so I can overlay items over the photo on hover of the div.

The way I've got it coded at the moment is:

container [
  [image]
  [image]
  [image]
  [image]
]

I've drawn a skitch to help out with the visualisation of this..

替代文字

As soon as the width of the container is met, the image-divs are dropping to the next line. The CSS for the Divs is as follows:

.gallery_block_image_p {
width: 354px;
height: 532px;
display: inline-block;
margin: 0px;
padding: 0px;
margin-left: 10px;
float: left;
background-repeat: no-repeat;
}

and for the container...

#gallery {
    border: 0px solid black;
    position: relative;
    top: 99px;
/*  width: 8000px;  */ /* When this is uncommented it works, with a huge amount of space to the right */
    height: 532px;
    z-index: 99;
    }

and last but not least, the HTML used for the image divs...

<div id="gallery_1_0_img" class="gallery_block_image_p" style="background-image: url(gallery_img/ith/adamd_20101021_137.jpg);"></div>

if you remove "float:left;" from the gallery block style and add "white-space:nowrap" to the container then it should work.

Edit: I think something like this is what you're looking for

<div style="width: 800px; overflow-x:auto; white-space: nowrap;">
    <div style="width: 300px; height: 100px; background-color: #f00; display: inline-block;"></div>
    <div style="width: 300px; height: 100px; background-color: #0f0; display: inline-block;"></div>
    <div style="width: 300px; height: 100px; background-color: #00f; display: inline-block;"></div>
    <div style="width: 300px; height: 100px; background-color: #ff0; display: inline-block;"></div>
</div>

Try specifying the width of 800 and adding an overflow declaration:

#gallery {
   border: 0px solid black;
   position: relative;
   top: 99px;
   width: 800px;
   height: 532px;
   z-index: 99;
   overflow:auto;
}

try using the overflow property for the container. so something like this:

#gallery {
overflow-x: scroll;
overflow-y: hidden;
}

here are some examples http://www.brunildo.org/test/Overflowxy2.html

I think you might need to define the width of your gallery! see fiddle I have added the view to hold it all, but like you seemed to find there was no way of forcing a line, might be able to do something with positioning. Alternatively declare the width at the top of the page with the server side logic instead of the javascript on the fiddle

Not tested, but could you use the

white-space:nowrap;

css property to stop the divs from wrapping when you specify the width?

I came up with a bit of a hacky solution, the only downside of which, you need to know the width of the scrolling gallery. I'm sure that's pretty easy to predetermine or calculate. Below is the code and here is an online demo .

Some cheeky jQuery will allow you to calculate it all on the fly if results are dynamic.

<style type="text/css">
#gallery {
    border: 0px solid black;
    position: relative;
    width:500px;
    height: 450px;
    overflow:scroll;
    overflow-y:hidden;
    z-index: 99;
}

.gallery_block_image_p {
    width: 354px;
    height: 400px;
    margin: 0 0 0 10px;
    padding: 0;
    background-repeat: no-repeat;
    display:inline-block;
}
#stretch{
    width:1850px;
}
</style>

<div id="gallery">
    <div id="stretch">
        <div id="gallery_1_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
        <div id="gallery_1_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
        <div id="gallery_2_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
        <div id="gallery_3_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
        <div id="gallery_4_0_img" class="gallery_block_image_p" style="background-image: url(http://blogs.westword.com/demver/kitten.JPG);"></div>
    </div>
</div>

I have done some thing very similar with a site and was challenged by this as the user would be adding / removing divs on his own. My solution for this was to use jQuery to count each item/div within the container and set the width of the container based on items within the container.

jQuery:

   $('.gallery-item').each(function(scroll){ n = n+310; });
    $('#gallery').css( "width", n);
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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