1. 程式人生 > >微信小程式 scroll-view滾動到索引位置

微信小程式 scroll-view滾動到索引位置

有一些重要的關鍵點請看上一篇文章

主要用到scroll-into-view  如果你想讓他回頂部 直接用scroll-top即可

話不多說,看程式碼

wxml

  1. <scroll-viewscroll-y="true"scroll-into-view="{{toView}}">
  2.   <viewclass="brand"wx:for="{{brandList}}">
  3.     <viewclass="line"></view>
  4.     <viewid="{{item.wordindex}}"class="wordindex">{{item.wordindex}}
    </view>
  5.     <viewclass="line"></view>
  6.     <viewclass="brand_block">
  7.       <viewclass="color_view"wx:for="{{item.brand}}"bindtap="click">
  8.         <imagesrc="{{item.brandimg}}"></image>
  9.       </view>
  10.     </view>
  11.   </view>
  12. </scroll-view>
  13. <viewclass="index"
    >
  14.   <textwx:for="{{wordindex}}"bindtap="choiceWordindex"data-wordindex="{{item.wordindex}}">
  15.     {{item.wordindex}}  
  16.   </text>
  17. </view>

wxjs[javascript] view plain copy
  1. // pages/order/car/add_car/car_brand/car_brand.js
  2. var app = getApp()  
  3. Page({  
  4.   data: {  
  5.     "brandList": [],  
  6.     "wordindex"
    : [],  
  7.     "toView"'#',  
  8.   },  
  9.   onLoad: function (options) {  
  10.     var that = this;  
  11.     // 頁面初始化 options為頁面跳轉所帶來的引數
  12.     app.func.req('getCarBrand?cx=1'function (res) {  
  13.       if (res.data.result == 'false') {  
  14.         console.log('false');  
  15.         that.wetoast.toast({  
  16.           title: res.data.msg,  
  17.           duration: 2000  
  18.         })  
  19.       } else {  
  20.         that.setData({  
  21.           brandList: res.data.brandList,  
  22.           wordindex: res.data.brandList,  
  23.         });  
  24.         var cData = that.data.brandList;  
  25.         cData[0].wordindex = "#";//先修改json值
  26.         that.setData({ //再set值
  27.           wordindex: cData  
  28.         })  
  29.       }  
  30.     }, function (res) {  
  31.     });  
  32.   },  
  33.   onReady: function () {  
  34.     // 頁面渲染完成
  35.   },  
  36.   onShow: function () {  
  37.     // 頁面顯示
  38.   },  
  39.   onHide: function () {  
  40.     // 頁面隱藏
  41.   },  
  42.   onUnload: function () {  
  43.     // 頁面關閉
  44.   },  
  45.   click: function () {  
  46.     wx.navigateTo({  
  47.       url: '/pages/order/car/add_car/car_model/car_model',  
  48.     })  
  49.   },  
  50.   choiceWordindex: function (event) {  
  51.     let wordindex = event.currentTarget.dataset.wordindex;  
  52.     if (wordindex == '#') {  
  53.       this.setData({  
  54.         toView: '常用品牌',  
  55.       })  
  56.     } else {  
  57.       this.setData({  
  58.         toView: wordindex,  
  59.       })  
  60.     }  
  61.     console.log(this.data.toView);  
  62.   }  
  63. })  

wcss:

  1. /* pages/order/car/add_car/car_brand/car_brand.wxss */
  2. scroll-view {  
  3.   height600px;  
  4.     floatleft;  
  5.   width94%;  
  6. }  
  7. .wordindex {  
  8.   line-height50rpx;  
  9.   background#f7f7f7;  
  10.   font-size14px;  
  11.   color#929292;  
  12.   padding-left28rpx;  
  13. }  
  14. .color_view {  
  15.   text-aligncenter;  
  16.   display: inline-flex;  
  17.   flex-direction: column;  
  18. }  
  19. .brand_block {  
  20.   margin-top28rpx;  
  21.   margin-left28rpx;  
  22. }  
  23. image {  
  24.   width140rpx;  
  25.   height140rpx;  
  26.   margin-right24rpx;  
  27.   margin-bottom24rpx;  
  28.   border-stylesolid;  
  29.   border-width1rpx;  
  30.   border-color#ededed;  
  31. }  
  32. .index {  
  33.   floatright;  
  34.   positionfixed;  
  35.   right: 0;  
  36.   margin-top20%;  
  37.   margin-right10rpx;  
  38.   color#e0004a;  
  39.   font-size11px;  
  40. }  
  41. .index text {  
  42.   display: flex;  
  43.   line-height18rpx;  
  44. }