欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序滾動Tab實現(xiàn)左右可滑動切換

 更新時間:2017年08月17日 14:21:41   投稿:lqh  
這篇文章主要介紹了微信小程序滾動Tab實現(xiàn)左右可滑動切換的相關(guān)資料,這里提供實現(xiàn)實例幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下

 微信小程序滾動Tab實現(xiàn)左右可滑動切換

效果:

最終效果如上。問題:

1、tab標(biāo)題總共8個,所以一屏無法全部顯示。
2、tab內(nèi)容區(qū)左右滑動切換時,tab標(biāo)題隨即做標(biāo)記(active)。
3、當(dāng)active的標(biāo)題不在當(dāng)前屏顯示時,要使其能顯示到當(dāng)前屏中。

一、wxml結(jié)構(gòu)

tab標(biāo)題因一排八個,所以使用 scroll-view組件,使其可橫向滾動。

tab內(nèi)容可左右滑動切換,使用swiper組件實現(xiàn)

為了偷懶,所以數(shù)據(jù)都通過wx:for遍歷重復(fù)出來。

說明:

1、設(shè)置data-current屬性用于:點擊當(dāng)前項時,通過點擊事件swichNav中處理e.dataset.current取到點擊的目標(biāo)值。
2、swiper組件的current組件用于控制當(dāng)前顯示哪一頁
3、swiper組件綁定change事件switchTab,通過e.detail.current拿到當(dāng)前頁

<view >
  <scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
    <view class="tab-item {{currentTab==0?'active':''}}" data-current="0" bindtap="swichNav">健康</view>
    <view class="tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="swichNav">情感</view>
    <view class="tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="swichNav">職場</view>
    <view class="tab-item {{currentTab==3?'active':''}}" data-current="3" bindtap="swichNav">育兒</view>
    <view class="tab-item {{currentTab==4?'active':''}}" data-current="4" bindtap="swichNav">糾紛</view>
    <view class="tab-item {{currentTab==5?'active':''}}" data-current="5" bindtap="swichNav">青蔥</view>
    <view class="tab-item {{currentTab==6?'active':''}}" data-current="6" bindtap="swichNav">全部</view>
    <view class="tab-item {{currentTab==7?'active':''}}" data-current="7" bindtap="swichNav">其他</view>
  </scroll-view>
  <swiper class="tab-content" current="{{currentTab}}" duration="300" bindchange="switchTab"
   style="height:{{winHeight}}rpx">
    <swiper-item wx:for="{{[0,1,2,3,4,5,6,7]}}">
      <scroll-view scroll-y="true" class="scoll-h" >
        <block wx:for="{{[1,2,3,4,5,6,7,8]}}" wx:key="*this">
          <view class="item-ans">
            <view class="avatar">
              <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
            </view>
            <view class="expertInfo">
              <view class="name">歡顏</view>
              <view class="tag">知名情感博主</view>
              <view class="answerHistory">134個回答,2234人聽過 </view>
            </view>
            <navigator url="/pages/askExpert/expertDetail" class="askBtn">問TA</navigator> 
          </view>
        </block>
      </scroll-view>
    </swiper-item>
  </swiper>
</view>

二、js部分

微信小程序在開發(fā)起來,個人感覺挺像vue的,以數(shù)據(jù)驅(qū)動視圖的更新。所以在小程序中,不能直接操作dom,當(dāng)然也不能使用jquery之類的庫。

var app = getApp();
Page({
  data:{
    winHeight:"",//窗口高度
    currentTab:0, //預(yù)設(shè)當(dāng)前項的值
    scrollLeft:0, //tab標(biāo)題的滾動條位置
    expertList:[{ //假數(shù)據(jù)
      img:"avatar.png",
      name:"歡顔",
      tag:"知名情感博主",
      answer:134,
      listen:2234
    }]
  },
  // 滾動切換標(biāo)簽樣式
  switchTab:function(e){
    this.setData({
      currentTab:e.detail.current
    });
    this.checkCor();
  },
  // 點擊標(biāo)題切換當(dāng)前頁時改變樣式
  swichNav:function(e){
    var cur=e.target.dataset.current;
    if(this.data.currentTaB==cur){return false;}
    else{
      this.setData({
        currentTab:cur
      })
    }
  },
  //判斷當(dāng)前滾動超過一屏?xí)r,設(shè)置tab標(biāo)題滾動條。
  checkCor:function(){
   if (this.data.currentTab>4){
    this.setData({
     scrollLeft:300
    })
   }else{
    this.setData({
     scrollLeft:0
    })
   }
  },
  onLoad: function() { 
    var that = this; 
    // 高度自適應(yīng)
    wx.getSystemInfo( { 
      success: function( res ) { 
        var clientHeight=res.windowHeight,
          clientWidth=res.windowWidth,
          rpxR=750/clientWidth;
       var calc=clientHeight*rpxR-180;
        console.log(calc)
        that.setData( { 
          winHeight: calc 
        }); 
      } 
    });
  }, 
  footerTap:app.footerTap
})

三、wxss樣式

.tab-h{
  height: 80rpx;width: 100%; box-sizing: border-box;overflow: hidden;line-height: 80rpx;background: #F7F7F7; font-size: 16px; white-space: nowrap;position: fixed;top: 0; left: 0; z-index: 99;}
.tab-item{margin:0 36rpx;display: inline-block;}
.tab-item.active{color: #4675F9;position: relative;}
.tab-item.active:after{ content: "";display: block;height: 8rpx;width: 52rpx;background: #4675F9;position: absolute; bottom: 0;left: 5rpx;border-radius: 16rpx;}
.item-ans{ width: 100%;display: flex; flex-grow: row no-wrap;justify-content: space-between; padding: 30rpx;box-sizing: border-box; height: 180rpx;align-items: center;border-bottom: 1px solid #F2F2F2;}
.avatar{width: 100rpx;height: 100rpx;position: relative;padding-right: 30rpx;}
.avatar .img{width: 100%;height: 100%;}
.avatar .doyen{width: 40rpx;height: 40rpx;position: absolute;bottom: -2px;right: 20rpx;}
.expertInfo{font-size: 12px;flex-grow: 2;color: #B0B0B0;line-height: 1.5em;}
.expertInfo .name{font-size: 16px;color:#000;margin-bottom: 6px;}
.askBtn{ width: 120rpx;height: 60rpx;line-height: 60rpx;text-align: center;font-size: 14px; border-radius: 60rpx;border: 1px solid #4675F9; color:#4675F9;}
.tab-content{margin-top: 80rpx;}
.scoll-h{height: 100%;}

 如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助大家,謝謝大家對本站的支持!

  • JS前端canvas交互實現(xiàn)拖拽旋轉(zhuǎn)及縮放示例

    JS前端canvas交互實現(xiàn)拖拽旋轉(zhuǎn)及縮放示例

    這篇文章主要為大家介紹了JS前端canvas交互實現(xiàn)拖拽旋轉(zhuǎn)及縮放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 微信小程序(二十)slider組件詳細(xì)介紹

    微信小程序(二十)slider組件詳細(xì)介紹

    這篇文章主要介紹了 微信小程序(二十)slider組件詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • 9種使用Chrome Firefox 自帶調(diào)試工具調(diào)試javascript技巧

    9種使用Chrome Firefox 自帶調(diào)試工具調(diào)試javascript技巧

    這篇文章主要介紹了9種使用Chrome Firefox 自帶網(wǎng)頁調(diào)試工具調(diào)試javascript技巧
    2017-12-12
  • 淺談TypeScript 索引簽名的理解

    淺談TypeScript 索引簽名的理解

    這篇文章主要給大家分享的是TypeScript 索引簽名的理解,索引簽名由方括號中的索引名稱及其類型組成,后面是冒號和值類型:{ [indexName: KeyType]: ValueType }, KeyType 可以是一個 string、number 或 symbol,而ValueType 可以是任何類型,下面就倆簡單了解一下吧
    2021-10-10
  • JavaScript?鍵盤事件的處理及屬性詳解

    JavaScript?鍵盤事件的處理及屬性詳解

    這篇文章主要為大家介紹了JavaScript?鍵盤事件的處理及屬性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 前端JavaScript之Promise

    前端JavaScript之Promise

    這篇文章主要介紹關(guān)于前端JavaScript的Promise,Promise 是異步編程的一種解決方案,下面文章我們就來看看Promise的基本用法及各種方法,需要的朋友可以參考一下,希望對你有所幫助
    2021-10-10
  • 微信小程序 商城開發(fā)(ecshop )簡單實例

    微信小程序 商城開發(fā)(ecshop )簡單實例

    這篇文章主要介紹了微信小程序 商城開發(fā)(ecshop )簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • javascript函數(shù)式編程基礎(chǔ)

    javascript函數(shù)式編程基礎(chǔ)

    這篇文章主要介紹了javascript函數(shù)式編程,文章有介紹函數(shù)自核,聲明式和命令式代碼,感興趣的小伙伴可以一起來閱讀下面我文章內(nèi)容
    2021-09-09
  • 8個工程必備的JavaScript代碼片段

    8個工程必備的JavaScript代碼片段

    這篇文章小編主要給大家介紹是8個工程中必須必備的JavaScript代碼片段,需要的小伙伴可以參考下面文章的具體內(nèi)容,對你有用的話建議收藏哦
    2021-09-09
  • 最新評論