当前位置:首页 > VUE

vue实现tag效果

2026-02-11 09:41:43VUE

Vue 实现标签(Tag)效果的方法

基础标签组件实现

通过Vue组件封装可复用的标签样式,支持动态内容和自定义样式:

vue实现tag效果

<template>
  <span class="tag" :style="{ backgroundColor: color }">
    {{ text }}
    <span v-if="closable" class="close-icon" @click="$emit('close')">×</span>
  </span>
</template>

<script>
export default {
  props: {
    text: String,
    color: {
      type: String,
      default: '#409EFF'
    },
    closable: Boolean
  }
}
</script>

<style scoped>
.tag {
  display: inline-block;
  padding: 0 8px;
  height: 24px;
  line-height: 24px;
  border-radius: 4px;
  color: white;
  margin-right: 8px;
}
.close-icon {
  margin-left: 4px;
  cursor: pointer;
}
</style>

动态标签列表

结合v-for实现可增删的标签列表:

vue实现tag效果

<template>
  <div>
    <div v-for="(tag, index) in tags" :key="index">
      <Tag 
        :text="tag.text" 
        :color="tag.color"
        :closable="true"
        @close="removeTag(index)"
      />
    </div>
    <input 
      v-model="newTag"
      @keyup.enter="addTag"
      placeholder="输入标签后按回车"
    />
  </div>
</template>

<script>
import Tag from './Tag.vue'
export default {
  components: { Tag },
  data() {
    return {
      tags: [
        { text: 'Vue', color: '#42b983' },
        { text: 'JavaScript', color: '#f1c40f' }
      ],
      newTag: ''
    }
  },
  methods: {
    addTag() {
      if (this.newTag.trim()) {
        this.tags.push({
          text: this.newTag,
          color: `#${Math.floor(Math.random()*16777215).toString(16)}`
        })
        this.newTag = ''
      }
    },
    removeTag(index) {
      this.tags.splice(index, 1)
    }
  }
}
</script>

第三方库实现

使用成熟的UI库如Element UI的Tag组件:

<template>
  <el-tag
    v-for="tag in dynamicTags"
    :key="tag"
    closable
    :type="tag.type"
    @close="handleClose(tag)">
    {{ tag.name }}
  </el-tag>
</template>

<script>
export default {
  data() {
    return {
      dynamicTags: [
        { name: '标签一', type: '' },
        { name: '标签二', type: 'success' }
      ]
    }
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    }
  }
}
</script>

动画效果增强

为标签添加过渡动画:

<template>
  <transition-group name="tag-list" tag="div">
    <Tag 
      v-for="tag in tags"
      :key="tag.id"
      :text="tag.text"
      @close="removeTag(tag.id)"
    />
  </transition-group>
</template>

<style>
.tag-list-move {
  transition: transform 0.3s;
}
.tag-list-enter-active,
.tag-list-leave-active {
  transition: opacity 0.3s;
}
.tag-list-enter,
.tag-list-leave-to {
  opacity: 0;
}
</style>

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

相关文章

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="w…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用X…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @t…