微信小程序:滚动视图容器

直接上两种方案代码以及效果图:

方案1

  • 这种方案是直接使用view,并设置overflow: scroll
    wxml:
 <view class="container">
    <view class="content" wx:for="{{11}}" wx:key="item">
        {{item}}
    </view>
</view> 

wxss:

 .container {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100vh;
    overflow: scroll;
    padding-bottom: 20rpx;
    background: #FD9494;
}

.content {
    margin: 20rpx auto 0 auto;
    width: 710rpx;
    height: 300rpx;
    background: #ddd;
    border-radius: 16rpx;
    font-size: 80rpx;
    line-height: 300rpx;
    text-align: center;
}

效果图:


IMG_5550.TRIM.gif

方案2

  • 使用scroll-view + container作为容器
    wxml:
<scroll-view class="main_container" scroll-y>
    <view class="container">
        <view class="content" wx:for="{{11}}" wx:key="item">
            {{item}}
        </view>
    </view>
</scroll-view> 

wxss:

.main_container {
    position: relative;
    width: 750rpx;
    height: 100vh;
    background: #FD9494;
}

 .container {
    position: absolute;  /*使用absolute的原因是因为为了防止第一个子视图有margin-top时,造成顶部留白的情况*/
    left: 0;
    top: 0;
    width: 100%;
    padding-bottom: 20rpx;
} 

.content {
    margin: 20rpx auto 0 auto;
    width: 710rpx;
    height: 300rpx;
    background: #ddd;
    border-radius: 16rpx;
    font-size: 80rpx;
    line-height: 300rpx;
    text-align: center;
}

需要在页面json里添加:"disableScroll": true

效果图:


IMG_5552.TRIM.gif

对比结果:
因为iPhone上滚动会带有弹簧效果,所以方案1在滚动时会出现不流畅的现象。方案2就不会出现这种情况,而且滚动也是流畅的。

方案2是我目前总结出来的比较好的滚动视图方案。

注意

  • 因为方案二使用了scroll-view,所以在使用有些官方不推荐在scroll-view中使用的组件(如canvas)时需要注意。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容