亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng)效果

 更新時(shí)間:2021年09月24日 11:53:44   作者:朵拉。  
這篇文章主要為大家詳細(xì)介紹了微信小程序scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

微信小程序利用scroll-view實(shí)現(xiàn)左右聯(lián)動(dòng),供大家參考,具體內(nèi)容如下

點(diǎn)擊左邊的按鈕時(shí),右邊可以跳動(dòng)到指定的位置

  • 首先要注意使用scroll-view豎屏滾動(dòng),需要給scroll-view固定高度
  • 其次在點(diǎn)擊時(shí),需要給需要滾動(dòng)的scroll-view加上scroll-into-view,其值應(yīng)該是子元素的id,且id不能以數(shù)字 開頭

滾動(dòng)右邊,左邊菜單跳到相應(yīng)的位置

  • 其實(shí)現(xiàn)的思想是,在右邊滾動(dòng)屏滾動(dòng)時(shí),得到滾動(dòng)的距離。將右邊滾動(dòng)屏中各模塊到達(dá)頂部的距離計(jì)算出來放到一個(gè)數(shù)組中。第一個(gè)模塊的滾動(dòng)距離是本身的高度,第二個(gè)模塊的滾動(dòng)距離是第一個(gè)模塊的高度加上自身的高度,以此類推。滾動(dòng)時(shí),判斷滾動(dòng)距離在保存好的數(shù)組中的哪個(gè)階段,并以此得出符合條件的下標(biāo)值,將左側(cè)菜單對(duì)應(yīng)的下標(biāo)中的值做改動(dòng),就可以實(shí)現(xiàn)左右聯(lián)動(dòng)。
  • 計(jì)算各模塊的高度時(shí),獲取元素需要使用wx.createSelectorQuery(),其返回selectorQuerys對(duì)象實(shí)例;再利用返回來的節(jié)點(diǎn)的boundingClientRect(function callback)方法獲取節(jié)點(diǎn)的布局位置信息,在SelectorQuery.exec()執(zhí)行后,將信息返回在回調(diào)函數(shù)中。本文中將獲取元素高度的方法寫在了onload中。

實(shí)現(xiàn)效果圖:

主要代碼如下:

index.wxml

<view class="container">
  <view class="category-left">
    <scroll-view scroll-y="true" style="height:100%">
      <block wx:for="{{category}}" wx:key="id">
       <view class="catgegory-item {{activeId === item.id?'active-item':''}}" data-id="{{item.id}}" bindtap="clickItem">{{item.name}}</view>
      </block>
    </scroll-view>
  </view>
  <view class="category-right">
    <scroll-view scroll-y="true" style="height:100%" scroll-into-view="{{toView}}" scroll-with-animation="ture" bindscroll="scroll">
      <view class="categoty-detail">
      <block wx:for="{{content}}" wx:key="id">
        <view class="catefory-main">
          <view class="category-title" id="{{item.id}}">{{item.title}}</view>
          <view class="category-content">
              <view class="content-item" wx:for="{{item.options}}" wx:for-item="i" wx:key="id">
                <image src="{{i.src}}"></image>
                <text>{{i.text}}</text>                                                      
              </view>
          </view>
        </view> 
      </block>
      </view>
    </scroll-view>
  </view>
</view>

index.js

//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()

Page({
  data: {
    toView: 'a1',
    activeId: 'a1',
    category: [
      {name: '新品', id: 'a1'},
      { name: '眾籌', id: 'a2' },
      { name: '小米手機(jī)', id: 'a3' },
      { name: 'redmi手機(jī)', id: 'a4' },
      { name: '黑鯊游戲', id: 'a5' },
      { name: "手機(jī)配件", id: 'a6' },
      { name: '電視', id: 'a7'},
      { name: '電腦', id: 'a8' },
    ],
    content: [
      {
        title: '- 新品 -', 
        options: [
          { src: '../../image/redmi.png',id: '001',text: 'redmi8'},
          { src: '../../image/redmi.png', id: '002', text: 'redmi8A' },
          { src: '../../image/redmi.png', id: '003', text: '小米9pro 5G'},
          { src: '../../image/redmi.png', id: '004', text: 'redmi8'},
          { src: '../../image/redmi.png', id: '005',text: 'redmi8' }
        ],
        id: 'a1'
      },
      {
        title: '- 眾籌 -',
        options: [
          { src: '../../image/zhongchou.png', id: '006', text: 'redmi8' },
          { src: '../../image/zhongchou.png', id: '007' ,text: 'redmi8'},
          { src: '../../image/zhongchou.png', id: '008', text: 'redmi8' },
          { src: '../../image/zhongchou.png', id: '009',text: 'redmi8' }
        ],
        id: 'a2'
      },
      {
        title: '- 小米手機(jī) -',
        options: [
          { src: '../../image/xiaomi.png', id: '006', text: 'redmi8' },
          { src: '../../image/xiaomi.png', id: '007', text: 'redmi8' },
          { src: '../../image/xiaomi.png', id: '008', text: 'redmi8' },
          { src: '../../image/xiaomi.png', id: '009', text: 'redmi8' }
        ],
         id: 'a3'
      },
      {
        title: '- redmi手機(jī) -',
        options: [
          { src: '../../image/hongmi.png', id: '006', text: 'redmi8' },
          { src: '../../image/hongmi.png', id: '007', text: 'redmi8' },
          { src: '../../image/hongmi.png', id: '008', text: 'redmi8' },
          { src: '../../image/hongmi.png', id: '009', text: 'redmi8' }
        ],
        id: 'a4'
      }

    ],
  },
  //事件處理函數(shù)
  onLoad: function () {
    this.setData({
      toView: 'a1',
      heightArr: []
    })
    let query = wx.createSelectorQuery();
    query.selectAll('.catefory-main').boundingClientRect((rect)=> {
      rect.forEach(ele => {
        this.calculateHeight(ele.height);
      })
    }).exec();
  },
  clickItem(e) {
    this.setData({
      activeId: e.currentTarget.dataset.id,
      toView: e.currentTarget.dataset.id
    })
  },
  scroll(e) {
    let scrollHeight = e.detail.scrollTop;
    let index = this.calculateIndex(this.data.heightArr,scrollHeight);
    this.setData({
      activeId: 'a'+index
    })

  },
  // 計(jì)算滾動(dòng)的區(qū)間
  calculateHeight(height) {
    if(!this.data.heightArr.length) {
      this.data.heightArr.push(height)
    }else {
      this.data.heightArr.forEach(ele => {
        height += ele
      })
      this.data.heightArr.push(height);
    }
  },
  // 計(jì)算左邊選中的下標(biāo)
  calculateIndex(arr, scrollHeight) {
    let index= '';
    for(let i =0;i<arr.length;i++) {
      if (scrollHeight >= 0 && scrollHeight < arr[0]){
        index = 0;
      }else if(scrollHeight >= arr[i-1] && scrollHeight < arr[i]){
        index = i;
      }
    }
    return index+1;
  }
})

index.wxss

/**index.wxss**/
.container {
  padding: 0;
  width:100%;
  height: 100vh;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
}
.category-left {
  height: 100%;
  width: 22%;
  padding: 0 20rpx;
  box-sizing: border-box;
  border-right: 1px solid #efefef;
}
.catgegory-item {
  padding: 20rpx 0;
  font-size: 30rpx;
  text-align:  center;
}
.active-item {
  color: orange;
}
.category-right {
  flex:1;
  height: 100%;
}
.category-content {
  display: grid;
  grid-template-columns: repeat(auto-fill, 190rpx);
}
.category-title {
  text-align: center;
}
.content-item {
  display: flex;
  flex-direction: column;
  padding: 20rpx;
  text-align: center;
  font-size: 30rpx;
}
.content-item image{
  width: 120rpx;
  height: 120rpx;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JS簡(jiǎn)單獲取并修改input文本框內(nèi)容的方法示例

    JS簡(jiǎn)單獲取并修改input文本框內(nèi)容的方法示例

    這篇文章主要介紹了JS簡(jiǎn)單獲取并修改input文本框內(nèi)容的方法,結(jié)合實(shí)例形式分析了JavaScript針對(duì)頁(yè)面元素的獲取、賦值等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • JavaScript中innerHTML,innerText,outerHTML的用法及區(qū)別

    JavaScript中innerHTML,innerText,outerHTML的用法及區(qū)別

    在javascript中如果我們要獲取對(duì)象內(nèi)容,js為我們提供了三種方法outerhtml、innerhtml和innertext,但他們之間具體怎么使用與具體的區(qū)別在哪里,可能很多人不知道吧,接下來跟著小編一起來學(xué)習(xí)innerHTML,innerText,outerHTML的用法及區(qū)別吧。
    2015-09-09
  • JS實(shí)現(xiàn)的自定義map方法示例

    JS實(shí)現(xiàn)的自定義map方法示例

    這篇文章主要介紹了JS實(shí)現(xiàn)的自定義map方法,結(jié)合實(shí)例形式分析了javascript自定義map相關(guān)的json數(shù)組定義、遍歷、添加、刪除、讀取等相關(guān)操作技巧,需要的朋友可以參考下
    2019-05-05
  • 原生JS實(shí)現(xiàn)加入收藏夾的代碼

    原生JS實(shí)現(xiàn)加入收藏夾的代碼

    加入收藏夾功能在網(wǎng)上可以搜索到很多相關(guān)的文章,本文要為大家介紹的是如何使用原生js來實(shí)現(xiàn)所謂的收藏,感興趣的朋友可以參考下
    2013-10-10
  • 原生js實(shí)現(xiàn)給指定元素的后面追加內(nèi)容

    原生js實(shí)現(xiàn)給指定元素的后面追加內(nèi)容

    給指定元素的后面追加一些自己想要實(shí)現(xiàn)的效果或者是一些屬性之類的,有木有遇到過,接下來為大家詳細(xì)介紹下追加的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈
    2013-04-04
  • 使用typescript類型來實(shí)現(xiàn)快排詳情

    使用typescript類型來實(shí)現(xiàn)快排詳情

    這篇文章主要介紹了使用typescript類型來實(shí)現(xiàn)快排詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 第二章之Bootstrap 頁(yè)面排版樣式

    第二章之Bootstrap 頁(yè)面排版樣式

    Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的,它簡(jiǎn)潔靈活,使得 Web 開發(fā)更加快捷。本文給大家介紹Bootstrap 頁(yè)面排版樣式的相關(guān)知識(shí),希望對(duì)大家有所幫助!
    2016-04-04
  • HTML Color Picker(js拾色器效果)

    HTML Color Picker(js拾色器效果)

    這篇文章主要是用js實(shí)現(xiàn)拾色器效果,來自國(guó)外的網(wǎng)站,喜歡的朋友可以參考下
    2013-08-08
  • 詳解小程序用戶登錄狀態(tài)檢查與更新實(shí)例

    詳解小程序用戶登錄狀態(tài)檢查與更新實(shí)例

    這篇文章主要介紹了小程序用戶登錄狀態(tài)檢查與更新實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 微信小程序swiper組件用法實(shí)例分析【附源碼下載】

    微信小程序swiper組件用法實(shí)例分析【附源碼下載】

    這篇文章主要介紹了微信小程序swiper組件用法,結(jié)合實(shí)例形式分析了微信小程序中swiper組件的功能、使用方法,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下
    2017-12-12

最新評(píng)論