方案一:滿屏
QQ截圖20181212112423.png
/*wxml*/
<swiper style='width:100%;height:100%;' current='{{current}}' bindchange='bindChange'>
切記是行內(nèi)樣式
/*wxss*/
swiper-item {
overflow: scroll;
}
ok完美解決!
方案二:半屏
這個(gè)才是重點(diǎn)宠默,小二灵巧,上酸菜!瓤球!
QQ截圖20181212113929.png
這個(gè)就不能行內(nèi)設(shè)置height:100%;了,要根據(jù)元素節(jié)點(diǎn)動態(tài)設(shè)置高敏弃,紅框中的是我的循環(huán)元素麦到,要獲得每個(gè)swiper-item的高
//js
get_wxml: function (className, callback){
wx.createSelectorQuery().selectAll(className).boundingClientRect(callback).exec()
},
wx.request({
url: baseUrl + 'm=wttapi&a=getcategories',
method: "POST",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function(res) {
console.log(res)
that.setData({
scrollNav: res.data.data[1],
scrollContent: res.data.data[2],
indexTit: res.data.data[1][0].name
})
let change_id = that.data.scrollNav[that.data.idx].cid//動態(tài)獲取每個(gè)swiper-item元素節(jié)點(diǎn)的id
that.setData({
swiper_length: that.data.scrollNav.length-1//獲取節(jié)點(diǎn)數(shù)量
})
//獲取節(jié)點(diǎn)信息
that.get_wxml(`#cont${change_id}`,(rects)=>{
that.setData({
height:rects[0].height
})
})
}
})
/*wxml*/
<swiper style='width:100%;height:{{height}}px;background:#F5F5F5;' current='{{current}}' bindchange='moveSwiper'>
<swiper-item wx:for='{{scrollNav}}' wx:for-item='item' wx:key='index' class='swiper_content' item-id='{{item.cid}}'>
<!-- <view class='loading'>
<image src='/images/loading.gif'></image>
<text>努力加載中</text>
</view> -->
<view id='cont{{item.cid}}'>
<view wx:for='{{scrollContent}}' wx:key='index' id='cont{{item[0].pcid}}'>
<view class='content-title'>
<image src='/images/lefticon@2x.png'></image>
<text style='display:inline-block;margin:0 10rpx;'>{{item[0].pcname}}</text>
<image src='/images/righticon@2x.png'></image>
</view>
<view class='content-box'>
<view wx:for='{{scrollContent[index]}}' wx:key='index' class='content-box-list' data-id='{{item.ccid}}' data-name='{{item.name}}' bindtap='intoList'>
<image src='{{item.image}}' style='max-width:160rpx;max-height:160rpx;'></image>
<view class='content-box-price'>
<view>
<text class='price-text1 fontweight'>{{item.name}}</text>
<text class='price-text2'>今日最值</text>
</view>
<text class='price-text3'>{{item.price}}</text>
</view>
</view>
</view>
</view>
</view>
</swiper-item>
</swiper>
ok拟赊,小白一枚步清,以上兩種方案是小程序初步踩坑經(jīng)驗(yàn)