如何在 iScroll 中设置滚动条的动态宽度?
在此示例中 http://bit.ly/t2ImYS 所有元素的包装宽度都是固定的 8520px
#scroller {
width: 8520px;
height: 100%;
float: left;
padding: 0;}
我想要width
动态,因此如果我在
内添加更多元素,则此 #scroller 应采用其中元素的宽度。因此尝试设置宽度
#scroller {
width: 100%;}
,
#scroller {
width: auto}
但滚动器无法正常工作。
有没有办法在滚动正常的情况下获得 %
中的 width
?
In this example http://bit.ly/t2ImYS width of wrapper of all elements is fixed 8520px
#scroller {
width: 8520px;
height: 100%;
float: left;
padding: 0;}
I want width
dynamic so if i add more elements inside <div id="scroller">
this #scroller should take the width upon elements inside it.
So tried to set width
#scroller {
width: 100%;}
and
#scroller {
width: auto}
but then scroller doesn't work properly.
is there a way to get width
in %
with properly working scroll?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
设置li元素为display:inline-block;并删除浮动:左; (您也可以删除垂直对齐,因为它仅适用于表格单元格元素)
从包装器中删除固定宽度。
添加空白:nowrap;向ul
你应该没问题...
而且
(除了 <=ie7,但我想这对你来说没有问题?)
Set the li elements to display:inline-block; and remove the float:left; (you could also remove the vertical-align, since that will only work on table-cell elements)
Remove the fixed width from the wrapper.
Add white-space:nowrap; to the ul
And you should be fine...
(Except in <=ie7, but I suppose that's no problem in your case?)
如果您使用 iScroll4,您应该刷新滚动条或销毁并重新创建它。
摘自此处:
“iScroll 需要知道包装器和滚动器的正确尺寸。它们是在启动时第一次计算的,但如果您的代码更改了元素大小,则需要警告 iScroll,您正在弄乱 DOM。”
If you are using iScroll4 you should refresh the scroller or destroy and recreate it.
Excerpt from here:
"iScroll needs to know the correct dimensions of both the wrapper and the scroller. They are computed the first time at start up but if your code changes the elements size, iScroll needs to be warned that you are messing with the DOM."
使用显示:内联块
使用百分比对我有用:
Using Display:inline-block
and using percentages worked for me:
在滚动条中添加动态项后尝试调用 iscroll
refresh()
方法来设置宽度。Try calling the iscroll
refresh()
method after adding dynamic items within the scroller to set the width.尝试这个CSS代码,它对我有用:
http://jsfiddle.net/manseuk/r9VL2/2/
try this css code, it worked for me:
http://jsfiddle.net/manseuk/r9VL2/2/