微信小程式實現列表的橫向滑動方式
阿新 • • 發佈:2020-07-16
微信小程式原生方式實現列表的橫向滑動的兩種方法:
效果圖:
方式一:簡單樣式實現
父元素設定:
white-space:nowrap;//不換行
overflow-x: auto;子元素設定:
display:inline-block;
方式二:scroll-view 標籤 + 樣式
scroll-view的橫向滾動:
- scroll-view的內層view元素需要:display:inline-block;
- scroll-view的外層元素需要:white-space:nowrap;
實現程式碼:
1.wxml
<!--pages/packageA//multiple-function/multiple-function.wxml--> <view class="content"> <view class="Btn"> <view class="clickBtn" data-id="" bindtap = "toClickTab">返回功能列表頁</view> </view> <view>實現橫向滾動效果:</view> <view style="margin-top: 60rpx;"> 方式一:<view>父元素設定 white-space:nowrap;//不換行 overflow-x: auto;</view> <view>子元素設定display:inline-block;</view> </view> <view class="listContent"> <view class="item" wx:for="{{userList}}" wx:key="{{index}}"> <image class="userAvatar" src="{{item.avatar}}" mode="aspectFit"/> <view class="userName">{{item.userName}}</view> </view> </view> <view style="margin-top: 60rpx;margin-bottom: 20rpx;"> <view>方式二:scroll-view的橫向滾動:</view> <view>scroll-view的內層view元素需要:display:inline-block;</view> <view>scroll-view的外層元素需要:white-space:nowrap;</view> </view> <view style="white-space:nowrap"> <scroll-view scroll-x> <view class="listContent2"> <view style="display: inline-block;" class="item2" wx:for="{{userList}}" wx:key="{{index}}"> <image class="userAvatar" src="{{item.avatar}}" mode="aspectFit"/> <view class="userName">{{item.userName}}</view> </view> </view> </scroll-view> </view> </view>
2.js
// pages/packageA//multiple-function/multiple-function.js Page({ /** * 頁面的初始資料 */ data: { userList:[ {'id':"1","avatar":"../../../images/tarbar/my-selected.png","userName":"使用者1"},{'id':"2","userName":"使用者2"},{'id':"3","userName":"使用者3"},{'id':"4","userName":"使用者4"},{'id':"5","userName":"使用者5"},{'id':"6","userName":"使用者6"},{'id':"7","userName":"使用者7"},{'id':"8","userName":"使用者8"},{'id':"9","userName":"使用者9"},{'id':"10","userName":"使用者10"},{'id':"11","userName":"使用者11"},] },/** * 生命週期函式--監聽頁面載入 */ onLoad: function (options) { },/** * 生命週期函式--監聽頁面顯示 */ onShow: function () { } })
3.wxss
/* pages/packageA//multiple-function/multiple-function.wxss */ page{ width: 100%; height: 100%; background: #f7f7f7; } .main{ width:100%; margin-top: 60rpx; position: relative; } .row{ display: flex; flex-direction: row; align-items: center; height: 100rpx; background: #ffffff; } .hintText{ width:90%; font-size: 28rpx; color:#000000; font-family: PingFang SC; font-weight: 400; padding-left: 30rpx; } .rightArrow{ width:36rpx; height: 36rpx; } .line{ margin-left: 30rpx; height: 1px; background: #eeeeee; } /*列表項*/ .listContent{ width:100%; margin-top: 20rpx; white-space: nowrap;/*父元素 一行顯示,不換行*/ overflow-x: auto; } .item{ display: inline-block;/*子元素 display: inline-block*/ width:20%; height: 100rpx; text-align: center; } .userAvatar{ width:60rpx; height: 60rpx; } /*使用者名稱*/ .userName{ font-size: 28rpx; color:#acacac } /*列表項*/ .listContent2{ width:100%; } .item2{ width:20%; height: 100rpx; text-align: center; } /*去除下劃線的線條*/ ::-webkit-scrollbar{ width:0; height: 0; color: transparent; } .viewBtn{ width: 100%; height: 80rpx; padding-top: 20rpx; } /*返回按鈕*/ .clickBtn{ width:250rpx; line-height: 1; font-size: 32rpx; font-weight: 600; padding: 24rpx 0; background-color: #07c160; color:#ffffff; margin-left: 20rpx; border-radius: 10rpx; text-align: center; }
到此這篇關於微信小程式實現列表的橫向滑動的文章就介紹到這了,更多相關微信小程式的橫向滾動內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!