微信小程式完美解決scroll-view高度自適應問題的方法
阿新 • • 發佈:2020-08-09
第一種情況,scroll-view佔據整屏
scroll-view { height: 100vh; }
第二種情況,scroll-view自適應頁面剩餘高度
xml檔案
<view class="box"> <view class="box-head"></view> <scroll-view class="box-scroll"></scroll-view> </view>
wxss檔案
.box { display: flex; flex-direction:column; height:100vh; overflow:hidden; } .box-head { flex-shrink: 0; height: 50px; } .box-scroll { flex: 1; height: 1px; }
flex:1 高度依然不會自適應加一個預設高度1px就可以自適應了
通用元件化處理
list.wxml
<scroll-view class="list-scroll {{ autoHeight ? 'list-scroll--auto' : '' }}" scroll-y enable-back-to-top bind:scrolltolower="scrolltolower" > <slot></slot> <!-- 載入完成 --> <view wx:if="{{ finished }}" class="list-loading"> <view class="list-loading__text">{{ finishedText }}</view> </view> <!-- 載入效果 --> <view wx:elif="{{ loading }}" class="list-loading"> <van-loading type="spinner" size="20"></van-loading> </view> </scroll-view>
list.scss(需編譯成list.wxss)
.list { &-scroll { flex: 1; height: 100vh; &--auto { height: 1px; } } &-loading { margin: 10px 0; text-align: center; &__text { font-size: 16px; color: #a6a6a6; line-height: 1; } } }
list.js
// components/list/list.js Component({ externalClasses: ["class"],options: { virtualHost: true,// 元件虛擬化 },/** * 元件的屬性列表 */ properties: { // 載入狀態 loading: { type: Boolean,value: false,},// 載入完成 finished: { type: Boolean,// 載入完成文字 finishedText: { type: String,value: "沒有更多了",// 自動初始化獲取資料 autoInit: { type: Boolean,value: true,// flex自定適應高度 autoHeight: { type: Boolean,/** * 元件的方法列表 */ methods: { /** * 滾動到底部閾值 */ scrolltolower() { // 退出執行 if ( this.data.emptyText || // 沒有資料 this.data.loading || // 正在載入 this.data.finished // 載入完成 ) { return; } this.setData({ loading: true,}); this.triggerEvent("load"); },/** * 元件宣告週期 */ lifetimes: { attached() { // 自動初始化 if (this.data.autoInit) { this.scrolltolower(); } },});
元件化後一定要設定元件虛擬化。否則高度還是不會自適應
list.json
"component": true,"usingComponents": { "van-loading": "@vant/weapp/loading/index" }
需要安裝van-loading或者自己寫一個loading效果
使用
全屏
<com-list></com-list>
自適應
<com-list auto-height></com-list>
到此這篇關於微信小程式完美解決scroll-view高度自適應問題的方法的文章就介紹到這了,更多相關小程式scroll-view高度自適應內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!