1. 程式人生 > >小程式資料列表上下滑的內部加左右滑

小程式資料列表上下滑的內部加左右滑

在這裡插入圖片描述 如圖,左邊是固定的,右邊內容是可以左右滑動的,上面和下面部分也是固定的,整個可以上下滑動。 wxml:

<view class="top">
  <view class='topLeft'></view>
  <scroll-view class='topBox' scroll-x scroll-with-animation scroll-left='{{left}}' bindscroll="scrolling" bindtouchstart='scrollTouchStart' data-scroll='1'>
    <block wx:for='{{groupArr}}' wx:key>
      <view class='topRight'>{{item.company}}</view>
    </block>
  </scroll-view>
</view>
<view class="type">
  <view class="label">品質:</view>
  <block wx:for='{{typeArr}}' wx:key>
    <view class="item {{index==0?'marginLeft':''}} {{activeIndex==index?'cur':''}}" bindtap='chooseType' data-index='{{index}}'>{{item}}</view>
  </block>
</view>
<view class="wrapper">
  <view class="main">
    <view class="left">
      <block wx:for='{{lists}}' wx:key>
        <view class="line1 {{index==lists.length-1?'bottomBorder':''}}">
          <text class='border'>{{item.name}}</text>
        </view>
      </block>
    </view>
    <scroll-view scroll-x class="right" bindscroll="scrolling" scroll-with-animation scroll-left='{{left}}' bindtouchstart='scrollTouchStart' data-scroll='2'>
      <!-- <view class="right-wrapper"> -->
      <block wx:for='{{lists}}' wx:key>
        <view class="line2 {{index==lists.length-1?'bottomBorder':''}}">
          <block wx:for='{{item.priceArr}}' wx:key wx:for-item='price' wx:for-index='index2'>
            <view class='price'>
              {{price}}
              <!-- 最低價的樣式 -->
              <image src='/img/
[email protected]
' class='minPrice' wx:if='{{item.minIndex==index2}}'></image> <!-- 建議讓ui切圖的時候直接帶上文字,前端加上的話,第一不好固定位置,第二增加太多頁面節點,資料量大的時候就會延長載入時間 --> <view class='minPriceText' wx:if='{{item.minIndex==index2}}'>最低</view> </view> </block> </view> </block> <!-- </view> --> </scroll-view> </view> </view> <view style='width:100%;height:100rpx;'></view> <view class="bottom"> <view class='bottomLeft'></view> <scroll-view class='bottomBox' scroll-x scroll-with-animation scroll-left='{{left}}' bindscroll="scrolling" bindtouchstart='scrollTouchStart' data-scroll='3'> <block wx:for='{{groupArr}}' wx:key> <view class='bottomRight'> <text class='allCostText'>合計\n</text> <text class='allCost'>¥{{item.allCost}}</text> <image src='/img/back
[email protected]
' class='icon'></image> </view> </block> </scroll-view> </view>

wxss:

.top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  text-align: center;
  z-index: 3;
  white-space: nowrap;
  background-color: white;
  width: 100%;
  border-bottom: 1px solid #D8D8D8;
}
page{
  overflow-x: hidden;
  width: 100%;
}
.topBox{
  width: 600rpx;
  height: 100rpx;
  float: right;
}
.topLeft{
  width: 150rpx;
  height: 100rpx;
  float: left;
}
.topRight{
  width: 200rpx;
  height: 100rpx;
  text-align: center;
  line-height: 100rpx;
  display: inline-block;
  vertical-align: middle;
  border-left: 1px solid #D8D8D8;
  font-size: 28rpx;
}
.type{
  position: fixed;
  top:100rpx;
  z-index: 3;
  font-size: 28rpx;
  color: #555;
  background: #F9F9F9;
  width: 100%;
  white-space: nowrap;
}
.item{
  display: inline-block;
  padding: 10rpx 20rpx;
  background: #fff;
  border: 1px solid #de5550;
  border-radius: 4px;
  margin-left: 15rpx;
}
.label{
  font-weight: bold;
  line-height: 100rpx;
  padding: 0rpx 20rpx;
  display: inline-block;
}
.marginLeft{
  margin-left: 40rpx;
}
.cur{
  padding: 10rpx 20rpx;
  background: #fff;
  border: 1px solid #de5550;
  border-radius: 4px;
  background-image: url("http://store.ddyc.com/xiaochengxu/cs/2018/0413/icon_bg_label_chosen%403x.png");
  background-position: right bottom;
  background-size: 30rpx 30rpx;
  background-repeat: no-repeat;
}
.wrapper {
  margin-top: 200rpx;
  width: 750rpx;
  overflow-x: hidden;
  position: relative;
  background-color: white;
}

.left {
  position: absolute;
  width: 150rpx;
  z-index: 2;
  background-color: white;
}

.right {
  margin-left: 150rpx;
  width: 600rpx;
}

.right-wrapper {
  overflow-x: auto;
}

.line1 {
  width: 150rpx;
  height: 100rpx;
  text-align: center;
  line-height: 100rpx;
}
.border{
  width: 148rpx;
  height: 98rpx;
  display: block;
  border-top: 1rpx solid #d8d8d8;
  border-right: 1rpx solid #d8d8d8;
}
.line2 {
  height: 100rpx;
  white-space: nowrap;
}

.price {
  width: 200rpx;
  text-align: center;
  height: 100rpx;
  line-height: 100rpx;
  display: inline-block;
  border-top: 1rpx solid #d8d8d8;
  border-right: 1rpx solid #d8d8d8;
  position: relative;
}
.minPrice{
  width: 96rpx;
  height: 80rpx;
  position: absolute;
  top: 0;
  right: 0;
  opacity: 0.2;
}
.minPriceText{
  width: 56rpx;
  height: 56rpx;
  font-size: 24rpx;
  color: #de5550;
  position: absolute;
  top: -15rpx;
  right: -10rpx;
  transform: rotate(43deg);
}
.bottom{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  text-align: center;
  z-index: 3;
  white-space: nowrap;
  background-color: white;
  width: 100%;
  border-top: 1rpx solid #d8d8d8;
}
.bottomLeft{
  width: 148rpx;
  height: 100rpx;
  float: left;
  border-left: 1rpx solid #d8d8d8;
}
.bottomBox{
  width: 600rpx;
  height: 100rpx;
  float: left;
}
.bottomRight{
  width: 200rpx;
  height: 100rpx;
  text-align: left;
  display: inline-block;
  border-left: 1rpx solid #d8d8d8;
}
.bottomBorder{
  border-bottom: 1rpx solid #d8d8d8;
}
.icon{
  width: 24rpx;
  height: 24rpx;
  float: right;
  margin-right: 20rpx;
  line-height: 100rpx;
  margin-top: -5rpx;
}
.allCostText{
  font-size: 24rpx;color: #999999;margin-left:30rpx;
}
.allCost{
  font-size: 30rpx;color: #555555;margin-left:30rpx;font-weight:bold;
}

js:

// pages/classification1/classification.js
Page({

  /**
   * 頁面的初始資料
   */
  data: {
    activeIndex:0,//選擇的型別下標
    typeArr: ["原廠原包", "原廠配套", "品牌","適用"],
    // 上面和下面定位的資料,結構可以根據實際介面更改
    groupArr: [
      { company: '西安恆美華城',allCost:900},
      { company: '西安恆美華城', allCost: 900 },
      { company: '西安恆美華城', allCost: 900 },
      { company: '西安恆美華城', allCost: 900 },
    ],
    // 中間內容部分的資料,資料結構以及欄位名根據實際介面更改
    lists:[
      {
        type:1,
        name:'輪胎',
        priceArr:[110,190,100,180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
      {
        type: 1,
        name: '輪胎',
        priceArr: [110, 190, 100, 180],
      },
    ]
  },
  /**
   * 生命週期函式--監聽頁面載入
   */
  onLoad: function (options) {
    this.getList();
  },
  scrollTouchStart:function(e){
    if (e.currentTarget.dataset.scroll != this.data.touchTarget){
      this.setData({
        touchTarget: e.currentTarget.dataset.scroll
      })
    }
  },
  // 
  scrolling:function(e){
    if (e.target.dataset.scroll == this.data.touchTarget){
      this.setData({
        left: e.detail.scrollLeft
      })
    }
  },
  // 點選選擇型別
  chooseType:function(e){
    this.setData({
      activeIndex: e.currentTarget.dataset.index
    });
    // this.getList();//重新請求資料
  },
  getList:function(){
    // 請求介面,接收資料並渲染
    var activeIndex = this.data.activeIndex;//當前選擇的型別下標,
    //request之後處理資料,如果後臺直接返回一行的哪個資料數值最小也就是最便宜的那個,直接渲染,如果後臺只是返回具體數字,前端處理一下,如上面的模擬的資料,前端處理
    var lists = this.data.lists;
    for(var i=0;i<lists.length;i++){
      var min = Math.min.apply(null, lists[i].priceArr);
      var index = lists[i].priceArr.indexOf(min);
      lists[i].minIndex = index;
    }
    this.setData({
      lists:lists
    })

  },
  /**
   * 生命週期函式--監聽頁面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命週期函式--監聽頁面顯示
   */
  onShow: function () {

  },

  /**
   * 生命週期函式--監聽頁面隱藏
   */
  onHide: function () {

  },

  /**
   * 生命週期函式--監聽頁面解除安裝
   */
  onUnload: function () {

  },

  /**
   * 頁面相關事件處理函式--監聽使用者下拉動作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 頁面上拉觸底事件的處理函式
   */
  onReachBottom: function () {

  },

  /**
   * 使用者點選右上角分享
   */
  onShareAppMessage: function () {

  }
})