5

在这个例子中http://bit.ly/t2ImYS所有元素的包装宽度是固定的8520px

#scroller {
width: 8520px;
height: 100%;
float: left;
padding: 0;}

我想要width动态的,所以如果我在<div id="scroller">这个#scroller 中添加更多元素,应该在其中的元素上采用宽度。

所以尝试设置宽度

#scroller {
    width: 100%;}

 #scroller {
    width: auto}

但随后滚动条无法正常工作。

width有没有办法让%滚动正常工作?

4

5 回答 5

11
  • 将 li 元素设置为 display:inline-block; 并删除浮动:左;(您也可以删除垂直对齐,因为这只适用于表格单元格元素)

  • 从包装器中删除固定宽度。

  • 添加空格:nowrap;到 ul

  • 你应该没事...

(除了<=ie7,但我想你的情况没问题?)

#scroller li {
    display: inline-block;/* changed */
    /*float:left; */   /* deleted */
    padding: 0 10px;
    width: 120px;
    height: 100%;
    border-left: 1px solid #CCC;
    border-right: 1px solid white;
    background-color: #FAFAFA;
    font-size: 14px;
}
#scroller ul {
    list-style: none;
    display: block;
    float: left;
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    text-align: left;
    white-space:nowrap; /* added */
}
#scroller {
    /* width: 8520px; */   /* deleted */
    height: 100%;
    float: left;
    padding: 0;
}
于 2011-12-12T09:35:42.167 回答
2

如果您使用的是 iScroll4,您应该刷新滚动条或销毁并重新创建它。

从这里摘录:“iScroll 需要知道包装器和滚动器的正确尺寸。它们是在启动时第一次计算的,但是如果您的代码更改了元素大小,则需要警告 iScroll 您正在弄乱 DOM 。”

于 2012-01-24T09:24:11.570 回答
2

使用 Display:inline-block 和使用百分比对我有用:

#scroller li {
 height: 100%;
 width: 2%;
 display: inline-block;
}

#scroller ul {
 list-style: none;
 display: block;
 float: left;
 width: 100%;
 height: 100%;
}

#scroller {
 width: 5000%;
 height: 100%;
 float: left;
}
于 2012-12-03T16:28:17.953 回答
1

refresh()在滚动条中添加动态项以设置宽度后,尝试调用 iscroll方法。

于 2012-05-23T05:57:54.697 回答
0

试试这个css代码,它对我有用:http://jsfiddle.net/manseuk/r9VL2/2/

#wrapper {
                 z-index:1;

                width:100%;
                background:#aaa;
                overflow:auto;
            }

            #scroller {
               z-index:1;
            /*    -webkit-touch-callout:none;*/
                -webkit-tap-highlight-color:rgba(0,0,0,0);
                width:100%;
                padding:0;
            }

            #scroller ul {
                list-style:none;
                padding:0;
                margin:0;
                width:100%;
                text-align:left;
            }

            #scroller li 
            {
                background-color: White !important;
                padding:0 10px;
                height:40px;
                line-height:40px;
                border-bottom:1px solid #ccc;
                border-top:1px solid #fff;
                background-color:#fafafa;
                font-size:14px;
            }
于 2013-05-12T21:41:28.810 回答