小程序實現簡單分頁組件
本文實例為大傢分享瞭小程序實現簡單分頁組件的具體代碼,供大傢參考,具體內容如下
wxml:
<!-- 分頁組件 --> <view class="page_div"> <view class="page_sum">共{{pagetotal}}頁</view> <view class="page_prev" bindtap="prevFn">上一頁</view> <view class="page_number_div"> <input value="{{pageNumber}}" bindinput="inputValue" data-name="pageNumber"></input> <view class="pageGo" bindtap="pageGo">Go</view> </view> <view class="page_next" bindtap="nextFn">下一頁</view> </view>
wxss:
/************分頁樣式****************/ .page_div{ display: flex; width: 100%; justify-content: flex-end; box-sizing: border-box; padding:20rpx; background-color: #fff; } .page_div .page_sum, .page_div .page_prev, .page_div .page_number_div, .page_div .page_next{ height: 50rpx; line-height: 50rpx; font-size:24rpx; color: #333; } .page_div .page_prev, .page_div .page_next{ background-color: #eee; padding:0 10rpx; margin:0 10rpx; } .page_div .page_number_div .pageGo{ display: inline-block; vertical-align: middle; width: 50rpx; box-sizing: border-box; background-color: #eee; text-align: center; margin:0 10rpx; } .page_div .page_number_div input{ width: 100rpx; display: inline-block; vertical-align: middle; text-align: center; border:1px solid #eee; } /************分頁樣式結束************/
js:
Page({ data: { //分頁數據 pageNumber:1, pagetotal:5, page:1 }, onLoad: function (options) { }, //input輸入雙向綁定數據 inputValue:function(e){ let name = e.currentTarget.dataset.name; let mapName ={}; mapName[name]=e.detail && e.detail.value; // console.log(mapName); this.setData(mapName); }, //上一頁 prevFn:function(){ if(this.data.pageNumber <=1){ wx.showToast({ icon:'none', title: '已經是最前一頁', }) return; } wx.showLoading({ title: '加載中...', }) this.setData({ pageNumber:Number(this.data.pageNumber)-1 }) console.log(this.data.pageNumber); setTimeout(function(){ wx.hideLoading() },1000) }, //下一頁 nextFn:function(){ if(this.data.pageNumber === this.data.pagetotal){ wx.showToast({ icon:'none', title: '已經是最後一頁', }) return; } wx.showLoading({ title: '加載中...', }) this.setData({ pageNumber:Number(this.data.pageNumber)+1 }) console.log(this.data.pageNumber); setTimeout(function(){ wx.hideLoading() },1000) }, //去到某一頁 pageGo:function(){ console.log(Number(this.data.pageNumber)); if(Number(this.data.pageNumber) > this.data.pagetotal){ this.setData({ pageNumber:this.data.pagetotal }) }else if(Number(this.data.pageNumber) <= 0){ this.setData({ pageNumber:1 }) } console.log(Number(this.data.pageNumber)); wx.showLoading({ title: '加載中...', }) setTimeout(function(){ wx.hideLoading() },1000) } })
效果圖:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。