參考文章 javascript
https://developers.weixin.qq.com/community/develop/doc/00008aaf4a473056d1c69a8b253c04html
看了他的代碼後,其實他已經寫了百分之80了,但不完美,只是針對固定定位的。修改代碼以下:java
原理爲在swiper-item
的最上面和最下面插入空view,並利用wx的方法獲取兩個之間的高度差,而後設置給swiper
。 有些細節,本身調整下。說真的,搞不懂爲何小程序不把這個組件作好,還得咱們本身計算,坑!!!!web
<swiper bindchange="swiperChange" current="{{isIndex}}" style="height:{{swiper_height}}"> <swiper-item> <view id="start{{isIndex}}"></view> <view class="item" wx:for="{{listData}}" wx:key="{{index}}"> <image class="item-img" src="{{item.img}}"></image> <view class="item-wrap"> <view class="item-name text-line-2">{{item.name + index}}</view> <view class="item-des text-line-1">{{item.des}}</view> <view class="item-price" >{{item.price}}</view> <view class="item-add"> <view class="item-plus"></view> </view> </view> </view> <view id="end{{isIndex}}"></view> </swiper-item> </swiper>
page { background: #fbf8fb; height: 100%;/*必須設置*/ } .item { margin: 30rpx 30rpx 0; height: 220rpx; position: relative; border-radius: 8rpx; box-shadow: 0 2px 30rpx #eee; box-sizing: border-box; font-size: 28rpx; color: #63595b; background: #fff; overflow: hidden; } .item-wrap { padding: 20rpx; } .item-img { width: 180rpx; height: 180rpx; border-radius: 8rpx; float: left; margin: 20rpx; } .item-name { margin-bottom: 10rpx; word-break: break-all; } .item-des { font-size: 24rpx; color: #a9a4a5; word-break: break-all; } .item-price { color: #c05d70; position: absolute; left: 220rpx; bottom: 20rpx; } .item-price::before { content: '¥'; font-size: 20rpx; } .item-price::after { content: attr(data-text); color: #a9a4a5; } .item-add { position: absolute; width: 50rpx; height: 50rpx; right: 20rpx; bottom: 20rpx; background: #c05d70; border-radius: 50%; border: 2rpx solid #fff; box-shadow: 0 0 30rpx #e2b5be; } .item-add::after { content: ''; position: absolute; width: 200%; height: 200%; left: -50%; top: -50%; } .item-plus { width: 24rpx; height: 24rpx; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); -webkit-transform: translate(-50%,-50%); } .item-plus::before { content: ''; width: 24rpx; height: 4rpx; background: #fff; position: absolute; left: 0; top: 10rpx; } .item-plus::after { content: ''; width: 4rpx; height: 24rpx; background: #fff; position: absolute; left: 10rpx; top: 0; }
const app = getApp() Page({ data: { isIndex: 0, swiper_height: 0, listData:[] }, onLoad: function () { this.getData(); }, getData(){ let { listData } = this.data; let _item = { img: 'http://yanxuan.nosdn.127.net/149dfa87a7324e184c5526ead81de9ad.png?imageView&thumbnail=430x430&quality=95', name: '日式和風懶人沙發', des: '隨形承託,外套可拆洗', price: 139 } wx.showLoading(); setTimeout(()=>{ for (var i = 0; i < 101; i++) { listData.push(_item) } this.setData({ listData }); this.autoHeight() wx.hideLoading(); }, 1000) }, swiperChange: function (e) { this.setData({ isIndex: e.detail.current }); this.autoHeight(); }, autoHeight(){ let isIndex = this.data.isIndex; wx.createSelectorQuery() .select('#end'+isIndex ).boundingClientRect() .select('#start'+isIndex ).boundingClientRect().exec(rect => { let _space = rect[0].top - rect[1].top; _space = _space === 0 ? '100%' : _space+'px'; this.setData({ swiper_height: _space }); }) } })