首页 > 其他分享 >侧边导航,左右两边联动

侧边导航,左右两边联动

时间:2024-09-30 14:51:22浏览次数:3  
标签:index 滚动 侧边 heights item 联动 导航 projects wx

实现效果:左侧是导航,右边根据点击导航滚动到指定位置,右侧滚动到某个位置,左侧也要跟着变化。

使用的是原生微信小程序,使用了vant-weapp的侧边导航组件

<view class="content">
  <scroll-view scroll-y class="left" show-scrollbar="{{false}}" enhanced>
    <van-sidebar active-key="{{ activeSidebar }}" custom-class="custom-class">
      <block wx:for="{{projects}}" wx:key="id">
        <van-sidebar-item title="{{item.title}}" custom-class="custom-class-item" bind:click="onSidebarChange" />
      </block>
    </van-sidebar>
  </scroll-view>
  <scroll-view flex-able class="right" scroll-y bindscroll="onScroll" scroll-into-view="{{scrollIntoViewId}}" show-scrollbar="{{false}}" enhanced>
    <block wx:for="{{projects}}" wx:key="id">
      <view class="projects-content" id="projects-{{index}}">
        {{item.title}}
        ...
      </view>
    </block>
  </scroll-view>
</view>
data: {
    activeSidebar: 0,                                 // 左侧激活的项
    scrollIntoViewId: '',                             // 右侧滚动到的视图id
    contentHeights: [] as number[],                   // 存储每个分类内容的高度
    projects:[...]
},
lifetimes: {
    attached() {
      this.calculateHeights()
    },
  },
  methods: {
    // 计算每个分类内容的高度,便于滚动时判断在哪个分类
    calculateHeights() {
      const query = this.createSelectorQuery();
      let heights: number[] = [];
      let totalHeight = 0;

      // 遍历每个右侧分类内容,获取它们的高度
      this.data.projects.forEach((_, index) => {
        query.select(`#projects-${index}`).boundingClientRect((rect) => {
          totalHeight += rect.height;
          heights.push(totalHeight);
        }).exec();
      });

      this.setData({
        contentHeights: heights
      });
    },
    // 右侧内容滚动时触发
    onScroll(event: tapEventOption<any, { scrollTop: number }>) {
      const scrollTop = event.detail.scrollTop;
      const heights = this.data.contentHeights;

      // 根据滚动高度计算当前应该激活的左侧项
      for (let i = 0; i < heights.length; i++) {
        if (scrollTop < heights[i]) {
          this.setData({
            activeSidebar: i
          });
          break;
        }
      }
    },
    // 左侧激活项变化时触发
    onSidebarChange(event: tapEventOption<any, number>) {
      const index = event.detail;
      // 设置右侧内容滚动到相应的位置
      this.setData({
        activeSidebar: index,
        scrollIntoViewId: `projects-${index}`
      });
    }
  }

这是在子组件中,在attached中调用方法,因为我在父组件用了两个子组件切换展示,另一个不需要获取高度,只有attached中调用不报错,created中获取不到,ready中调用会在展示另一个子组件时报错,获取不到高度。

不过使用vant-weapp的侧边导航数据只能一层,要做点击后有子项需要展开的需要自己做

<scroll-view flex-able scroll-y class="left" show-scrollbar="{{false}}" enhanced>
    <block wx:for="{{sidebar}}" wx:key="index">
      <view class="one-level-item {{index === activeSidebar ? 'active' : ''}}" bind:tap="onSidebarChange" data-index="{{index}}">
        {{item.name}}
        <block wx:if="{{item.children.length}}">
          <view class="triangle-down" wx:if="{{!item.expanded}}"></view>
          <view class="triangle-up" wx:if="{{item.expanded}}"></view>
        </block>
      </view>
      <block wx:if="{{item.expanded && item.children.length}}">
        <block wx:for="{{item.children}}" wx:for-item="itm" wx:for-index="idx" wx:key="id">
          <view class="two-level-item {{idx === activeSidebarItem ? 'active' : ''}}" bind:tap="onSidebarItem" data-idx="{{idx}}">{{itm.name}}</view>
        </block>
      </block>
    </block>
  </scroll-view>

用了滚动,如果里面有flex布局,scroll-view要加flex-able,不然里面的flex布局会失效

标签:index,滚动,侧边,heights,item,联动,导航,projects,wx
From: https://blog.csdn.net/qq_51354326/article/details/142656322

相关文章

  • PbootCMS默认面包屑导航样式修改及自定义的设置方法
    在使用PbootCMS建站时,如果需要对系统默认的面包屑(breadcrumb)标签样式进行修改,可以通过自定义参数来实现。下面详细介绍如何使用 {pboot:position} 标签,并自定义相关的参数。自定义面包屑标签基本语法{pboot:position}[position:breadcrumb]{/pboot:position}......
  • pbootcms模板导航设置外链时新窗口打开
    要在PbootCMS中设置导航链接并在新窗口中打开外部链接,可以使用以下方法。具体步骤如下:修改导航标签添加条件判断示例代码以下是完整的示例代码,展示了如何在导航链接中添加条件判断,以便在新窗口中打开外部链接:{pboot:nav}<ahref="[nav:link]"{pboot:if('[nav:ou......
  • Antd4X Form.List嵌入Table 表格项数据联动
    antd4X版本父组件是Form组件,其中一项为Form.List,Form.List内嵌了一个Tableconstform=Form.useForm()<Formform={form}><Form.List>{(fields,operate,{errors})=>{return(<TableclassName="wf-subform"......
  • Prism导航
    注册导航页面注册区域使用p:RegionManager.RegionName注册页面区域<Windowx:Class="WpfApp1.NavigationWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&......
  • PbootCms导航菜单标签的这些小技巧你都知道吗?
    为了帮助新手更好地理解和使用PbootCMS模板中的标签,以下是一些常见问题及其解决方案。1.常用的导航标签<spanstyle="font-size:14px;">{pboot:nav}<ahref="[nav:link]">[nav:name]</a>{/pboot:nav}</span>控制参数*num=数量:非必填,用于控制输出的数量。*parent=......
  • 无人机之视觉导航算法篇
    一、图像采集与预处理图像采集:无人机通过其搭载的摄像头或其他视觉传感器实时采集周围环境的图像信息。图像预处理:对采集到的图像进行预处理,包括滤波、降噪、增强等操作,以提高图像的质量和清晰度,为后续的特征提取和匹配奠定基础。二、特征提取与匹配特征提取:从预处理后的图......
  • pbootcms模板导航设置外链时新窗口打开
    在PbootCMS中,如果你想在模板导航中设置外链并在新窗口中打开,可以通过条件判断来实现这一功能。具体步骤如下:实现步骤编写条件判断代码:使用 {pboot:if} 语句来判断外链是否为空。如果外链不为空,则添加 target="_blank" 属性。整合到导航链接中:将条件判断代码嵌......
  • 移动端tree组件父子组件联动。
     <!--*@Author:yeminglong*@Date:2024-09-2710:14:30*@LastEditors:yeminglong*@LastEditTime:2024-09-2716:49:05*@Description:--><script>importTreeItemfrom'@/views/test/TreeItem.vue'exportdefault{ name:&#......
  • 蓝牙定位导航系统深度解析:技术原理、实现步骤与实战应用
    随着物联网(IoT)技术的飞速发展,蓝牙低功耗(BLE)技术凭借其低功耗、高兼容性及短距离通信的优势,在各类定位系统中占据了重要地位。其中,蓝牙定位导航系统作为室内定位解决方案的佼佼者,正逐步改变着我们的生活方式。本文将深入探讨蓝牙定位导航系统的技术原理、关键技术、实现步骤,并通......
  • uniapp h5端地图导航功能
    <template> <viewclass="container"> <viewclass="content"> <map:scale="14":show-location="true":show-compass="true"class="map-content" :latitude="position.lati......