当前位置:首页 > VUE

vue实现横向树

2026-03-09 22:46:30VUE

实现横向树的思路

横向树结构通常用于展示层级关系,但布局方向为水平而非传统的垂直方向。Vue中可以通过递归组件和CSS样式实现。

基础组件结构

创建递归组件HorizontalTree.vue,用于渲染树节点:

<template>
  <div class="horizontal-tree">
    <div class="node">
      {{ node.name }}
    </div>
    <div 
      v-if="node.children && node.children.length" 
      class="children"
    >
      <HorizontalTree
        v-for="child in node.children"
        :key="child.id"
        :node="child"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'HorizontalTree',
  props: {
    node: {
      type: Object,
      required: true
    }
  }
}
</script>

CSS样式设置

关键是通过Flex布局实现水平排列:

vue实现横向树

.horizontal-tree {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.node {
  padding: 8px 12px;
  margin: 0 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: #f5f5f5;
}

.children {
  display: flex;
  position: relative;
}

.children::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #ccc;
  z-index: -1;
}

使用组件

在父组件中传入树形数据:

<template>
  <div>
    <HorizontalTree :node="treeData" />
  </div>
</template>

<script>
import HorizontalTree from './HorizontalTree.vue'

export default {
  components: { HorizontalTree },
  data() {
    return {
      treeData: {
        id: 1,
        name: 'Root',
        children: [
          {
            id: 2,
            name: 'Child 1',
            children: [
              { id: 4, name: 'Grandchild 1' },
              { id: 5, name: 'Grandchild 2' }
            ]
          },
          {
            id: 3,
            name: 'Child 2'
          }
        ]
      }
    }
  }
}
</script>

高级优化

添加动画效果增强用户体验:

vue实现横向树

.node {
  transition: all 0.3s ease;
}

.node:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

实现动态展开/折叠功能:

<template>
  <div class="horizontal-tree">
    <div 
      class="node"
      @click="toggle"
    >
      {{ node.name }}
      <span v-if="hasChildren">
        {{ isExpanded ? '−' : '+' }}
      </span>
    </div>
    <div 
      v-if="isExpanded && hasChildren" 
      class="children"
    >
      <HorizontalTree
        v-for="child in node.children"
        :key="child.id"
        :node="child"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'HorizontalTree',
  props: {
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isExpanded: true
    }
  },
  computed: {
    hasChildren() {
      return this.node.children && this.node.children.length
    }
  },
  methods: {
    toggle() {
      if (this.hasChildren) {
        this.isExpanded = !this.isExpanded
      }
    }
  }
}
</script>

响应式设计

添加媒体查询确保在小屏幕上正常显示:

@media (max-width: 768px) {
  .horizontal-tree {
    flex-direction: column;
  }
  .children {
    flex-direction: column;
  }
  .children::before {
    width: 1px;
    height: auto;
    bottom: 0;
    left: 50%;
    top: 0;
  }
}

标签: 横向vue
分享给朋友:

相关文章

vue实现图片分析

vue实现图片分析

Vue 实现图片分析的方法 在 Vue 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas 进行前…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…