水平滚动
在微信小程序文档中,只提到了设置scroll-x可以实现滑动方向的改变。但今天在练习时发现,对用view包裹的较复杂布局使用scroll-x并不能实现理想效果。
在网上搜索后,发现,原来是因为少了wxss的设置。
在scroll-view控件的属性里设置:white-space: nowrap; (不换行)
在子控件里设置: display: inline-block;
垂直滚动
- scroll-view的scroll-top不生效问题
原因: 要设置scroll-view的高度(且不能设置百分比形式)
注意滚动视图垂直滚动时一定要设置高度否则的话scroll-view不会生效。
基本使用
<!--垂直滚动,这里必须设置高度-->
<scroll-view scroll-y="true" style="height: 200px">
<view style="background: red; width: 100px; height: 100px" ></view>
<view style="background: green; width: 100px; height: 100px"></view>
<view style="background: blue; width: 100px; height: 100px"></view>
<view style="background: yellow; width: 100px; height: 100px"></view>
</scroll-view>
<!--水平滚动-->
<scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" >
<!-- display: inline-block-->
<view style="background: red; width: 200px; height: 100px; display: inline-block" ></view>
<view style="background: green; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: blue; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: yellow; width: 200px; height: 100px; display: inline-block"></view>
</scroll-view>