当前位置:首页 > VUE

标签云实现vue

2026-01-18 15:35:50VUE

使用 vue-wordcloud 库实现标签云

vue-wordcloud 是一个基于 Vue.js 的标签云组件,使用简单且功能丰富。

安装依赖:

npm install vue-wordcloud d3-cloud

基本用法示例:

<template>
  <div>
    <word-cloud
      :data="words"
      nameKey="text"
      valueKey="value"
      :color="colors"
      :showTooltip="true"
    />
  </div>
</template>

<script>
import WordCloud from 'vue-wordcloud'

export default {
  components: { WordCloud },
  data() {
    return {
      words: [
        { text: 'Vue', value: 100 },
        { text: 'React', value: 80 },
        { text: 'Angular', value: 60 }
      ],
      colors: ['#1f77b4', '#ff7f0e', '#2ca02c']
    }
  }
}
</script>

自定义 SVG 标签云实现

对于需要高度自定义的场景,可以使用 SVG 手动实现。

<template>
  <svg :width="width" :height="height" class="word-cloud">
    <g :transform="`translate(${width/2},${height/2})`">
      <text
        v-for="(word, index) in cloudWords"
        :key="index"
        :transform="`translate(${word.x}, ${word.y}) rotate(${word.rotate})`"
        :font-size="word.size"
        :fill="colors[index % colors.length]"
        text-anchor="middle"
      >
        {{ word.text }}
      </text>
    </g>
  </svg>
</template>

<script>
import cloud from 'd3-cloud'

export default {
  props: {
    words: Array,
    width: { type: Number, default: 600 },
    height: { type: Number, default: 400 }
  },
  data() {
    return {
      colors: ['#4285F4', '#EA4335', '#FBBC05', '#34A853'],
      cloudWords: []
    }
  },
  mounted() {
    this.generateCloud()
  },
  methods: {
    generateCloud() {
      const layout = cloud()
        .size([this.width, this.height])
        .words(this.words.map(d => ({
          text: d.text,
          size: d.value * 2
        })))
        .padding(5)
        .rotate(() => (Math.random() * 2 - 1) * 30)
        .font('Impact')
        .fontSize(d => d.size)
        .on('end', words => {
          this.cloudWords = words
        })

      layout.start()
    }
  }
}
</script>

<style>
.word-cloud {
  font-family: 'Impact';
}
</style>

响应式标签云实现

结合 Vue 的响应式特性,实现动态更新的标签云。

<template>
  <div>
    <input v-model="newTag" @keyup.enter="addTag">
    <word-cloud :data="tags" nameKey="name" valueKey="count"/>
  </div>
</template>

<script>
import WordCloud from 'vue-wordcloud'

export default {
  components: { WordCloud },
  data() {
    return {
      newTag: '',
      tags: [
        { name: 'JavaScript', count: 50 },
        { name: 'Vue', count: 40 },
        { name: 'React', count: 30 }
      ]
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim()) {
        const existing = this.tags.find(t => t.name === this.newTag)
        if (existing) {
          existing.count++
        } else {
          this.tags.push({ name: this.newTag, count: 1 })
        }
        this.newTag = ''
      }
    }
  }
}
</script>

3D 标签云效果实现

使用 CSS 3D 变换创建更生动的视觉效果。

标签云实现vue

<template>
  <div class="cloud-container">
    <div 
      v-for="(word, index) in words"
      :key="index"
      class="tag"
      :style="{
        transform: `rotateY(${index * (360 / words.length)}deg) 
                   translateZ(${word.value * 2}px)`,
        fontSize: `${word.value}px`,
        color: getColor(index)
      }"
    >
      {{ word.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      words: [
        { text: 'HTML', value: 24 },
        { text: 'CSS', value: 28 },
        { text: 'JavaScript', value: 32 }
      ],
      colors: ['#E44D26', '#264DE4', '#F0DB4F']
    }
  },
  methods: {
    getColor(index) {
      return this.colors[index % this.colors.length]
    }
  }
}
</script>

<style>
.cloud-container {
  width: 300px;
  height: 300px;
  position: relative;
  perspective: 1000px;
  margin: 0 auto;
  transform-style: preserve-3d;
  animation: rotate 20s infinite linear;
}

.tag {
  position: absolute;
  top: 50%;
  left: 50%;
  transform-origin: 0 0;
  white-space: nowrap;
  font-weight: bold;
}

@keyframes rotate {
  from { transform: rotateY(0); }
  to { transform: rotateY(360deg); }
}
</style>

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

相关文章

vue 实现pdf

vue 实现pdf

在Vue中实现PDF功能 在Vue项目中实现PDF功能通常涉及PDF生成、预览或下载。以下是几种常见实现方式: 使用vue-pdf库预览PDF 安装依赖: npm install vue-pdf…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…