CSS水平滚动问题

6

问题

enter image description here enter image description here

我需要让内容在X轴上滚动,而不是在Y轴上下滚动。

HTML

我知道这个格式很糟糕,但它是动态生成的,没有空格。

<div class="folderWrapper" id="folderContainer"><div class="folderBox ui-droppable folderBoxSel" onclick="folderSelected(0)" id="fBox0"><div class="counter" id="fCount0">4</div><div class="folderName">Unsorted</div></div><div class="folderBox ui-droppable" onclick="folderSelected(1)" id="fBox1"><div class="counter" id="fCount1">0</div><div class="folderName">test</div></div><div class="folderBox" onclick="folderSelected(1)" id="fBox1"><div class="counter" id="fCount1">0</div><div class="folderName">test</div></div><div class="folderBox" onclick="folderSelected(1)" id="fBox1"><div class="counter" id="fCount1">0</div><div class="folderName">test</div></div><div class="folderBox" onclick="folderSelected(1)" id="fBox1"><div class="counter" id="fCount1">0</div><div class="folderName">test</div></div><div class="folderBox" onclick="folderSelected(1)" id="fBox1"><div class="counter" id="fCount1">0</div><div class="folderName">test</div></div></div>

使用一个文件夹内框架,格式化得很好:

<div class="folderWrapper" id="folderContainer">
    <div class="folderBox ui-droppable folderBoxSel" onclick="folderSelected(0)" id="fBox0">
        <div class="counter" id="fCount0">4</div>
        <div class="folderName">Unsorted</div>
    </div>
</div>

CSS

.folderWrapper{
    overflow-x:scroll;
    overflow-y:hidden;
    height:85px;
    white-space:nowrap;
    margin-bottom:5px;
}
.folderBox{
    float:left;
    background-image:url(../images/artworking/folder.png);
    background-position:center top;
    background-repeat:no-repeat;
    width:85px;
    height:55px;  
    position:relative;
    padding:5px;
    z-index:4;
    white-space:nowrap;
}
.folderBox:hover{
    cursor: pointer;
}

如果有人能帮忙,感谢!

编辑

Bazzz的答案在除IE兼容模式外的所有浏览器中都有效(不幸的是它必须兼容),在IE兼容模式下会出现以下情况:

enter image description here

使用IE hack后:

enter image description here

2个回答

8

在您的文件夹框中使用inline-block而不是float:left

.folderBox{
    float: left; /* remove this line */
    display: inline-block; /* add this line */
}

在浮动元素上,whitespace: no-wrap不起作用,它只对内联元素起作用。

对于IE 7,我发现一个小技巧可以帮助你:

http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/

尝试使用以下CSS:

.folderBox{
 display: inline-block;
 zoom: 1;
 *display: inline;
 }

最近一次编辑:

这个CSS在IE 8兼容模式(IE7标准)下可以使用:

.folderWrapper{
    overflow-x: scroll;
    overflow-y: hidden;
    height:85px;
    width: 300px; /* not really your css, I used it in my test case */
    white-space:nowrap;
}
.folderBox{
    display: inline-block;
    zoom: 1;
    *display: inline;
    background-image:url(../images/artworking/folder.png);
    background-position:center top;
    background-repeat:no-repeat;
    width:85px;
    height:55px;  
}

我认为IE7中的非溢出问题在于您使用的position:relative。我将其删除以及其他一些CSS,现在它可以正常工作。


抱歉,Baz不得不将其删除,在IE兼容模式下,它显示得非常奇怪,我会更新问题以显示。 - Tom Gullen
IE 8 及以下版本不太支持 inline-block。如果您需要在 IE 7 及更早版本中使用,可以使用 float:left,但是您必须为 folderWrapper 定义一个宽度,以便浮动元素不会移到下一行。如果文件夹数量不是静态的,则提供此宽度并不容易。或者,您可以将 folderBox 设为 span 而不是 div,我不确定这是否会产生正确的结果,但值得一试。 - Bazzz
请注意,IE 7及更早版本不太支持overflow-xoverflow-y。我相信overflow是支持的,但这可能会导致水平和垂直滚动条同时出现。 - Bazzz
谢谢,这个hack有点用,但它没有水平隐藏内容,所有的文件夹都显示在滚动区域之外。但是你给了我其他一些可以使用的东西,谢谢。该死的IE浪费了我们太多的时间。 - Tom Gullen
哈哈哈,我确实会投票支持那个通过法律废除IE7(特别是IE6)的人。你能否更新你的问题并附上一些新的截图或者你现有的代码?我感觉我们离一个可行的解决方案很近了,而且我也很想找到适用于IE7的答案。 - Bazzz
显示剩余2条评论

0
我会创建类似以下的HTML代码:
<div id="folderWrapper">
     <ul id="folderList">
         <li class="folderBox">...</li>
         <li class="folderBox">...</li>
         <li class="folderBox">...</li>
     </ul>
</div>

以及CSS:

#folderWrapper {
    position:relative;
    z-index:1;
    width:300px;
    overflow:hidden;
}
#folderList {
    position:absolute;
    z-index:2;
    width:20000px;
}
.folderBox {
    float:left;
}

然后在 #folderWrapper 中使用基于 jQuery 的滚动条: http://www.net-kit.com/jquery-custom-scrollbar-plugins/

我喜欢 jScrollPane。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接