• uni-app —— 小程序实现左右菜单联动


    文章目录

    • 前言
    • 一、示意图
    • 二、使用步骤
      • 1.静态页面的布局
      • 2.模拟数据格式
      • 3.左侧菜单的点击效果
      • 4.右侧菜单的联动效果
    • 三、具体实现代码
      • 1.页面结构
      • 2.相关样式
      • 3.业务逻辑部分


    前言

    今天写出了一个新的小玩意儿,个人认为实现思路与方法还算值得学习,在这里分享给大家!


    一、示意图

    二、实现步骤与思路讲解

    1.静态页面的布局

    • 页面的布局——在实现具体功能之前,我们就要考虑所要实现的具体功能是什么,将静态页面结构搭建完成,页面结构的构成,决定了后续功能的实现难易程度与方便度,这里我们所要实现的是左右菜单的联动,这就需要用到滑动效果,在uni-app中可利用scroll-view实现这一效果,相关属性如下

    属性类型默认值说明
    scroll-xBooleanfalse允许横向滚动
    scroll-yBooleanfalse允许纵向滚动
    scroll-into-viewString值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
    @scrollEventHandle滚动时触发,event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}

    2.模拟数据格式

    数据结构—— 没有后台接口的同学可以自行模拟数据 ,在开发中,自行模拟数据格式的能力也至关重要。这里所需要的数据结构应为: 页面整体的数据是一个数组,其中左侧菜单应为一个个的对象,其中每一个对象应该有一个子元素 name属性名,它的值应为标题的文,另外还应有两外一个子元素是一个数组,数组中的内容应为子菜单对应的数据。

    3.左侧菜单的点击效果

    实现思路:

    可给左侧菜单一个点击事件,在点击中触发scroll-view 的scroll-into-view属性,所以这里千万不要忘记给子元素添加相应的id属性,在点击相应标题时,即可自动切换

    相应代码如下:

    页面属性的设置

    左侧菜单的点击事件

    1. // 左侧列表菜单的点击事件
    2. changeIndex(index) {
    3. this.currentIndex = index;
    4. this.rightScrollinto = 'tab' + index;
    5. if (this.currentIndex < 6) {
    6. this.rightScrollinto = "tab0"
    7. }
    8. this.leftScrollinto = 'left' + index;
    9. },

    4.右侧菜单的联动效果

    实现思路:

    可获得每一个子列表区块的距离顶部的高度,那么这就涉及到要获取具体的节点信息,在uni-app中相关的api可用于获取某元素的节点信息,随之声明一个数组,将这些数据存放在一个数组中,然后判断滑动的高度(这就需要用到scroll-view的@scroll事件,可获取用户滑动的距离)是否大于数组中的数据,若大于,则将该区域的索引传递到左侧菜单中,左侧菜单移动到对应的索引位置即可。

    相关代码:

    1. // 获取右侧滑动区域每一个子区域的高度
    2. getTop() {
    3. const query = uni.createSelectorQuery().in(this);
    4. query.selectAll('.demo3').boundingClientRect(data => {
    5. // console.log("得到布局位置信息" + JSON.stringify(data));
    6. // console.log("节点离页面顶部的距离为" + data.top);
    7. if (data) {
    8. data.map((item, index) => {
    9. let top = index > 0 ? this.topList[index - 1] : 0;
    10. top += item.height;
    11. this.topList.push(top);
    12. })
    13. }
    14. console.log(this.topList);
    15. }).exec();
    16. },
    17. //右侧滑动区域的滑动事件
    18. rightscroll(event) {
    19. // console.log(event.target.scrollTop)
    20. let scrollTop = event.target.scrollTop
    21. let result = this.topList.findIndex((item,index)=>{
    22. return scrollTop<=item
    23. })
    24. this.currentIndex = result;
    25. // this.changeIndex();
    26. }
    27. },

    三、具体实现代码

    1.页面结构

    1. <view class="uni-padding-wrap uni-common-mt">
    2. <view class="d-flex">
    3. <scroll-view scroll-with-animation :scroll-top="scrollTop"
    4. scroll-y="true" class="scroll-Y left-scroll"
    5. :scroll-into-view="rightScrollinto">
    6. <view @click="changeIndex(index)" :id="'tab'+index"
    7. v-for="(item,index) in listName" :key="item.id"
    8. :class="currentIndex == index?'active-class':''">
    9. {{item.name}}
    10. view>
    11. scroll-view>
    12. <scroll-view @scroll="rightscroll" scroll-with-animation :style="'height:'+scrollH+'px'"
    13. :scroll-top="scrollTop" scroll-y="true" class="scroll-Y right-scroll"
    14. :scroll-into-view="leftScrollinto">
    15. <view :id="'left'+bindex" v-for="(bitem,bindex) in listName" :key="bindex" class="d-flex flex-wrap demo3">
    16. <view v-for="(childItem, Aindex) in bitem.app_category_items" :key="childItem.id"
    17. class=" demo2 scroll-view-item uni-bg-red demo2">
    18. <view class="img">
    19. <image :src="childItem.cover" mode="scaleToFill">image>
    20. view>
    21. <view class="text">
    22. <text>{{childItem.name}}text>
    23. view>
    24. view>
    25. view>
    26. scroll-view>
    27. view>
    28. view>

    2.相关样式

    1. .left-scroll {
    2. width: 30%;
    3. background: #f4f4f4;
    4. text-align: center;
    5. }
    6. .left-scroll view {
    7. height: 120rpx;
    8. line-height: 120rpx;
    9. }
    10. .right-scroll {
    11. width: 70%;
    12. }
    13. .right-scroll .demo2 {
    14. width: 33%;
    15. text-align:center;
    16. margin-top:0;
    17. }
    18. image {
    19. width: 120rpx;
    20. height: 120rpx;
    21. }
    22. .active-class {
    23. color: orange;
    24. background: white;
    25. border-top-right-radius: 10rpx;
    26. border-bottom-right-radius: 10rpx;
    27. }

    3.业务逻辑部分

  • 相关阅读:
    2023(2024届)计算机保研经验分享,圆梦山东大学
    【C++从0到王者】第三十三站:AVL树
    DSP28335模块配置模板系列——定时器中断配置模板
    Nodejs安装教程
    Excel VSTO开发2 -建立Excel VSTO项目
    基于图搜索的规划算法之可视图法
    电脑特别卡,但是看cpu和内存使用量并不大,该如何提升电脑运行速度
    top命令应用(查看进程实时动态信息)
    qt使用http get和post
    设计超萌的机械键盘,超有手感还不吵人,雷柏MT510PRO键盘上手
  • 原文地址:https://blog.csdn.net/Bonsoir777/article/details/127795820