国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序?qū)崿F(xiàn)滑動(dòng)/點(diǎn)擊切換Tab

這篇具有很好參考價(jià)值的文章主要介紹了微信小程序?qū)崿F(xiàn)滑動(dòng)/點(diǎn)擊切換Tab。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

背景

?? swiper+scroll-view實(shí)現(xiàn)滑動(dòng)/點(diǎn)擊切換Tab,以及scroll-left的使用~

??文末分享源代碼。記得點(diǎn)贊+關(guān)注+收藏!

1.實(shí)現(xiàn)效果

小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端

2.實(shí)現(xiàn)步驟

2.1 scroll-view實(shí)現(xiàn)tab列表

scroll-view:
可滾動(dòng)視圖區(qū)域。使用豎向滾動(dòng)時(shí),需要給scroll-view一個(gè)固定高度,通過 WXSS 設(shè)置 height。組件屬性的長度單位默認(rèn)為px。
scroll-x(boolean):允許橫向滾動(dòng)
scroll-y(boolean):允許縱向滾動(dòng)
scroll-left(number/string):設(shè)置橫向滾動(dòng)條位置
scroll-with-animation(boolean):在設(shè)置滾動(dòng)條位置時(shí)使用動(dòng)畫過渡

  • 定義一個(gè)tab列表,scroll-view包裹,允許橫向滾動(dòng),設(shè)置scroll-left默認(rèn)為0
  • 每個(gè)tab設(shè)置為display: inline-block,scroll-view設(shè)置 white-space: nowrap不換行

小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端

<scroll-view scroll-x class="container-head-sc" scroll-left="{{sleft}}" scroll-with-animation="true">
	 <view class="item" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view>
 </scroll-view>
.container-head-sc {
  height: 50rpx;
  border-radius: 25rpx;
  background: #eeece4;
  color: #333;
  white-space: nowrap;
}

.container-head-sc .item {
  padding: 0 20rpx;
  min-width: 90rpx;
  text-align: center;
  line-height: 50rpx;
  font-size: 26rpx;
  display: inline-block;
  height: 50rpx;
}
  • 給每個(gè)tab設(shè)置vertical-align: top;防止高度塌陷
.container-head-sc .item{
  /* 防止高度塌陷 */
 + vertical-align: top;
}
  • 添加當(dāng)前激活tab樣式,定義當(dāng)前選中項(xiàng)索引currentTab默認(rèn)為0(即選中第一個(gè)),當(dāng)currentTab==列表的某一項(xiàng)索引表示選中
    小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端
 <view class="item {{currentTab == index ?'active':''}}" data-current="{{index}}" catchtap="handleTabChange" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view>
.container-head-sc .active {
  color: #ffffff;
  font-weight: bold;
  background: orange;
  border-radius: 25rpx;
}
  • 添加切換事件
    小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端
handleTabChange(e) {
	let { current } = e.target.dataset;
	if (this.data.currentTab == current || current === undefined) return;
	this.setData({
	  currentTab: current,
	});
},

2.2 swiper+scroll-iew 實(shí)現(xiàn)內(nèi)容列表

swiper:
滑塊視圖容器。默認(rèn)高度為150px;
current(number):當(dāng)前所在滑塊的 index,默認(rèn)為0
autoplay(boolean):是否自動(dòng)切換
bindchange(eventhandle):current 改變時(shí)會(huì)觸發(fā) change 事件,event.detail = {current, source}

  • swiper包裹內(nèi)容列表,需要為swiper指定高度,這里我們?cè)O(shè)置為撐滿一屏

小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端

/* swiper默認(rèn)高度為150px */
.container-swiper {
  height: calc(100% - 110rpx);
}
  • 設(shè)置swiper的current為當(dāng)前選中的tab標(biāo)簽索引,即currentTab
<swiper current="{{currentTab}}"  class="container-swiper">
 <swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'>
 </swiper-item>
</swiper>
  • swiper-item展示內(nèi)容列表,用scroll-view包裹內(nèi)容,設(shè)置豎向滾動(dòng),使用豎向滾動(dòng)時(shí),需要給scroll-view一個(gè)固定高度,這里將scroll-view高度設(shè)置為100%,與swiper同高,鋪滿一屏
<swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'>
  <scroll-view scroll-y class="container-swiper-sc">
     <view class="flex-wrap flex-row items">
     ....//內(nèi)容
     </view>
   </scroll-view>
 </swiper-item>
.container-swiper-sc {
  height: 100%;
}
  • swiper添加bindchange事件,當(dāng)滑動(dòng)時(shí)候,動(dòng)態(tài)的設(shè)置currentTab,實(shí)現(xiàn)tab列表的同步更新
    小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端
<swiper current="{{currentTab}}" bindchange="handleSwiperChange" class="container-swiper">
	....//內(nèi)容
</swiper>
  handleSwiperChange(e) {
    this.setData({
      currentTab: e.detail.current,
    });
  },
  • 可以發(fā)現(xiàn),當(dāng)swiper所在滑塊的 index超出tab列表的可視范圍,我們得手動(dòng)滑動(dòng)tab列表才能看見當(dāng)前所選中的tab
  • 找到2.1節(jié) scroll-left=“{{sleft}}”,scroll-left用來設(shè)置橫向滾動(dòng)條位置,也就是說,我們可以監(jiān)聽swiper的滾動(dòng),在滑塊所在的index改變的時(shí)候,去動(dòng)態(tài)的設(shè)置scroll-left的位置
  • scroll-left的計(jì)算

wx.createSelectorQuery():
返回一個(gè) SelectorQuery 對(duì)象實(shí)例
SelectorQuery.selectAll(string selector):
在當(dāng)前頁面下選擇匹配選擇器 selector 的所有節(jié)點(diǎn)。文章來源地址http://www.zghlxwxcb.cn/news/detail-804613.html

getScrollLeft() {
	const query = wx.createSelectorQuery();
	query.selectAll(".item").boundingClientRect();
	//這里將會(huì)返回頁面中所有class為item的節(jié)點(diǎn),個(gè)數(shù)為tab列表的長度
	query.exec((res) => {
	  let num = 0;
	  for (let i = 0; i < this.data.currentTab; i++) {
	    num += res[0][i].width;
	  }
	  // 計(jì)算當(dāng)前currentTab之前的寬度總和
	  this.setData({
	    sleft: Math.ceil(num),
	  });
	});
},
  • 修改swiper的bindchange事件,每次滑塊的變化,都重新計(jì)算scroll-left的大小
    小程序左右滑動(dòng)切換tab,小程序,微信小程序,微信小程序,小程序,前端
  handleSwiperChange(e) {
   + this.getScrollLeft();
  },

3.實(shí)現(xiàn)代碼

<view class="head flex-row">
  <view class="head-title">scroll-left</view>
</view>
<scroll-view scroll-y class="container">
  <view class="container-head flex-row">
    <scroll-view scroll-x class="container-head-sc" scroll-left="{{sleft}}" scroll-with-animation="true">
      <view class="item {{currentTab == index ?'active':''}}" data-current="{{index}}" catchtap="handleTabChange" wx:key="list" wx:for="{{list}}" wx:for-index="index">tab-{{index+1}} </view>
    </scroll-view>
  </view>
  <swiper current="{{currentTab}}" bindchange="handleSwiperChange" class="container-swiper">
    <swiper-item class="flex-column j_c" wx:for="{{list}}" wx:key='index'>
      <scroll-view scroll-y class="container-swiper-sc">
        <view class="flex-wrap flex-row items">
          <block wx:for="{{item}}" wx:key="index">
            <image src="https://i.postimg.cc/mgsKJGLw/susu1.jpg" mode="aspectFill" class="item-img" />
          </block>
        </view>
      </scroll-view>
    </swiper-item>
  </swiper>
</scroll-view>
page {
  background-color: #ffa500;
  height: 100%;
}
.head {
  height: 90rpx;
  color: #333;
  font-size: 30rpx;
  padding-left: 30rpx;
  font-weight: bold;
  padding-bottom: 10rpx;
  box-sizing: border-box;
}
.head-title {
  position: relative;
  display: inline-block;
  height: 100%;
}
.head-title::after {
  content: '';
  position: absolute;
  z-index: 99;
  width: 15px;
  height: 15px;
  margin-left: -15rpx;
  border-top: 3px solid #333;
  border-right: 3px solid #333;
  border-top-right-radius: 100%;
  transform: rotate(-225deg);
  left: 50%;
  bottom: 3px;
}
.container {
  width: 100%;
  height: calc(100% - 90rpx);
  background-color: #fff;
  overflow: hidden;
  border-radius: 30rpx 30rpx 0 0;
}
.container-head {
  width: 100%;
  height: 110rpx;
  box-sizing: border-box;
  padding: 10rpx 20rpx;
}
.container-head-sc {
  height: 50rpx;
  border-radius: 25rpx;
  background: #eeece4;
  color: #333;
  white-space: nowrap;
}
.container-head-sc .item {
  padding: 0 20rpx;
  min-width: 90rpx;
  text-align: center;
  line-height: 50rpx;
  font-size: 26rpx;
  display: inline-block;
  /* 引起高度塌陷 */
  vertical-align: top;
  height: 50rpx;
}
.container-head-sc .active {
  color: #ffffff;
  font-weight: bold;
  background: orange;
  border-radius: 25rpx;
}
/* swiper默認(rèn)高度為150px */
.container-swiper {
  height: calc(100% - 110rpx);
}
.container-swiper-sc {
  height: 100%;
}
.container-swiper-sc .items {
  padding: 0 2%;
  width: 100%;
  box-sizing: border-box;
}
.container-swiper-sc .items .item-img {
  width: 30vw;
  height: 30vw;
  margin-right: 2.8%;
  margin-bottom: 10rpx;
  flex-shrink: 0;
}
.container-swiper-sc .items .item-img:nth-child(3n+3) {
  margin-right: 0;
}
/* 隱藏scroll-view的滾動(dòng)條 */
::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
Page({
  data: {
    currentTab: 0,
    sleft: "", //橫向滾動(dòng)條位置
    list: [1, 2, 3, 4, 5, 6, 7, 22, 32],//測試列表
  },
  handleTabChange(e) {
    let { current } = e.target.dataset;
    if (this.data.currentTab == current || current === undefined) return;
    this.setData({
      currentTab: current,
    });
  },
  handleSwiperChange(e) {
    this.setData({
      currentTab: e.detail.current,
    });
    this.getScrollLeft();
  },
  getScrollLeft() {
    const query = wx.createSelectorQuery();
    query.selectAll(".item").boundingClientRect();
    query.exec((res) => {
      let num = 0;
      for (let i = 0; i < this.data.currentTab; i++) {
        num += res[0][i].width;
      }
      this.setData({
        sleft: Math.ceil(num),
      });
    });
  },
});

4.寫在最后??

看完本文如果覺得有用,記得點(diǎn)贊+關(guān)注+收藏鴨 ??
更多小程序相關(guān),關(guān)注??蘇蘇的bug,??蘇蘇的github,??蘇蘇的碼云~

到了這里,關(guān)于微信小程序?qū)崿F(xiàn)滑動(dòng)/點(diǎn)擊切換Tab的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Uni-app實(shí)現(xiàn)左右滑動(dòng)頁面內(nèi)容切換(兼容微信小程序)

    提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔 ? ? ? ? 前言 ? ? ? ? 整體思路 ? ? ? ??一、HTML部分 ????????二、Script部分 ????????三、Style部分 ? ? ? ? ? (先聲明哦我可不是偷懶,只是想學(xué)術(shù)借鑒一下)因?yàn)樽罱性谧鲎笥一瑒?dòng)功能,

    2024年02月07日
    瀏覽(112)
  • 微信小程序———同一頁面內(nèi)左右滑動(dòng)切換內(nèi)容顯示

    微信小程序———同一頁面內(nèi)左右滑動(dòng)切換內(nèi)容顯示

    一、微信小程序事件 ?由于首先介紹一下微信小程序中的事件,可選擇快速略過或者直接進(jìn)去之后的重點(diǎn)內(nèi)容 一、什么是事件 事件是視圖層到邏輯層的通訊方式。 事件可以將用戶的行為反饋到邏輯層進(jìn)行處理。 事件可以綁定在組件上,當(dāng)達(dá)到觸發(fā)事件,就會(huì)執(zhí)行邏輯層中對(duì)

    2024年02月03日
    瀏覽(86)
  • 微信小程序選項(xiàng)卡切換(滑動(dòng)切換,點(diǎn)擊切換)

    微信小程序選項(xiàng)卡切換(滑動(dòng)切換,點(diǎn)擊切換)

    效果如下:可點(diǎn)擊切換,滑動(dòng)切換 代碼如下 這個(gè)可以在項(xiàng)目用 index.wxml index.wxss index.js

    2024年02月14日
    瀏覽(89)
  • 微信小程序?qū)崿F(xiàn)左右滑動(dòng)觸發(fā)內(nèi)容及聯(lián)動(dòng)選項(xiàng)卡切換、Math、abs、findIndex、parseInt、String、push、createSelectorQuery、selectAll

    微信小程序?qū)崿F(xiàn)左右滑動(dòng)觸發(fā)內(nèi)容及聯(lián)動(dòng)選項(xiàng)卡切換、Math、abs、findIndex、parseInt、String、push、createSelectorQuery、selectAll

    在寫原生微信小程序項(xiàng)目的時(shí)候,遇到左右滑動(dòng)更新內(nèi)容及聯(lián)動(dòng)選項(xiàng)卡切換的功能。于是就寫了這篇文章,關(guān)于文章的 css 不在此文章中展示,使用了公共的自定義類名,所以通過類名大概就能推敲出 css 的屬性及值。 頁面分為三個(gè)模塊,分別是頂部橫向滾動(dòng)選項(xiàng)卡,底部內(nèi)

    2024年02月09日
    瀏覽(96)
  • uniapp 左右滑動(dòng)切換頁面并切換tab

    uniapp 左右滑動(dòng)切換頁面并切換tab

    實(shí)現(xiàn)效果如圖 要實(shí)現(xiàn)底部內(nèi)部的左右滑動(dòng)切換帶動(dòng)上方tab欄的切換,并且下方內(nèi)容要實(shí)現(xiàn)縱向滾動(dòng) ,所以需要swiper,swiper-item,scroll-view組合使用 tab欄部分 ?tab欄點(diǎn)擊切換,需要重新調(diào)取數(shù)據(jù) 下方內(nèi)容部分 滑動(dòng)切換,改變上方tab欄狀態(tài),并重新調(diào)取數(shù)據(jù) 以上即可實(shí)現(xiàn)頁面左

    2024年02月13日
    瀏覽(28)
  • 微信小程序(二)微信小程序選擇本地圖片顯示并預(yù)覽(實(shí)現(xiàn)左右滑動(dòng))

    微信小程序(二)微信小程序選擇本地圖片顯示并預(yù)覽(實(shí)現(xiàn)左右滑動(dòng))

    在微信小程序里面實(shí)現(xiàn)選擇圖片然后預(yù)覽是一個(gè)非常普遍的功能,在我們上傳圖片文件的時(shí)候,都會(huì)選擇本地的圖片進(jìn)行上傳,在上傳之前會(huì)查看一下自己上傳的圖片是否準(zhǔn)確。所以要做到預(yù)覽圖片。 下面就實(shí)現(xiàn)一個(gè)簡單的本地圖片顯示預(yù)覽的功能~~ 1、創(chuàng)建頁面 這里我直接

    2024年02月03日
    瀏覽(94)
  • 微信小程序?qū)崿F(xiàn)tab切換

    微信小程序?qū)崿F(xiàn)tab切換

    循環(huán)一個(gè)數(shù)組,切換數(shù)據(jù)的時(shí)候根據(jù)index索引來動(dòng)態(tài)的設(shè)置選中項(xiàng),設(shè)置fixed定位,固定在頂部。

    2024年02月16日
    瀏覽(30)
  • 微信小程序?qū)崿F(xiàn)頁面tab切換

    微信小程序?qū)崿F(xiàn)頁面tab切換

    客戶端開發(fā)過程中,實(shí)現(xiàn)頁面切換是一個(gè)很常見的場景,本文將介紹一下微信小程序是如何實(shí)現(xiàn)頁面tab切換的。 小程序一個(gè)頁面相關(guān)的文件結(jié)構(gòu)如下: 話不多說直接上代碼。 (1)index.js,代碼如下: (2)index.wxml,代碼如下: (3)index.wxss,代碼如下: 運(yùn)行結(jié)果如下: 本文主要介

    2024年02月04日
    瀏覽(96)
  • 【微信小程序】實(shí)現(xiàn)頁面tab切換效果

    【微信小程序】實(shí)現(xiàn)頁面tab切換效果

    目錄 前言 本次效果展示 一、如何實(shí)現(xiàn)頁面tab 1.使用內(nèi)置組件scroll-view 2.實(shí)現(xiàn)點(diǎn)擊時(shí)出現(xiàn)的背景樣式 3.使用scroll-into-view,實(shí)現(xiàn)點(diǎn)擊時(shí)自動(dòng)滾動(dòng) 本次主要內(nèi)容是介紹頁面tab的開發(fā),如何實(shí)現(xiàn)tab與頁面內(nèi)容聯(lián)動(dòng)呢?關(guān)注我就知道! ? 如下圖所示,我們需要使用到紅色框框中的屬

    2024年02月09日
    瀏覽(505)
  • 微信小程序?qū)崿F(xiàn)tab切換和數(shù)據(jù)列表

    微信小程序?qū)崿F(xiàn)tab切換和數(shù)據(jù)列表

    上篇文章介紹了微信小程序?qū)崿F(xiàn)tab切換的一種方案(參考 https://blog.51cto.com/baorant24/6188157 ),感覺代碼不是很精簡,本文再用一個(gè)demo介紹微信小程序如何實(shí)現(xiàn)tab切換和數(shù)據(jù)列表。 微信小程序?qū)?yīng)頁面文件結(jié)構(gòu)如下: 話不多說,直接上代碼: (1)index.js文件,代碼如下: (2)index.

    2024年02月05日
    瀏覽(93)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包