vue实现蜂窝导航栏
Vue 实现蜂窝导航栏
蜂窝导航栏是一种视觉上吸引人的布局方式,通常由六边形组成。以下是实现蜂窝导航栏的几种方法:
使用 CSS 和 Flexbox 布局
通过 CSS 的 clip-path 属性可以创建六边形形状,再结合 Flexbox 布局实现蜂窝排列。
<template>
<div class="honeycomb-container">
<div v-for="(item, index) in items" :key="index" class="honeycomb-cell">
{{ item.name }}
</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ name: 'Home' },
{ name: 'About' },
{ name: 'Services' },
{ name: 'Contact' }
]
}
}
}
</script>
<style>
.honeycomb-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 20px auto;
width: 80%;
}
.honeycomb-cell {
width: 100px;
height: 110px;
margin: 0 5px 10px;
background-color: #3498db;
color: white;
display: flex;
align-items: center;
justify-content: center;
clip-path: polygon(
50% 0%,
100% 25%,
100% 75%,
50% 100%,
0% 75%,
0% 25%
);
}
</style>
使用 SVG 实现
SVG 提供了更精确的形状控制,适合需要复杂交互的场景。
<template>
<div class="honeycomb-svg">
<svg width="300" height="300" viewBox="0 0 300 300">
<polygon
v-for="(hex, index) in hexagons"
:key="index"
:points="hex.points"
fill="#3498db"
stroke="#2980b9"
stroke-width="2"
@click="handleClick(hex.id)"
/>
</svg>
</div>
</template>
<script>
export default {
data() {
return {
hexagons: [
{ id: 1, points: "150,50 200,75 200,125 150,150 100,125 100,75" },
{ id: 2, points: "150,150 200,175 200,225 150,250 100,225 100,175" }
]
}
},
methods: {
handleClick(id) {
console.log(`Hexagon ${id} clicked`);
}
}
}
</script>
使用第三方库
如果需要更复杂的蜂窝布局,可以考虑使用第三方库如 honeycomb-grid。
-
安装库:
npm install honeycomb-grid -
在 Vue 组件中使用:
<template> <div ref="gridContainer" class="grid-container"></div> </template>
export default { mounted() { const grid = Grid({ size: 50, orientation: 'flat' }); const hexagons = grid.rectangle({ width: 3, height: 3 });
hexagons.forEach(hex => {
const { x, y } = grid.hexToPixel(hex);
const points = grid.polygonCorners(hex).join(' ');
const polygon = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
polygon.setAttribute('points', points);
polygon.setAttribute('fill', '#3498db');
polygon.setAttribute('transform', `translate(${x}, ${y})`);
this.$refs.gridContainer.appendChild(polygon);
});
} }
.grid-container { width: 300px; height: 300px; margin: 0 auto; } ```响应式设计
为了使蜂窝导航栏适应不同屏幕尺寸,可以结合 CSS 媒体查询和动态计算。
<template>
<div class="responsive-honeycomb">
<div v-for="(item, index) in items" :key="index" class="hexagon">
{{ item.name }}
</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ name: 'Home' },
{ name: 'About' },
{ name: 'Services' }
]
}
}
}
</script>
<style>
.responsive-honeycomb {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.hexagon {
width: 80px;
height: 92px;
margin: 5px;
background-color: #3498db;
color: white;
display: flex;
align-items: center;
justify-content: center;
clip-path: polygon(
50% 0%,
100% 25%,
100% 75%,
50% 100%,
0% 75%,
0% 25%
);
}
@media (max-width: 768px) {
.hexagon {
width: 60px;
height: 69px;
}
}
</style>
动画效果
为蜂窝导航栏添加悬停动画可以增强用户体验。
<template>
<div class="animated-honeycomb">
<div v-for="(item, index) in items" :key="index" class="hexagon">
{{ item.name }}
</div>
</div>
</template>
<style>
.animated-honeycomb .hexagon {
transition: all 0.3s ease;
}
.animated-honeycomb .hexagon:hover {
transform: scale(1.1);
background-color: #2980b9;
}
</style>
以上方法提供了从简单到复杂的蜂窝导航栏实现方案,可以根据项目需求选择合适的方式。







