解决微信小程序scroll-view高度自适应问题的方法

第一种情况,scroll-view占据整屏

html
scroll-view {
 
 height: 100vh;
 
}



第二种情况,scroll-view自适应页面剩余高度

 wxml

<view class="box">
 <view class="view"></view>
 <scroll-view class="box-scroll"></scroll-view>
</view>


wxss

box {
 display: flex;
 flex-direction:column;
 height:100vh;
 overflow:hidden;
}
.view {    <!-- 这段可以不用-->
 flex-shrink: 0;
 height: 50px;
}
.box-scroll {
 flex: 1;
 height: 1px;
}

flex:1 高度依然不会自适应加一个默认高度1px就可以自适应了

实际中的使用示例(重点:第七行代码中的style="height: 1px;")


 <view wx:if="{{sheetid==2}}" class="hxt-flex-fill hxt-flex-column" style="background-color: #fff;">
        <view class="hxt-flex" style="border-bottom: 1rpx solid #eee;padding: 10rpx 30rpx;display: flex;align-items: center;">
            <view class="title">收费项目:</view>
            <view class="hxt-flex-fill" style="color: #00B1FF;" bindtap="sheetproject">选择收费项目</view>
        </view>
        <view class="hxt-flex-fill" style="height: 1px;" >
            <scroll-view style="height:100%" scroll-y enhanced="{{true}}" bounces="{{false}}">
                <view>
                    <view class="hxt-flex" style="border-bottom: 1rpx solid #eee;padding: 10rpx 30rpx;display: flex;align-items: center;">
                        <view class="title">截止日期:</view>
                        <picker mode="date" value="{{date}}" class="hxt-flex-fill" style="color: {{picker?'':'grey'}};" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
                            {{picker?picker:'请选择截止日期'}}
                        </picker>
                    </view>
                    <view class="hxt-flex-column" style="padding: 10rpx 0rpx 10rpx 30rpx;">
                        <view class="hxt-flex">
                            <view class="title hxt-flex-fill">收费明细:</view>
                            <view class="title" style="width: 150rpx;text-align: center;">收费金额</view>
                            <view class="title" style="width: 100rpx;text-align: center;">必交</view>
                        </view>
                        <view class="hxt-flex">
                            <view class="title hxt-flex-fill" style="color: gray;">以下项目由《速达快餐》提供:</view>
                        </view>
                        <checkbox-group bindchange="checkboxChange" style="border-bottom: 1rpx solid #eee;padding-bottom: 10rpx;">
                            <view class="hxt-flex" wx:for="{{2}}" style="margin-top: 10rpx;">
                                <view class="title hxt-flex-fill">
                                    <checkbox class="round sm blue" value="{{objCheck.value(item)}}"></checkbox>
                                    汉族中餐
                                </view>
                                <view class="title hxt-flex" style="width: 150rpx;justify-content: center;">¥<input type="number" placeholder="金额" style="width: 70rpx;text-align: left;color: #00B1FF;" /></view>
                                <view class="title" style="width: 100rpx;text-align: center;">
                                    <i class="fa fa-check" aria-hidden="true" style="color: #00B1FF;"></i>
                                </view>
                            </view>
                        </checkbox-group>
                        <view class="hxt-flex">
                            <view class="title hxt-flex-fill" style="color: gray;">通知对象:</view>
                        </view>
                        <checkbox-group bindchange="checkboxChange" style="border-bottom: 1rpx solid #eee;padding-bottom: 10rpx;">
                            <view class="hxt-flex" wx:for="{{20}}" style="margin-top: 10rpx;">
                                <view class="title hxt-flex-fill">
                                    <checkbox class="round sm blue" value="{{objCheck.value(item)}}"></checkbox>
                                    汉族中餐
                                </view>
                                <view class="title hxt-flex" style="width: 150rpx;justify-content: center;">
                                    <button class="cu-btn shadow" style="width:auto;height: auto;padding:10rpx 30rpx;font-weight: normal;color: gray;" bindtap="Sheet">0/28</button>
                                </view>
                            </view>
                        </checkbox-group>
                        <view>
                            <view style="padding: 20rpx 0;">备注:</view>
                            <textarea maxlength="{{noteMaxLen}}" style="width: 100%;border-top: 1rpx solid #eee;border-bottom: 1rpx solid #eee;padding: 10rpx 0;height: 200rpx;" fixed="true" bindinput="getWords" placeholder="请输入备注。。。" />
                        </view>
                    </view>
                </view>
            </scroll-view>
        </view>
        <view style="border-top: 1rpx solid #eee;padding: 20rpx 10rpx;text-align: right;">
        <button class="cu-btn shadow" style="width:auto;height: auto;padding:10rpx 30rpx;font-weight: normal;color: white;background-color: #00B1FF;" bindtap="Sheet">保存</button>
        </view>
    </view>


 

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
微信小程序中,可以通过设置scroll-view组件的样式来实现自定义的外观效果。根据引用\[1\]中的描述,可以使用以下方法来设置scroll-view的样式: 1. 如果要让scroll-view占据整个屏幕,可以设置scroll-view高度为100vh,表示占据整个屏幕的高度。 2. 如果要让scroll-view占据剩余的屏幕高度,可以使用flex布局,将scroll-view的父容器设置为flex,并设置flex-grow属性为1,这样scroll-view会自动占据剩余的空间。 3. 如果scroll-view内部有多个view布局,可以根据需要设置每个view高度,并将scroll-view高度设置为剩余的屏幕高度。 根据引用\[2\]中的测试布局,可以参考以下代码示例来设置scroll-view的样式: ```html <view class="box"> <view class="view"></view> <scroll-view class="box-scroll"></scroll-view> </view> ``` ```css .box { display: flex; flex-direction: column; height: 100vh; } .view { height: 100px; } .box-scroll { flex-grow: 1; } ``` 在上述代码中,通过设置.box的高度为100vh,使其占据整个屏幕高度。.view高度设置为100px,.box-scroll使用flex-grow: 1来占据剩余的屏幕高度。 此外,根据引用\[3\]中的提示,还可以使用enhanced属性来启用scroll-view的增强特性,通过ScrollViewContext来操作scroll-view。但是需要注意,该特性仅在scroll-view组件开启enhanced属性后生效。 希望以上信息对您有所帮助! #### 引用[.reference_title] - *1* *2* [【微信小程序scroll-view高度自适应】](https://blog.csdn.net/qq_31629679/article/details/131381572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [微信小程序 scroll-view](https://blog.csdn.net/weixin_59727199/article/details/126875797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值