如何调整不同li元素之间的连接线角度,避免全部处于同一水平位置
气泡菜单错落连接线实现方案
核心调整点
- 删除原有ul上的全局水平横线样式,改为给每个li(除第一个)单独绘制连接前一个气泡的线段,每个线段可独立调整角度、位置
- 新增气泡偏移类,可自由设置每个气泡的垂直位置,实现错落排布的IoT链路效果
- 将原line-height垂直居中改为flex布局,解决修改字号后元素对齐错乱的问题
完整修改代码
SCSS代码
#large-dots { $line-width: 1px; $line-color: #008CBA; $circle-size: 40px; $circle-space: 20px; $circle-color: #008CBA; display: inline-block; width: auto; color: #333333; font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif; ul.chains { position: relative; padding: 0; display: flex; align-items: center; gap: $circle-space; } li.chain { position: relative; list-style-type: none; background-color: $circle-color; border-radius: 50%; width: $circle-size; height: $circle-size; display: flex; align-items: center; justify-content: center; /* 自定义垂直偏移,可针对每个li单独设置,连接线会自动适配 */ &.offset-up { transform: translateY(-30px); } &.offset-down { transform: translateY(30px); } /* 绘制两个气泡之间的连接线 */ &:not(:first-child)::before { content: ""; position: absolute; left: calc(-#{$circle-space} - #{$line-width}); top: 50%; width: $circle-space + $line-width; height: $line-width; background: $line-color; transform-origin: left center; /* 角度可根据前后气泡的高度差自定义,也可以用JS动态计算 */ transform: rotate(var(--line-angle, 0deg)) translateY(-50%); z-index: -1; } &.enabled { width: $circle-size * 2; height: $circle-size * 2; .price { font-size: 1.25em; } } } .price { font-size: 1.25em; } .smallround:hover { background-color: blue; border-radius: 50%; width: $circle-size; height: $circle-size; display: flex; align-items: center; justify-content: center; } .biground:hover { background-color: #FFFF00; border-radius: 50%; width: $circle-size * 2; height: $circle-size * 2; display: flex; align-items: center; justify-content: center; } }
HTML代码
<div id="large-dots" class="example-container"> Example of Landing Page <ul class="chains"> <li class="chain enabled"> <div class="price biground">Home Page</div> </li> <!-- 向上偏移,连接线角度设为15度 --> <li class="chain offset-up" style="--line-angle: 15deg"> <div class="price smallround"></div> </li> <!-- 向下偏移,连接线角度设为-20度 --> <li class="chain enabled offset-down" style="--line-angle: -20deg"> <div class="price biground">Contact</div> </li> <li class="chain" style="--line-angle: 10deg"> <div class="price smallround"></div> </li> <li class="chain offset-up" style="--line-angle: -12deg"> <div class="price smallround"></div> </li> <li class="chain enabled" style="--line-angle: 8deg"> <div class="price biground">About</div> </ul> </div>
使用说明
- 给每个li添加
offset-up/offset-down类或者直接设置transform: translateY()自定义垂直位置 - 通过给li设置行内CSS变量
--line-angle调整当前节点和前一个节点之间连接线的角度,正数向上倾斜,负数向下倾斜 - 如果需要完全自动化适配角度,可以加少量JS获取前后两个li的位置差,自动计算角度赋值给
--line-angle即可
内容的提问来源于stack exchange,提问作者Jason Anderson
相关产品推荐
相关产品推荐

