uni-app制作小程序?qū)崿F(xiàn)左右菜單聯(lián)動(dòng)效果
前言
今天寫出了一個(gè)新的小玩意兒,個(gè)人認(rèn)為實(shí)現(xiàn)思路與方法還算值得學(xué)習(xí),在這里分享給大家!
一、示意圖
示例:pandas 是基于NumPy 的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。
二、實(shí)現(xiàn)步驟與思路講解
1.靜態(tài)頁面的布局
頁面的布局——在實(shí)現(xiàn)具體功能之前,我們就要考慮所要實(shí)現(xiàn)的具體功能是什么,將靜態(tài)頁面結(jié)構(gòu)搭建完成,頁面結(jié)構(gòu)的構(gòu)成,決定了后續(xù)功能的實(shí)現(xiàn)難易程度與方便度,這里我們所要實(shí)現(xiàn)的是左右菜單的聯(lián)動(dòng),這就需要用到滑動(dòng)效果,在uni-app中可利用scroll-view實(shí)現(xiàn)這一效果,相關(guān)屬性如下
屬性 | 類型 | 默認(rèn)值 | 說明 |
scroll-x | Boolean | false | 允許橫向滾動(dòng) |
scroll-y | Boolean | false | 允許縱向滾動(dòng) |
scroll-into-view | String | 值應(yīng)為某子元素id(id不能以數(shù)字開頭)。設(shè)置哪個(gè)方向可滾動(dòng),則在哪個(gè)方向滾動(dòng)到該元素 | |
@scroll | EventHandle | 滾動(dòng)時(shí)觸發(fā),event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY} |
2.模擬數(shù)據(jù)格式
數(shù)據(jù)結(jié)構(gòu)—— 沒有后臺(tái)接口的同學(xué)可以自行模擬數(shù)據(jù) ,在開發(fā)中,自行模擬數(shù)據(jù)格式的能力也至關(guān)重要。這里所需要的數(shù)據(jù)結(jié)構(gòu)應(yīng)為: 頁面整體的數(shù)據(jù)是一個(gè)數(shù)組,其中左側(cè)菜單應(yīng)為一個(gè)個(gè)的對象,其中每一個(gè)對象應(yīng)該有一個(gè)子元素 name屬性名,它的值應(yīng)為標(biāo)題的文,另外還應(yīng)有兩外一個(gè)子元素是一個(gè)數(shù)組,數(shù)組中的內(nèi)容應(yīng)為子菜單對應(yīng)的數(shù)據(jù)。
3.左側(cè)菜單的點(diǎn)擊效果
實(shí)現(xiàn)思路:
可給左側(cè)菜單一個(gè)點(diǎn)擊事件,在點(diǎn)擊中觸發(fā)scroll-view 的scroll-into-view屬性,所以這里千萬不要忘記給子元素添加相應(yīng)的id屬性,在點(diǎn)擊相應(yīng)標(biāo)題時(shí),即可自動(dòng)切換
相應(yīng)代碼如下:
頁面屬性的設(shè)置
左側(cè)菜單的點(diǎn)擊事件
// 左側(cè)列表菜單的點(diǎn)擊事件 changeIndex(index) { this.currentIndex = index; this.rightScrollinto = 'tab' + index; if (this.currentIndex < 6) { this.rightScrollinto = "tab0" } this.leftScrollinto = 'left' + index; },
4.右側(cè)菜單的聯(lián)動(dòng)效果
實(shí)現(xiàn)思路:
可獲得每一個(gè)子列表區(qū)塊的距離頂部的高度,那么這就涉及到要獲取具體的節(jié)點(diǎn)信息,在uni-app中相關(guān)的api可用于獲取某元素的節(jié)點(diǎn)信息,隨之聲明一個(gè)數(shù)組,將這些數(shù)據(jù)存放在一個(gè)數(shù)組中,然后判斷滑動(dòng)的高度(這就需要用到scroll-view的@scroll事件,可獲取用戶滑動(dòng)的距離)是否大于數(shù)組中的數(shù)據(jù),若大于,則將該區(qū)域的索引傳遞到左側(cè)菜單中,左側(cè)菜單移動(dòng)到對應(yīng)的索引位置即可。
相關(guān)代碼:
// 獲取右側(cè)滑動(dòng)區(qū)域每一個(gè)子區(qū)域的高度 getTop() { const query = uni.createSelectorQuery().in(this); query.selectAll('.demo3').boundingClientRect(data => { // console.log("得到布局位置信息" + JSON.stringify(data)); // console.log("節(jié)點(diǎn)離頁面頂部的距離為" + data.top); if (data) { data.map((item, index) => { let top = index > 0 ? this.topList[index - 1] : 0; top += item.height; this.topList.push(top); }) } console.log(this.topList); }).exec(); }, //右側(cè)滑動(dòng)區(qū)域的滑動(dòng)事件 rightscroll(event) { // console.log(event.target.scrollTop) let scrollTop = event.target.scrollTop let result = this.topList.findIndex((item,index)=>{ return scrollTop<=item }) this.currentIndex = result; // this.changeIndex(); } },
三、具體實(shí)現(xiàn)代碼
1.頁面結(jié)構(gòu)
<!-- 左側(cè)列表欄區(qū)域 s--> <view class="uni-padding-wrap uni-common-mt"> <view class="d-flex"> <scroll-view scroll-with-animation :scroll-top="scrollTop" scroll-y="true" class="scroll-Y left-scroll" :scroll-into-view="rightScrollinto"> <view @click="changeIndex(index)" :id="'tab'+index" v-for="(item,index) in listName" :key="item.id" :class="currentIndex == index?'active-class':''"> {{item.name}} </view> </scroll-view> <scroll-view @scroll="rightscroll" scroll-with-animation :style="'height:'+scrollH+'px'" :scroll-top="scrollTop" scroll-y="true" class="scroll-Y right-scroll" :scroll-into-view="leftScrollinto"> <view :id="'left'+bindex" v-for="(bitem,bindex) in listName" :key="bindex" class="d-flex flex-wrap demo3"> <view v-for="(childItem, Aindex) in bitem.app_category_items" :key="childItem.id" class=" demo2 scroll-view-item uni-bg-red demo2"> <view class="img"> <image :src="childItem.cover" mode="scaleToFill"></image> </view> <view class="text"> <text>{{childItem.name}}</text> </view> </view> </view> </scroll-view> </view> </view>
2.相關(guān)樣式
.left-scroll { width: 30%; background: #f4f4f4; text-align: center; } .left-scroll view { height: 120rpx; line-height: 120rpx; } .right-scroll { width: 70%; } .right-scroll .demo2 { width: 33%; text-align:center; margin-top:0; } image { width: 120rpx; height: 120rpx; } .active-class { color: orange; background: white; border-top-right-radius: 10rpx; border-bottom-right-radius: 10rpx; }
3.業(yè)務(wù)邏輯部分
<script> import { getCate } from '../../api/cate.js'; export default { data() { return { currentIndex: 0, listName: [], scrollH: 0, // 表明左右兩側(cè)滑動(dòng)的標(biāo)志scroll-into-view rightScrollinto: '', leftScrollinto: '', // 用一個(gè)數(shù)組承載每一個(gè)子區(qū)塊的距離頂部的高度 topList: [], } }, mounted() { this.getCate(); // 使用定時(shí)器獲取區(qū)塊節(jié)點(diǎn)信息 setTimeout(() => { this.getTop(); }, 500) }, onLoad() { // 異步獲取系統(tǒng)信息,包括屏幕高度等 uni.getSystemInfo({ success: (res) => { console.log(res); // #ifdef MP this.scrollH = res.windowHeight - uni.upx2px(88) // #endif } }); }, methods: { // 調(diào)用獲取分類頁數(shù)據(jù)的方法 getCate() { getCate().then((response) => { console.log(response) this.listName = response.data }) }, // 左側(cè)列表菜單的點(diǎn)擊事件 changeIndex(index) { this.currentIndex = index; this.rightScrollinto = 'tab' + index; if (this.currentIndex < 6) { this.rightScrollinto = "tab0" } this.leftScrollinto = 'left' + index; }, // 獲取右側(cè)滑動(dòng)區(qū)域每一個(gè)子區(qū)域的高度 getTop() { const query = uni.createSelectorQuery().in(this); query.selectAll('.demo3').boundingClientRect(data => { // console.log("得到布局位置信息" + JSON.stringify(data)); // console.log("節(jié)點(diǎn)離頁面頂部的距離為" + data.top); if (data) { data.map((item, index) => { let top = index > 0 ? this.topList[index - 1] : 0; top += item.height; this.topList.push(top); }) } console.log(this.topList); }).exec(); }, //右側(cè)滑動(dòng)區(qū)域的滑動(dòng)事件 rightscroll(event) { // console.log(event.target.scrollTop) let scrollTop = event.target.scrollTop let result = this.topList.findIndex((item,index)=>{ return scrollTop<=item }) this.currentIndex = result; // this.changeIndex(); } }, } </script>
到此這篇關(guān)于uni-app制作小程序?qū)崿F(xiàn)左右菜單聯(lián)動(dòng)效果的文章就介紹到這了,更多相關(guān)uni-app左右菜單聯(lián)動(dòng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解mpvue開發(fā)微信小程序基礎(chǔ)知識(shí)
這篇文章主要介紹了詳解mpvue開發(fā)微信小程序基礎(chǔ)知識(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09淺談vue websocket nodeJS 進(jìn)行實(shí)時(shí)通信踩到的坑
這篇文章主要介紹了淺談vue websocket nodeJS 進(jìn)行實(shí)時(shí)通信踩到的坑,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09axios上傳文件錯(cuò)誤:Current?request?is?not?a?multipart?request
最近工作中使用vue上傳文件的時(shí)候遇到了個(gè)問題,下面這篇文章主要給大家介紹了關(guān)于axios上傳文件錯(cuò)誤:Current?request?is?not?a?multipart?request解決的相關(guān)資料,需要的朋友可以參考下2023-01-01vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開時(shí)縮小的代碼
這篇文章主要介紹了vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開時(shí)縮小,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01深入理解?Vue?3實(shí)現(xiàn)組件通信的方法
本文將介紹幾種常見的?Vue?3?組件通信方法,包括?props、emits、provide?和?inject、事件總線以及?Vuex?狀態(tài)管理,需要的朋友可以參考下2024-07-07vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟
這篇文章主要介紹了vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06