CSS水平滚动问题

时间:2011-02-03 10:43:20

标签: css horizontal-scrolling

问题

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兼容模式(不幸的是它必须兼容),它具有以下外观:

enter image description here

使用IE hack:

enter image description here

2 个答案:

答案 0 :(得分:8)

在您的folderBox而不是inline-block

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

空格:无包装对浮动元素不起作用,它适用于内联元素。

对于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 compat模式(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,现在它可以工作了。

答案 1 :(得分: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。

相关问题