首页 > 其他分享 >vue实现动态展开与折叠内联块元素

vue实现动态展开与折叠内联块元素

时间:2023-10-25 15:01:39浏览次数:26  
标签:isCollapsed vue return 折叠 第四行 按钮 内联 children

功能需求

当多个内联块元素(比如div)在一个固定宽度的父元素内自动排列换行时,如果这些元素的行数超过四行,那么默认折叠超出的部分,并在第四行末尾显示一个按钮供用户切换展开或折叠状态。在折叠状态下,为了确保展开/折叠按钮能够显示在第四行末尾,会隐藏第四行的最后一个元素。在展开状态下,所有元素都会显示,并且展开/折叠按钮会显示在所有元素的末尾。

代码实现

<template>
  <div>
    <div class="container" ref="container">
      <!-- 遍历每一个项目并根据条件决定是否隐藏 -->
      <div
        v-for="(item, index) in items"
        :key="index"
        :class="{ hidden: shouldHideItem(index) }"
        class="inline-block-item"
      >
        {{ item }}
      </div>
      <!-- 只有当需要显示切换按钮时,才渲染此按钮 -->
      <button 
        v-if="showToggle" 
        @click="toggle" 
        class="inline-block-item" 
        ref="toggleButton"
      >
        {{ isCollapsed ? '展开' : '折叠' }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 初始项目列表
      items: Array.from({ length: 20 }, (_, i) => `Item ${i + 1}`),
      // 默认为折叠状态
      isCollapsed: true,
      // 是否需要显示切换按钮
      showToggle: false,
      // 第四行后的第一个项目的索引
      firstItemIndexAfterFourRows: Infinity,
    };
  },
  mounted() {
    // 当组件加载完成后,检查是否需要显示切换按钮
    this.$nextTick(() => {
      this.checkIfNeedToggle();
    });
  },
  methods: {
    // 切换展开/折叠状态
    toggle() {
      this.isCollapsed = !this.isCollapsed;
    },
    // 根据当前状态和项目索引判断是否应该隐藏项目
    shouldHideItem(index) {
      if (this.isCollapsed) {
        // 折叠状态时,隐藏第四行的最后一个项目
        if (index === this.firstItemIndexAfterFourRows - 1) return true;
        // 以及所有在第四行之后的项目
        return index >= this.firstItemIndexAfterFourRows;
      }
      // 展开状态时不隐藏任何项目
      return false; 
    },
    // 检查是否需要显示切换按钮
    checkIfNeedToggle() {
      const container = this.$refs.container;
      const children = container.children;
      if (!children.length) return;

      let rowCount = 1;
      let prevTop = children[0].getBoundingClientRect().top;

      for (let i = 1; i < children.length; i++) {
        const currTop = children[i].getBoundingClientRect().top;

        // 如果当前项目的顶部位置大于前一个项目,说明它在一个新的行
        if (currTop > prevTop) {
          rowCount++;
          // 如果行数超过4行,记录索引并设置 showToggle 为 true
          if (rowCount > 4) {
            this.firstItemIndexAfterFourRows = i;
            this.showToggle = true;
            break;
          }
        }

        prevTop = currTop;
      }
    },
  },
};
</script>

<style scoped>
.container {
  width: 200px;
}

.inline-block-item {
  display: inline-block;
  margin: 2px;
  white-space: nowrap;
}

.hidden {
  display: none;
}
</style>

标签:isCollapsed,vue,return,折叠,第四行,按钮,内联,children
From: https://www.cnblogs.com/China-Dream/p/17787224.html

相关文章

  • 29-Vue脚手架-mixin 混入
    mixin混入功能:可以把多个组件共用的配置提取成一个混入对象使用混合:1)第一步,定义混入新建一个JS文件,比如mixin.jssrc/mixin.js//分别暴露exportconsthunhe1={methods:{showName(){alert(this.name)}},mounted(){......
  • vue3 watch 用法
    <scriptsetup>import{ref,computed,watch}from'vue'constnum=ref(1)constname=ref('ming')constobj=ref({name:'小明',age:30})//watch简单类型//watch(num,(newValue,oldVal......
  • [Vue]computed和watch的区别
    computed和watch之间的区别: 1.computed能完成的功能,watch都可以完成。 2.watch能完成的功能,computed不一定能完成,例如:watch可以进行异步操作。两个重要的小原则: 1.所有被Vue管理的函数,最好写成普通函数,这样this的指向才是vm或组件实例对象。 2.所有不......
  • vue中如何使用svg,以及碰到的相应问题
    安装cnpminstallsvg-sprite-loader--save-dev创建svg文件夹存放svg图标创建icons文件夹,在icons文件夹下创建svg文件夹存放本地svg图标。vue.config.js中配置svg图片config.module.rule("svg").exclude.add(resolve("src/icons")).end();config.module.rule......
  • vue3 elementplus table表格内添加checkbox和行号
    1.仅添加复选框<el-table-columntype="selection"width="55"></el-table-column>2.添加复选框和文字行号在一列<el-table-column><template#header><el-checkboxv-model="selectAll"@change="han......
  • vue 首次加载项目,控制台报错: Redirected when going from "/" to "/login"
    第一次加载加载页面时报错如下:Redirectedwhengoingfrom"/"to"/login"viaanavigationguard. ![image](https://img2023.cnblogs.com/blog/1880163/202310/1880163-20231025113840444-1010075971.png)后续在地址栏直接添加/login,index,错误页面等均正常无报错.路由......
  • Jenkins配置java和vue构建环境
    jdk,maven,node,localtime等配置可通过挂载的方式进行配置前提条件是虚拟机中已配置好jdk,maven,node等环境注意自己虚拟机相关环境配置的路径以下样例为我自己的虚拟机中的配置路径-v宿主机(虚拟机)路径:容器路径dockerrun--namejenkins-p28081:8080-p50000:50000-v/v......
  • vue3 动态加载组件
    <el-dropdownstyle="margin:0px"><el-buttontype="primary">视图</el-button><template#dropdown><el-dropdown-menu><el-dropdown-itemv-for="dropItemindropI......
  • js中使用css变量(vue)
    html<divclass="test":style="{'--backgroundColor':backgroundColor}"></div>jscss .test{background-color:var(--backgroundColor);} ......
  • 28-Vue脚手架-props配置项
    props配置项props让组件接收外部传过来的数据1)传递数据这里需要注意,通过age="18"的方式,传递进去的数据是字符类型的,通过动态绑定:age="26"的方式,传递进去的数据是整型类型<!--这里需要注意,:age="26"代表v-bind动态绑定,传入的age是int类型--><Studentname="马铃薯......