当前位置: 首页 > news >正文

js实现可折叠的列表或菜单

   setCollapse() {

      //初始展开数值

      const ref = this.$refs.listItemContentRef

      if(!ref || !ref.children) return;

      let len = ref.children.length

      if(ref.clientHeight > 77 && ref.children.length > 3) {

        const offsetTop = ref.children[0].offsetTop

        const maxHeight = 77 + offsetTop

        const collapseTotal = [];

        collapseTotal.push(this.collapseTotal[0])

        collapseTotal.push(this.collapseTotal[1])

        for(let i=2;i<len;i++) {

          if(ref.children[i].offsetTop >= maxHeight){

            const i_1_clientWidth= ref.children[i-1].clientWidth

            const i_1_offsetLeft= ref.children[i-1].offsetLeft

            const pcw = ref.clientWidth

            const collapse_w = 38

            const pcwDiff = pcw - (i_1_offsetLeft + i_1_clientWidth);

            if (pcwDiff > collapse_w) {

              collapseTotal[0] = i;

            } else if (i_1_clientWidth + pcwDiff > collapse_w) {

              collapseTotal[0] = i - 1;

            } else {

              collapseTotal[0] = i - 2;

            }

            this.collapseTotal=collapseTotal

            break;

          }

        }

      }

    },

这段代码的核心功能是根据父容器的高度和子元素的布局情况,动态计算并更新一个列表的折叠点。当列表的高度超过一定阈值(77像素)且子元素数量大于3时,通过一系列计算和判断,确定一个合适的折叠位置,并将该位置信息存储在组件的状态中,以便后续使用。这种逻辑通常用于实现可折叠的列表或菜单,以优化页面布局和提升用户体验。

相关文章:

  • 【亲测有效 | Cursor Pro每月500次快速请求扩5倍】(Windows版)Cursor中集成interactive-feedback-mcp
  • Java:跨越时代的编程语言,持续引领技术革新
  • 虚拟现实教育终端技术方案——基于EFISH-SCB-RK3588的全场景国产化替代
  • 软件评测师 综合测试 真题笔记
  • 【数据中心设计】
  • 《Effective Python》第六章 推导式和生成器——总结(基于智能物流仓储监控系统的数据处理)
  • 原始数据去哪找?分享15个免费官方网站
  • IP话机和APP拨打电话的区别
  • MidJourney入门学习
  • Spring AI介绍及大模型对接
  • [Java 基础]Java 是什么
  • 【QT】QString 与QString区别
  • 项目交付后缺乏回顾和改进,如何持续优化
  • 文件IO流
  • saveOrUpdate 有个缺点,不会把值赋值为null,解决办法
  • 笔记︱数据科学领域因果推断案例集锦(第三弹)
  • 爱普生Epson L3210打印机信息
  • LabVIEW磁悬浮轴承传感器故障识别
  • 金融中的线性优化:投资组合分配与求解器 - Part 2
  • SpringBoot系列之RabbitMQ 实现订单超时未支付自动关闭功能
  • 怎么做公司内部网站/优化网站排名需要多少钱
  • 做旅游景点网站的目的和意义/杭州优化外包
  • 桂林网站建设凡森网络/百度云账号登录
  • 军人可以做网站吗/无忧软文网
  • 文化馆网站建设/今日新闻热点10条
  • 华为域名购买/seo辅助优化工具