uni-app制作小程序實現(xiàn)左右菜單聯(lián)動效果
前言
今天寫出了一個新的小玩意兒,個人認為實現(xiàn)思路與方法還算值得學習,在這里分享給大家!
一、示意圖
示例:pandas 是基于NumPy 的一種工具,該工具是為了解決數(shù)據分析任務而創(chuàng)建的。
二、實現(xiàn)步驟與思路講解
1.靜態(tài)頁面的布局
頁面的布局——在實現(xiàn)具體功能之前,我們就要考慮所要實現(xiàn)的具體功能是什么,將靜態(tài)頁面結構搭建完成,頁面結構的構成,決定了后續(xù)功能的實現(xiàn)難易程度與方便度,這里我們所要實現(xiàn)的是左右菜單的聯(lián)動,這就需要用到滑動效果,在uni-app中可利用scroll-view實現(xiàn)這一效果,相關屬性如下
屬性 | 類型 | 默認值 | 說明 |
scroll-x | Boolean | false | 允許橫向滾動 |
scroll-y | Boolean | false | 允許縱向滾動 |
scroll-into-view | String | 值應為某子元素id(id不能以數(shù)字開頭)。設置哪個方向可滾動,則在哪個方向滾動到該元素 | |
@scroll | EventHandle | 滾動時觸發(fā),event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY} |
2.模擬數(shù)據格式
數(shù)據結構—— 沒有后臺接口的同學可以自行模擬數(shù)據 ,在開發(fā)中,自行模擬數(shù)據格式的能力也至關重要。這里所需要的數(shù)據結構應為: 頁面整體的數(shù)據是一個數(shù)組,其中左側菜單應為一個個的對象,其中每一個對象應該有一個子元素 name屬性名,它的值應為標題的文,另外還應有兩外一個子元素是一個數(shù)組,數(shù)組中的內容應為子菜單對應的數(shù)據。
3.左側菜單的點擊效果
實現(xiàn)思路:
可給左側菜單一個點擊事件,在點擊中觸發(fā)scroll-view 的scroll-into-view屬性,所以這里千萬不要忘記給子元素添加相應的id屬性,在點擊相應標題時,即可自動切換
相應代碼如下:
頁面屬性的設置
左側菜單的點擊事件
// 左側列表菜單的點擊事件 changeIndex(index) { this.currentIndex = index; this.rightScrollinto = 'tab' + index; if (this.currentIndex < 6) { this.rightScrollinto = "tab0" } this.leftScrollinto = 'left' + index; },
4.右側菜單的聯(lián)動效果
實現(xiàn)思路:
可獲得每一個子列表區(qū)塊的距離頂部的高度,那么這就涉及到要獲取具體的節(jié)點信息,在uni-app中相關的api可用于獲取某元素的節(jié)點信息,隨之聲明一個數(shù)組,將這些數(shù)據存放在一個數(shù)組中,然后判斷滑動的高度(這就需要用到scroll-view的@scroll事件,可獲取用戶滑動的距離)是否大于數(shù)組中的數(shù)據,若大于,則將該區(qū)域的索引傳遞到左側菜單中,左側菜單移動到對應的索引位置即可。
相關代碼:
// 獲取右側滑動區(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(); }, //右側滑動區(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.頁面結構
<!-- 左側列表欄區(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.相關樣式
.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è)務邏輯部分
<script> import { getCate } from '../../api/cate.js'; export default { data() { return { currentIndex: 0, listName: [], scrollH: 0, // 表明左右兩側滑動的標志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: { // 調用獲取分類頁數(shù)據的方法 getCate() { getCate().then((response) => { console.log(response) this.listName = response.data }) }, // 左側列表菜單的點擊事件 changeIndex(index) { this.currentIndex = index; this.rightScrollinto = 'tab' + index; if (this.currentIndex < 6) { this.rightScrollinto = "tab0" } this.leftScrollinto = 'left' + index; }, // 獲取右側滑動區(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(); }, //右側滑動區(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>
到此這篇關于uni-app制作小程序實現(xiàn)左右菜單聯(lián)動效果的文章就介紹到這了,更多相關uni-app左右菜單聯(lián)動內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談vue websocket nodeJS 進行實時通信踩到的坑
這篇文章主要介紹了淺談vue websocket nodeJS 進行實時通信踩到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09axios上傳文件錯誤:Current?request?is?not?a?multipart?request
最近工作中使用vue上傳文件的時候遇到了個問題,下面這篇文章主要給大家介紹了關于axios上傳文件錯誤:Current?request?is?not?a?multipart?request解決的相關資料,需要的朋友可以參考下2023-01-01vue-cli4.x創(chuàng)建企業(yè)級項目的方法步驟
這篇文章主要介紹了vue-cli4.x創(chuàng)建企業(yè)級項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06