亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
1、背景
2、實現(xiàn)
2.1 原理介紹
2.1 頁面布局代碼
2.2 樣式代碼
2.3 邏輯代碼
首頁 微信小程序 小程序開發(fā) 淺談小程序怎么實現(xiàn)列表滾動上下聯(lián)動效果

淺談小程序怎么實現(xiàn)列表滾動上下聯(lián)動效果

Dec 16, 2021 am 10:30 AM
小程序

小程序怎么實現(xiàn)列表滾動上下聯(lián)動效果?下面本篇文章給大家介紹一下微信小程序開發(fā)列表滾動上下聯(lián)動效果的方法,希望對大家有所幫助!

淺談小程序怎么實現(xiàn)列表滾動上下聯(lián)動效果

1、背景

最近在做公司的一款小程序,其中有一塊的設(shè)計的是在列表做上下滾動的是時候,頂部的tab欄跟著一起聯(lián)動,當(dāng)點擊tab欄的時候,列表數(shù)據(jù)也跟隨聯(lián)動。

下面是實現(xiàn)的一個效果圖:

淺談小程序怎么實現(xiàn)列表滾動上下聯(lián)動效果

頂部的頭部區(qū)域不跟隨列表滾動; 頭部區(qū)域以下屬于滾動區(qū)域。

2、實現(xiàn)

2.1 原理介紹

這個地方的實現(xiàn)主要借助了微信小程序原生的scroll-view組件。

使用它的 scroll-into-view 屬性,可以實現(xiàn)點擊頂部的tab欄,將頁面滾動到指定的列表位置;

使用 bindscroll 事件,可以知道當(dāng)前頁面滾動的距離,根據(jù)滾動的距離做tab欄的切換操作;

2.1 頁面布局代碼

先說下界面的整體布局,主要分為兩部分,頭部固定區(qū)域 可滾動列表區(qū)域。

可滾動的列表區(qū)域的標(biāo)題欄當(dāng)滾動一定的距離后,它也要固定在頂部。

代碼實現(xiàn):

<!--index.wxml-->
<view class="list">

<!--頂部固定區(qū)域-->
<view style="height: 88rpx;width: 100%;background-color: burlywood;text-align: center;">頭部區(qū)域</view>

<!--可滾動區(qū)域-->
<scroll-view scroll-y="true" style="width: 100%; height: {{scrollAreaHeight}}px;" bindscroll="scroll" scroll-into-view="{{scrollToItem}}" scroll-with-animation="true"  scroll-top="{{scrollTop}}">

   <!--水平滾動的tab欄-->
  <scroll-view scroll-x="true" style="height: 88rpx;width: 100%;">
  <view class="head-area {{float ? &#39;head-float&#39; : &#39;&#39;}}" >
    <view class="head-area-item {{curSelectTab === index ? &#39;head-area-item-select&#39; : &#39;&#39;}}" wx:for="{{appGroupList}}" bindtap="tabClick" data-index="{{index}}">
    {{item.name}}
  </view>
  </view>

  </scroll-view>

<!--數(shù)據(jù)列表-->
<view class="list-group" style="height: {{listGroupHeight}}px;">
  <view class="list-group-item" id="v_{{index}}" wx:for="{{appGroupList}}" data-index="{{index}}">
    <view class="group-name">
      {{item.name}}
    </view>
    <view class="group-children" >
      <view wx:for="{{item.children}}" class="group-children-item" style="width: {{itemWidth}}px;">
      <image src="{{item.url}}"></image>
      <view>{{item.name}}</view>
    </view>
    </view>

  </view>
</view> 
</scroll-view>

</view>

在布局代碼中有幾個點需要注意:

1、scrollAreaHeight 滾動區(qū)域的高度計算。 --- 通過獲取當(dāng)前設(shè)備的窗口高度減去頂部固定區(qū)域的高度

2、水平tab欄是否置頂。 --- 根據(jù)頁面的滾動距離來判斷,如果滾動距離 大于或者等于 水平tab欄的高度,則置頂;

3、設(shè)置數(shù)據(jù)列表的id="v_{{index}}" id,后續(xù)點擊tab欄滾動到指定的位置就是根據(jù)這個id去實現(xiàn)的。

2.2 樣式代碼

/**index.wxss**/
.list{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.head-area{
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  height: 88rpx;
  width: 100%;
  padding: 0 10;
}

.head-area-item{
  display: flex;
  height: 88rpx;
  text-align: center;
  width: 150rpx;
  align-items: center;
  justify-content: center;
}

.head-area-item-select{
  color: #09bb07;
}

image{
  width: 88rpx;
  height: 88rpx;
}

.list-group{
  display: flex;
  width: 100%;
  height: 1000%;
  flex-direction: column;
}

.list-group-item{
  display: flex;
  width: 100%;
  background-color: #aaa;
  flex-direction: column;
}

.group-name{
  height: 88rpx;
  display: flex;
  text-align: center;
  align-items: center;
  margin-left: 20rpx;
}

.group-children{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
}

.group-children-item{
  height: 160rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.head-float{
  position: fixed;
  top: 88rpx;
  background-color: #ffffff;
}

2.3 邏輯代碼

// index.js
Page({
  heightArr: [],
  //記錄scroll-view滾動過程中距離頂部的高度
  distance: 0,
  data: {
    appGroupList:[
      {name:"分組01",children:[{"name":"測試0","url":"/images/bluetooth.png"},
      {"name":"測試1","url":"/images/bluetooth.png"},
      {"name":"測試2","url":"/images/bluetooth.png"},
      {"name":"測試3","url":"/images/bluetooth.png"},
      {"name":"測試4","url":"/images/bluetooth.png"},
      {"name":"測試5","url":"/images/bluetooth.png"},
      {"name":"測試6","url":"/images/bluetooth.png"},
      {"name":"測試7","url":"/images/bluetooth.png"}]},
      {name:"分組02",children:[{"name":"測試0","url":"/images/bluetooth.png"},
      {"name":"測試1","url":"/images/bluetooth.png"},
      {"name":"測試2","url":"/images/bluetooth.png"},
      {"name":"測試3","url":"/images/bluetooth.png"},
      {"name":"測試4","url":"/images/bluetooth.png"},
      {"name":"測試5","url":"/images/bluetooth.png"},
      {"name":"測試6","url":"/images/bluetooth.png"},
      {"name":"測試7","url":"/images/bluetooth.png"}]},
      {name:"分組03",children:[{"name":"測試0","url":"/images/bluetooth.png"},
      {"name":"測試1","url":"/images/bluetooth.png"},
      {"name":"測試2","url":"/images/bluetooth.png"},
      {"name":"測試3","url":"/images/bluetooth.png"},
      {"name":"測試4","url":"/images/bluetooth.png"},
      {"name":"測試5","url":"/images/bluetooth.png"},
      {"name":"測試6","url":"/images/bluetooth.png"},
      {"name":"測試7","url":"/images/bluetooth.png"}]},
      {name:"分組04",children:[{"name":"測試0","url":"/images/bluetooth.png"},
      {"name":"測試1","url":"/images/bluetooth.png"},
      {"name":"測試2","url":"/images/bluetooth.png"},
      {"name":"測試3","url":"/images/bluetooth.png"},
      {"name":"測試4","url":"/images/bluetooth.png"},
      {"name":"測試5","url":"/images/bluetooth.png"},
      {"name":"測試6","url":"/images/bluetooth.png"},
      {"name":"測試7","url":"/images/bluetooth.png"}]},
      {name:"分組05",children:[{"name":"測試0","url":"/images/bluetooth.png"},
      {"name":"測試1","url":"/images/bluetooth.png"},
      {"name":"測試2","url":"/images/bluetooth.png"},
      {"name":"測試3","url":"/images/bluetooth.png"},
      {"name":"測試4","url":"/images/bluetooth.png"},
      {"name":"測試5","url":"/images/bluetooth.png"},
      {"name":"測試6","url":"/images/bluetooth.png"},
      {"name":"測試7","url":"/images/bluetooth.png"}]},
    ],
    itemWidth: wx.getSystemInfoSync().windowWidth / 4,
    scrollAreaHeight:wx.getSystemInfoSync().windowHeight - 44,
    float:false,
    curSelectTab:0,
    scrollToItem:null,
    scrollTop: 0, //到頂部的距離
    listGroupHeight:0,
  },

  onReady: function () {
    this.cacluItemHeight();
  },

  scroll:function(e){
    console.log("scroll:",e);
    if(e.detail.scrollTop>=44){
      this.setData({
        float : true
      })
    } else if(e.detail.scrollTop<44) {
      this.setData({
        float : false
      })
    }
    let scrollTop = e.detail.scrollTop;
    let current = this.data.curSelectTab;
    if (scrollTop >= this.distance) {
      //頁面向上滑動
      //列表當(dāng)前可視區(qū)域最底部到頂部的距離 超過 當(dāng)前列表選中項距頂部的高度(且沒有下標(biāo)越界),則更新tab欄
      if (current + 1 < this.heightArr.length && scrollTop >= this.heightArr[current]) {
        this.setData({
          curSelectTab: current + 1
        })
      }
    } else { 
      //頁面向下滑動
      //如果列表當(dāng)前可視區(qū)域最頂部到頂部的距離 小于 當(dāng)前列表選中的項距頂部的高度,則切換tab欄的選中項
      if (current - 1 >= 0 && scrollTop < this.heightArr[current - 1]) {
        this.setData({
          curSelectTab: current - 1
        })
      }
    }
    //更新到頂部的距離
    this.distance = scrollTop;
  },

  tabClick(e){
    this.setData({
      curSelectTab: e.currentTarget.dataset.index,
      scrollToItem: "v_"+e.currentTarget.dataset.index
    })
  },

  //計算每一個item高度
  cacluItemHeight() {
    let that = this;
    this.heightArr = [];
    let h = 0;
    const query = wx.createSelectorQuery();
    query.selectAll(&#39;.list-group-item&#39;).boundingClientRect()
    query.exec(function(res) {
      res[0].forEach((item) => {
        h += item.height;
        that.heightArr.push(h);
      })
      console.log(that.heightArr);
      that.setData({
        listGroupHeight: that.heightArr[that.heightArr.length - 1 ]
      })
    })
  },
})

在邏輯代碼中最主要的有兩個地方:

1、cacluItemHeight ?計算列表中item的高度數(shù)組,并將最終計算的結(jié)果保存在 heightArr數(shù)組中。

heightArr數(shù)組中的每一項的值是在前一項的基礎(chǔ)之上進行累加。

2、scroll 中判斷當(dāng)前的滾動方向,根據(jù)滾動判斷當(dāng)前的方向,然后根據(jù)滾動的距離設(shè)置當(dāng)前選擇的tab。

好了,就這么多,基于以上的內(nèi)容基本可以實現(xiàn)想要的滾動聯(lián)動、切換tab聯(lián)動效果。

【相關(guān)學(xué)習(xí)推薦:小程序開發(fā)教程

以上是淺談小程序怎么實現(xiàn)列表滾動上下聯(lián)動效果的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
使用Python開發(fā)微信小程序 使用Python開發(fā)微信小程序 Jun 17, 2023 pm 06:34 PM

隨著移動互聯(lián)網(wǎng)技術(shù)和智能手機的普及,微信成為了人們生活中不可或缺的一個應(yīng)用。而微信小程序則讓人們可以在不需要下載安裝應(yīng)用的情況下,直接使用小程序來解決一些簡單的需求。本文將介紹如何使用Python來開發(fā)微信小程序。一、準(zhǔn)備工作在使用Python開發(fā)微信小程序之前,需要安裝相關(guān)的Python庫。這里推薦使用wxpy和itchat這兩個庫。wxpy是一個微信機器

實現(xiàn)微信小程序中的卡片翻轉(zhuǎn)特效 實現(xiàn)微信小程序中的卡片翻轉(zhuǎn)特效 Nov 21, 2023 am 10:55 AM

實現(xiàn)微信小程序中的卡片翻轉(zhuǎn)特效在微信小程序中,實現(xiàn)卡片翻轉(zhuǎn)特效是一種常見的動畫效果,可以提升用戶體驗和界面交互的吸引力。下面將具體介紹如何在微信小程序中實現(xiàn)卡片翻轉(zhuǎn)的特效,并提供相關(guān)代碼示例。首先,需要在小程序的頁面布局文件中定義兩個卡片元素,一個用于顯示正面內(nèi)容,一個用于顯示背面內(nèi)容,具體示例代碼如下:&lt;!--index.wxml--&gt;&l

支付寶上線'漢字拾光-生僻字”小程序,用于征集、補充生僻字庫 支付寶上線'漢字拾光-生僻字”小程序,用于征集、補充生僻字庫 Oct 31, 2023 pm 09:25 PM

本站10月31日消息,今年5月27日,螞蟻集團宣布啟動“漢字拾光計劃”,最近又迎來新進展:支付寶上線“漢字拾光-生僻字”小程序,用于向社會征集生僻字,補充生僻字庫,同時提供不同的生僻字輸入體驗,以幫助完善支付寶內(nèi)的生僻字輸入方法。目前,用戶搜索“漢字拾光”、“生僻字”等關(guān)鍵詞就可以進入“生僻字”小程序。在小程序里,用戶可以提交尚未被系統(tǒng)識別錄入的生僻字圖片,支付寶工程師在確認(rèn)后,將會對字庫進行補錄入。本站注意到,用戶還可以在小程序體驗最新的拆字輸入法,這一輸入法針對讀音不明確的生僻字設(shè)計。用戶拆

小程序能用react嗎 小程序能用react嗎 Dec 29, 2022 am 11:06 AM

小程序能用react,其使用方法:1、基于“react-reconciler”實現(xiàn)一個渲染器,生成一個DSL;2、創(chuàng)建一個小程序組件,去解析和渲染DSL;3、安裝npm,并執(zhí)行開發(fā)者工具中的構(gòu)建npm;4、在自己的頁面中引入包,再利用api即可完成開發(fā)。

uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換 uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換 Oct 20, 2023 pm 02:12 PM

uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換,需要具體代碼示例近年來,隨著移動互聯(lián)網(wǎng)的發(fā)展和智能手機的普及,小程序和H5成為了不可或缺的應(yīng)用形式。而uniapp作為一個跨平臺的開發(fā)框架,可以在一套代碼的基礎(chǔ)上,快速實現(xiàn)小程序和H5的轉(zhuǎn)換,大大提高了開發(fā)效率。本文將介紹uniapp如何實現(xiàn)小程序和H5的快速轉(zhuǎn)換,并給出具體的代碼示例。一、uniapp簡介unia

教你如何在小程序中用公眾號模板消息(附詳細(xì)思路) 教你如何在小程序中用公眾號模板消息(附詳細(xì)思路) Nov 04, 2022 pm 04:53 PM

本篇文章給大家?guī)砹岁P(guān)于微信小程序的相關(guān)問題,其中主要介紹了如何在小程序中用公眾號模板消息,下面一起來看一下,希望對大家有幫助。

用Python編寫簡單的聊天程序教程 用Python編寫簡單的聊天程序教程 May 08, 2023 pm 06:37 PM

實現(xiàn)思路x01服務(wù)端的建立首先,在服務(wù)端,使用socket進行消息的接受,每接受一個socket的請求,就開啟一個新的線程來管理消息的分發(fā)與接受,同時,又存在一個handler來管理所有的線程,從而實現(xiàn)對聊天室的各種功能的處理x02客戶端的建立客戶端的建立就要比服務(wù)端簡單多了,客戶端的作用只是對消息的發(fā)送以及接受,以及按照特定的規(guī)則去輸入特定的字符從而實現(xiàn)不同的功能的使用,因此,在客戶端這里,只需要去使用兩個線程,一個是專門用于接受消息,一個是專門用于發(fā)送消息的至于為什么不用一個呢,那是因為,只

PHP與小程序的地理位置定位與地圖顯示 PHP與小程序的地理位置定位與地圖顯示 Jul 04, 2023 pm 04:01 PM

PHP與小程序的地理位置定位與地圖顯示地理位置定位與地圖顯示在現(xiàn)代科技中已經(jīng)成為了必備的功能之一。隨著移動設(shè)備的普及,人們對于定位和地圖顯示的需求也越來越高。在開發(fā)過程中,PHP和小程序是常見的兩種技術(shù)選擇。本文將為大家介紹PHP與小程序中的地理位置定位與地圖顯示的實現(xiàn)方法,并附上相應(yīng)的代碼示例。一、PHP中的地理位置定位在PHP中,我們可以使用第三方地理位

See all articles