当前位置:首页 > VUE

vue实现圆盘导航

2026-02-19 06:05:25VUE

Vue 实现圆盘导航

使用 CSS 和 Vue 动态计算位置

通过 Vue 的动态数据绑定和 CSS 的 transform 属性,可以实现圆盘导航的布局。核心思路是计算每个导航项在圆周上的位置。

vue实现圆盘导航

<template>
  <div class="circle-menu">
    <div 
      v-for="(item, index) in items" 
      :key="index"
      class="menu-item"
      :style="getItemStyle(index)"
      @click="handleClick(item)"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { label: '首页', path: '/' },
        { label: '产品', path: '/products' },
        { label: '服务', path: '/services' },
        { label: '关于', path: '/about' },
        { label: '联系', path: '/contact' }
      ],
      radius: 150 // 圆盘半径
    }
  },
  methods: {
    getItemStyle(index) {
      const angle = (index * 360 / this.items.length) - 90; // 从顶部开始
      const radian = angle * Math.PI / 180;
      return {
        left: `${this.radius + this.radius * Math.cos(radian)}px`,
        top: `${this.radius + this.radius * Math.sin(radian)}px`
      };
    },
    handleClick(item) {
      // 处理点击事件,例如路由跳转
      console.log('Navigating to:', item.path);
    }
  }
}
</script>

<style>
.circle-menu {
  position: relative;
  width: 300px;
  height: 300px;
  margin: 0 auto;
}
.menu-item {
  position: absolute;
  width: 60px;
  height: 60px;
  background: #42b983;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
  transform: translate(-50%, -50%);
  transition: all 0.3s ease;
}
.menu-item:hover {
  background: #33a06f;
  transform: translate(-50%, -50%) scale(1.1);
}
</style>

使用 SVG 实现圆盘导航

SVG 天然适合处理圆形布局,结合 Vue 可以动态生成导航项。

vue实现圆盘导航

<template>
  <svg width="300" height="300" viewBox="0 0 300 300">
    <circle 
      cx="150" 
      cy="150" 
      r="120" 
      fill="none" 
      stroke="#42b983" 
      stroke-width="2"
    />
    <g 
      v-for="(item, index) in items" 
      :key="index"
      @click="handleClick(item)"
      style="cursor: pointer"
    >
      <circle 
        :cx="getPosition(index).x" 
        :cy="getPosition(index).y" 
        r="20" 
        fill="#42b983"
      />
      <text 
        :x="getPosition(index).x" 
        :y="getPosition(index).y + 5" 
        text-anchor="middle" 
        fill="white"
      >
        {{ item.label }}
      </text>
    </g>
  </svg>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { label: '首页', path: '/' },
        { label: '产品', path: '/products' },
        { label: '服务', path: '/services' },
        { label: '关于', path: '/about' },
        { label: '联系', path: '/contact' }
      ],
      radius: 120,
      center: { x: 150, y: 150 }
    }
  },
  methods: {
    getPosition(index) {
      const angle = (index * 360 / this.items.length) - 90;
      const radian = angle * Math.PI / 180;
      return {
        x: this.center.x + this.radius * Math.cos(radian),
        y: this.center.y + this.radius * Math.sin(radian)
      };
    },
    handleClick(item) {
      console.log('Navigating to:', item.path);
    }
  }
}
</script>

使用第三方库(如 TweenMax)添加动画

为圆盘导航添加更流畅的动画效果,可以使用 GSAP 动画库。

<template>
  <div class="circle-menu">
    <div 
      v-for="(item, index) in items" 
      :key="index"
      class="menu-item"
      ref="items"
      @click="handleClick(item)"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
import { TweenMax } from 'gsap';

export default {
  data() {
    return {
      items: [
        { label: '首页', path: '/' },
        { label: '产品', path: '/products' },
        { label: '服务', path: '/services' },
        { label: '关于', path: '/about' },
        { label: '联系', path: '/contact' }
      ],
      radius: 150
    }
  },
  mounted() {
    this.arrangeItems();
  },
  methods: {
    arrangeItems() {
      this.$refs.items.forEach((item, index) => {
        const angle = (index * 360 / this.items.length) - 90;
        const radian = angle * Math.PI / 180;
        TweenMax.to(item, 0.5, {
          left: `${this.radius + this.radius * Math.cos(radian)}px`,
          top: `${this.radius + this.radius * Math.sin(radian)}px`,
          ease: 'back.out'
        });
      });
    },
    handleClick(item) {
      console.log('Navigating to:', item.path);
    }
  }
}
</script>

响应式圆盘导航

通过监听窗口大小变化,调整圆盘导航的尺寸和位置。

// 在 Vue 组件中添加
mounted() {
  window.addEventListener('resize', this.handleResize);
  this.handleResize();
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    const size = Math.min(window.innerWidth, window.innerHeight) * 0.8;
    this.radius = size / 2;
    this.$el.style.width = `${size}px`;
    this.$el.style.height = `${size}px`;
    this.arrangeItems();
  }
}

以上方法可以根据项目需求选择或组合使用,实现不同风格的圆盘导航效果。

标签: 圆盘vue
分享给朋友:

相关文章

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-i…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类…