uni-app制作小程序?qū)崿F(xiàn)左右菜單聯(lián)動效果
前言
今天寫出了一個新的小玩意兒,個人認為實現(xiàn)思路與方法還算值得學(xué)習(xí),在這里分享給大家!
一、示意圖
示例:pandas 是基于NumPy 的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。
二、實現(xiàn)步驟與思路講解
1.靜態(tài)頁面的布局
頁面的布局——在實現(xiàn)具體功能之前,我們就要考慮所要實現(xiàn)的具體功能是什么,將靜態(tài)頁面結(jié)構(gòu)搭建完成,頁面結(jié)構(gòu)的構(gòu)成,決定了后續(xù)功能的實現(xiàn)難易程度與方便度,這里我們所要實現(xiàn)的是左右菜單的聯(lián)動,這就需要用到滑動效果,在uni-app中可利用scroll-view實現(xiàn)這一效果,相關(guān)屬性如下
| 屬性 | 類型 | 默認值 | 說明 |
| scroll-x | Boolean | false | 允許橫向滾動 |
| scroll-y | Boolean | false | 允許縱向滾動 |
| scroll-into-view | String | 值應(yīng)為某子元素id(id不能以數(shù)字開頭)。設(shè)置哪個方向可滾動,則在哪個方向滾動到該元素 | |
| @scroll | EventHandle | 滾動時觸發(fā),event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY} |
2.模擬數(shù)據(jù)格式
數(shù)據(jù)結(jié)構(gòu)—— 沒有后臺接口的同學(xué)可以自行模擬數(shù)據(jù) ,在開發(fā)中,自行模擬數(shù)據(jù)格式的能力也至關(guān)重要。這里所需要的數(shù)據(jù)結(jié)構(gòu)應(yīng)為: 頁面整體的數(shù)據(jù)是一個數(shù)組,其中左側(cè)菜單應(yīng)為一個個的對象,其中每一個對象應(yīng)該有一個子元素 name屬性名,它的值應(yīng)為標題的文,另外還應(yīng)有兩外一個子元素是一個數(shù)組,數(shù)組中的內(nèi)容應(yīng)為子菜單對應(yīng)的數(shù)據(jù)。
3.左側(cè)菜單的點擊效果
實現(xiàn)思路:
可給左側(cè)菜單一個點擊事件,在點擊中觸發(fā)scroll-view 的scroll-into-view屬性,所以這里千萬不要忘記給子元素添加相應(yīng)的id屬性,在點擊相應(yīng)標題時,即可自動切換
相應(yīng)代碼如下:
頁面屬性的設(shè)置

左側(cè)菜單的點擊事件
// 左側(cè)列表菜單的點擊事件
changeIndex(index) {
this.currentIndex = index;
this.rightScrollinto = 'tab' + index;
if (this.currentIndex < 6) {
this.rightScrollinto = "tab0"
}
this.leftScrollinto = 'left' + index;
},4.右側(cè)菜單的聯(lián)動效果
實現(xiàn)思路:
可獲得每一個子列表區(qū)塊的距離頂部的高度,那么這就涉及到要獲取具體的節(jié)點信息,在uni-app中相關(guān)的api可用于獲取某元素的節(jié)點信息,隨之聲明一個數(shù)組,將這些數(shù)據(jù)存放在一個數(shù)組中,然后判斷滑動的高度(這就需要用到scroll-view的@scroll事件,可獲取用戶滑動的距離)是否大于數(shù)組中的數(shù)據(jù),若大于,則將該區(qū)域的索引傳遞到左側(cè)菜單中,左側(cè)菜單移動到對應(yīng)的索引位置即可。
相關(guān)代碼:
// 獲取右側(cè)滑動區(qū)域每一個子區(qū)域的高度
getTop() {
const query = uni.createSelectorQuery().in(this);
query.selectAll('.demo3').boundingClientRect(data => {
// console.log("得到布局位置信息" + JSON.stringify(data));
// console.log("節(jié)點離頁面頂部的距離為" + 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è)滑動區(qū)域的滑動事件
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();
}
},三、具體實現(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è)滑動的標志scroll-into-view
rightScrollinto: '',
leftScrollinto: '',
// 用一個數(shù)組承載每一個子區(qū)塊的距離頂部的高度
topList: [],
}
},
mounted() {
this.getCate();
// 使用定時器獲取區(qū)塊節(jié)點信息
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è)列表菜單的點擊事件
changeIndex(index) {
this.currentIndex = index;
this.rightScrollinto = 'tab' + index;
if (this.currentIndex < 6) {
this.rightScrollinto = "tab0"
}
this.leftScrollinto = 'left' + index;
},
// 獲取右側(cè)滑動區(qū)域每一個子區(qū)域的高度
getTop() {
const query = uni.createSelectorQuery().in(this);
query.selectAll('.demo3').boundingClientRect(data => {
// console.log("得到布局位置信息" + JSON.stringify(data));
// console.log("節(jié)點離頁面頂部的距離為" + 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è)滑動區(qū)域的滑動事件
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)動效果的文章就介紹到這了,更多相關(guān)uni-app左右菜單聯(lián)動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue安裝依賴npm install過程中報錯npm ERR! cb() nev
這篇文章主要介紹了vue安裝依賴npm install過程中報錯npm ERR! cb() never called!問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

