新聞中心
這篇文章主要介紹了小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司專(zhuān)注于天鎮(zhèn)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供天鎮(zhèn)營(yíng)銷(xiāo)型網(wǎng)站建設(shè),天鎮(zhèn)網(wǎng)站制作、天鎮(zhèn)網(wǎng)頁(yè)設(shè)計(jì)、天鎮(zhèn)網(wǎng)站官網(wǎng)定制、重慶小程序開(kāi)發(fā)公司服務(wù),打造天鎮(zhèn)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供天鎮(zhèn)網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
微信小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換
效果:
最終效果如上。問(wèn)題:
1、tab標(biāo)題總共8個(gè),所以一屏無(wú)法全部顯示。
2、tab內(nèi)容區(qū)左右滑動(dòng)切換時(shí),tab標(biāo)題隨即做標(biāo)記(active)。
3、當(dāng)active的標(biāo)題不在當(dāng)前屏顯示時(shí),要使其能顯示到當(dāng)前屏中。
一、wxml結(jié)構(gòu)
tab標(biāo)題因一排八個(gè),所以使用 scroll-view組件,使其可橫向滾動(dòng)。
tab內(nèi)容可左右滑動(dòng)切換,使用swiper組件實(shí)現(xiàn)
為了偷懶,所以數(shù)據(jù)都通過(guò)wx:for遍歷重復(fù)出來(lái)。
說(shuō)明:
1、設(shè)置data-current屬性用于:點(diǎn)擊當(dāng)前項(xiàng)時(shí),通過(guò)點(diǎn)擊事件swichNav中處理e.dataset.current取到點(diǎn)擊的目標(biāo)值。
2、swiper組件的current組件用于控制當(dāng)前顯示哪一頁(yè)
3、swiper組件綁定change事件switchTab,通過(guò)e.detail.current拿到當(dāng)前頁(yè)
健康 情感 職場(chǎng) 育兒 糾紛 青蔥 全部 其他 歡顏 知名情感博主 134個(gè)回答,2234人聽(tīng)過(guò) 問(wèn)TA
二、js部分
微信小程序在開(kāi)發(fā)起來(lái),個(gè)人感覺(jué)挺像vue的,以數(shù)據(jù)驅(qū)動(dòng)視圖的更新。所以在小程序中,不能直接操作dom,當(dāng)然也不能使用jquery之類(lèi)的庫(kù)。
var app = getApp(); Page({ data:{ winHeight:"",//窗口高度 currentTab:0, //預(yù)設(shè)當(dāng)前項(xiàng)的值 scrollLeft:0, //tab標(biāo)題的滾動(dòng)條位置 expertList:[{ //假數(shù)據(jù) img:"avatar.png", name:"歡顔", tag:"知名情感博主", answer:134, listen:2234 }] }, // 滾動(dòng)切換標(biāo)簽樣式 switchTab:function(e){ this.setData({ currentTab:e.detail.current }); this.checkCor(); }, // 點(diǎn)擊標(biāo)題切換當(dāng)前頁(yè)時(shí)改變樣式 swichNav:function(e){ var cur=e.target.dataset.current; if(this.data.currentTaB==cur){return false;} else{ this.setData({ currentTab:cur }) } }, //判斷當(dāng)前滾動(dòng)超過(guò)一屏?xí)r,設(shè)置tab標(biāo)題滾動(dòng)條。 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%;}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換的方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問(wèn)題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!
文章題目:小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換的方法
文章起源:http://ef60e0e.cn/article/podsdc.html