简体   繁体   English

防止浮动div下降到下一行

[英]Prevent floating divs from dropping to next line

I'm looking to create a viewer composed of an arbitrary number of horizontally-aligned divs where only 3 are visible at any given time. 我正在寻找创建一个由任意数量的水平对齐div组成的查看器,在任何给定时间只有3个可见。

<div id="viewport">
    <div id="slides">
        <div>Content 1</div> <!-- not visible -->
        <div>Content 2</div> <!-- visible -->
        <div>Content 3</div> <!-- visible -->
        <div>Content 4</div> <!-- visible -->
        <div>Content 5</div> <!-- not visible -->
        <div>...</div> <!-- not visible -->
    </div>
</div>

My approach is to have a parent div ("viewport") of fixed width/height and overflow: hidden then to slide its child div ("slides"), which has the actual contents in its child divs, to the left or the right. 我的方法是有一个固定宽度/高度的父div(“viewport”)和溢出:隐藏然后滑动其子div(“幻灯片”),其子div中的实际内容,向左或向右。

In order for this to work, I need the child divs of "slides" to be all horizontally aligned with none of them wrapping below, which will happen by default. 为了使它能够工作,我需要将“幻灯片”的子div全部水平对齐,而不是将它们包裹在下面,默认情况下会发生这种情况。 I'm successful in doing this when I know and specify the cumulative width of the children of the "slides" div in CSS, but I will be dynamically adding/removing them in JS. 当我知道并在CSS中指定“幻灯片”div的子元素的累积宽度时,我成功地执行了此操作,但我将在JS中动态添加/删除它们。 I would like to avoid having to constantly change the width of the "slides" div through JS and would rather just find out how to do it in CSS. 我想避免不断通过JS不断改变“幻灯片”div的宽度,而只是想知道如何在CSS中做到这一点。

So in short, how do I prevent a series of divs from wrapping below if the total width is unknown? 简而言之,如果总宽度未知,如何防止一系列div包裹在下面?

The trick is to set #slides to a sufficiently great width that you will never have to worry about it, and then chop it to your desired width using your #viewport div, as demonstrated in this fiddle . 诀窍是将#slides设置为足够大的宽度,您永远不必担心它,然后使用#viewport div将其#viewport所需的宽度,如此小提琴所示 By simply adjusting the left value of #slides , you can move your strip of divs left and right. 只需调整#slidesleft值,就可以左右移动div条。

The CSS: CSS:

#viewport {
    width:300px;
    overflow:hidden;
}

#slides {
    width:1000px;
    position:relative;
    left:-150px;
}

#slides div {
    width:100px;
    height:100px;
    float:left;
    border:1px solid black;
}​

Your HTML remains unchanged. 您的HTML保持不变。

I think that http://jsfiddle.net/5JHW5/2/ is what you're wanting. 我认为http://jsfiddle.net/5JHW5/2/就是你想要的。 It uses jQuery to figure what the width of #slides is and sets its width appropriately. 它使用jQuery来计算#slides的宽度并适当地设置其宽度。 I also added in some controls for scrolling, just because I like doing stuff like that. 我还添加了一些滚动控件,只是因为我喜欢做那样的事情。 If you need to see more in the example I gave let me know. 如果你需要在示例中看到更多,我告诉我。

Cheers! 干杯!

is this an example of what you want. 这是你想要的一个例子。

<div class="box">
<div class="div1">1st</div>
<div class="div2">2nd</div>
<div class="div3">3nd</div>
<div class="clear">
</div>

CSS CSS

div.box { background: #EEE; height: 100px; width: 600px; }
div.div1{background: #999; float: left; height: 100%; width: auto; }
div.div2{ background: #666; float: left;height: 100%; width: auto;height: 100%; }
div.div3{ background: green; height: 100%; }
div.clear { clear: both; height: 1px; overflow: hidden; font-size:0pt; margin-top: -1px; }

If you set display: none on the slides that aren't supposed to be visible, they won't take up any space, and there's no need for a container div any larger than needed to hold your three visible slides. 如果在不应该看到的幻灯片上设置display: none ,它们将不会占用任何空间,并且不需要任何大于容纳三个可见幻灯片所需的容器div。 I've added a shown class to three slides to distinguish which ones are visible (you could toggle this in javascript). 我已经在三张幻灯片中添加了一个shown类来区分哪些是可见的(你可以在javascript中切换它)。 Setting float=left on div#slides causes it to take up just enough space to fit its children. div#slides上设置float=left会导致它占用足够的空间来容纳其子项。

<div id="viewport">
  <div id="slides">
    <div>Content 1</div> <!-- not visible -->
    <div class="shown">Content 2</div> <!-- visible -->
    <div class="shown">Content 3</div> <!-- visible -->
    <div class="shown">Content 4</div> <!-- visible -->
    <div>Content 5</div> <!-- not visible -->
  </div>
</div>

The CSS: CSS:

div#slides {
    float: left;
}

div#slides > div {
    float: left;
    width: 10em;
    height: 10em;
    margin: 1em;
    background-color: red;
    display: none;
}

div#slides > div.shown {
    display: block;
}

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

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