html5 css3 实现二叉树,vue+js+css怎么实现以下的样式,点击A或者B,出现在右侧的方框中...

之前碰巧做过这种结构的东西,用的flex布局,上下左右四个方向均包含,部分内容不方便截图,仅对于实现的思路说明下,希望对你有帮助

bVcQUSg

对于你需要的左右结构的二叉树,那么每一级为一层node-wrap,最外层的node-box,使用display:flex; align-items:center;,每一层node-wrap使用flex-direction:column,针对横向的线条,对于每个node-item使用定位,利用伪类显示,而属相的层级的线条,使用node-wrap的伪类,长度需要动态计算 = calc(100% - 上一层第一个高度半值 - 最后一个高度半值)

bVcQUU1

部分关键代码贴一部分:// Right Nodes Box Stylus

.right-nodes-box {

position: relative;

display: flex;

flex-direction: column;

&::before {

content: "";

position: absolute;

left: 0;

height: calc(100% - #{$node-height} - 80px - 2px);

width: 2px;

background: $border-color;

bottom: ($node-height + 2px + 80px) / 2;

}

.node-wrap {

padding: 40px 0 40px 70px;

flex-direction: row;

&::before {

content: "";

position: absolute;

left: 0;

bottom: calc(50% - 2px);

width: 70px;

height: 2px;

background: #00aaff;

}

&:not(:first-child):not(:last-child) {

.nodes-child-box {

position: relative;

bottom: 0;

}

}

&::after {

content: '';

position: absolute;

left: 40px;

bottom: 50%;

margin-bottom: -8px;

transform: translateX(-50%);

width: 0;

border-color: transparent transparent transparent #00aaff;

border-style: solid;

border-width: 8px 6px 8px;

}

}

&.onlyone {

.node-wrap {

padding-left: 0;

&::before, &::after {

content: none;

}

}

}

}

.node-wrap .right-nodes-box {

position: absolute;

left: $node-width + 100px + 42px;

z-index: -1;

display: flex;

margin-left: 2px;

top: 50%;

transform: translateY(-50%);

}

主要是看你怎么想你的实现思路,针对实现思路再去考虑相应的代码如何去写~

希望对你有帮助~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 d3.js实现 Vue2 的二叉树页面。以下是一个简单的示例: 首先,安装 d3.js: ``` npm install d3 --save ``` 然后,在 Vue 组件引入 d3.js: ```javascript import * as d3 from 'd3'; ``` 接下来,在 Vue 组件使用 d3.js 创建一个 SVG 元素,并绘制一个简单的二叉树: ```html <template> <div ref="tree"></div> </template> <script> import * as d3 from 'd3'; export default { mounted() { const data = { name: 'A', children: [ { name: 'B', children: [ { name: 'D' }, { name: 'E' } ] }, { name: 'C', children: [ { name: 'F' }, { name: 'G' } ] } ] }; const width = 500; const height = 500; const svg = d3.select(this.$refs.tree) .append('svg') .attr('width', width) .attr('height', height); const treeLayout = d3.tree().size([width, height]); const root = d3.hierarchy(data); treeLayout(root); const nodes = svg.selectAll('g.node') .data(root.descendants()) .enter() .append('g') .attr('class', 'node') .attr('transform', d => `translate(${d.x}, ${d.y})`); nodes.append('circle') .attr('r', 10); nodes.append('text') .text(d => d.data.name); } } </script> ``` 在这个示例,我们首先定义了一个二叉树数据对象 `data`,然后创建了一个 SVG 元素,并设置了宽度和高度。接着,我们使用 d3.tree() 创建一个树形布局,并将它应用到数据对象上。最后,我们使用 d3.select() 选择根元素,然后使用 data()、enter() 和 append() 方法创建节点和连接线,并使用 transform 属性将它们放置在正确的位置上。 这只是一个简单的示例,你可以根据自己的需求修改代码。希望能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值