当前位置:首页 > VUE

vue实现收藏样式

2026-01-08 13:39:45VUE

vue实现收藏样式

使用图标库实现收藏样式

安装图标库如font-awesomeelement-ui的图标组件,通过v-bind动态绑定类名或样式实现收藏与未收藏状态切换。

<template>
  <i 
    class="icon" 
    :class="isCollected ? 'fas fa-star' : 'far fa-star'" 
    @click="toggleCollect"
  ></i>
</template>

<script>
export default {
  data() {
    return {
      isCollected: false
    }
  },
  methods: {
    toggleCollect() {
      this.isCollected = !this.isCollected
    }
  }
}
</script>

<style>
.icon {
  color: gold;
  cursor: pointer;
  font-size: 24px;
}
</style>

使用CSS自定义样式

通过动态类名结合CSS实现自定义收藏样式,利用transformtransition添加动画效果。

<template>
  <div 
    class="collect-btn" 
    :class="{ 'collected': isCollected }" 
    @click="toggleCollect"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      isCollected: false
    }
  },
  methods: {
    toggleCollect() {
      this.isCollected = !this.isCollected
    }
  }
}
</script>

<style>
.collect-btn {
  width: 30px;
  height: 30px;
  background: url('uncollected-icon.png');
  cursor: pointer;
  transition: transform 0.3s;
}

.collect-btn.collected {
  background: url('collected-icon.png');
  transform: scale(1.2);
}
</style>

使用SVG实现动态填充

通过SVG路径和动态绑定fill属性,实现更灵活的收藏样式控制。

<template>
  <svg 
    width="24" 
    height="24" 
    viewBox="0 0 24 24" 
    @click="toggleCollect"
  >
    <path 
      :fill="isCollected ? 'gold' : 'none'" 
      stroke="gray" 
      stroke-width="1.5"
      d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"
    />
  </svg>
</template>

<script>
export default {
  data() {
    return {
      isCollected: false
    }
  },
  methods: {
    toggleCollect() {
      this.isCollected = !this.isCollected
    }
  }
}
</script>

结合Vuex管理收藏状态

在大型应用中,通过Vuex集中管理收藏状态,实现跨组件状态同步。

// store.js
export default new Vuex.Store({
  state: {
    collectedItems: []
  },
  mutations: {
    toggleCollect(state, itemId) {
      const index = state.collectedItems.indexOf(itemId)
      if (index === -1) {
        state.collectedItems.push(itemId)
      } else {
        state.collectedItems.splice(index, 1)
      }
    }
  }
})
<template>
  <button @click="toggleCollect(item.id)">
    {{ isCollected ? '已收藏' : '收藏' }}
  </button>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  props: ['item'],
  computed: {
    ...mapState(['collectedItems']),
    isCollected() {
      return this.collectedItems.includes(this.item.id)
    }
  },
  methods: {
    ...mapMutations(['toggleCollect'])
  }
}
</script>

添加收藏动画效果

通过Vue的transition组件或CSS动画库(如animate.css)增强交互体验。

<template>
  <transition name="bounce">
    <i 
      v-if="showIcon" 
      class="fas fa-heart" 
      :class="{ 'text-red-500': isCollected }"
      @click="handleCollect"
    ></i>
  </transition>
</template>

<style>
.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% { transform: scale(0); }
  50% { transform: scale(1.5); }
  100% { transform: scale(1); }
}
</style>

vue实现收藏样式

标签: 样式收藏
分享给朋友:

相关文章

vue 实现动态样式

vue 实现动态样式

在Vue中实现动态样式可以通过多种方式实现,以下是一些常见且灵活的方法: 绑定内联样式 使用v-bind:style(或简写:style)直接绑定样式对象,对象中的属性可以是动态的。例如:…

vue 实现收藏功能

vue 实现收藏功能

实现收藏功能的基本思路 在Vue中实现收藏功能通常涉及前端交互与后端数据存储的结合。核心逻辑包括:用户点击收藏按钮时切换状态,并通过API将状态同步到后端数据库。 前端组件实现 创建收藏按钮组件,…

网页制作 css样式

网页制作 css样式

CSS样式基础 CSS(层叠样式表)用于控制网页的视觉表现,包括布局、颜色、字体等。以下为关键知识点和实用方法: 选择器与样式规则 CSS通过选择器定位HTML元素并应用样式: /* 标签…

引入css样式制作网页

引入css样式制作网页

内联样式 直接在HTML元素的style属性中编写CSS,适用于单个元素的简单样式调整。 <p style="color: blue; font-size: 16px;">这是一段…

怎么制作css链接样式

怎么制作css链接样式

CSS链接样式制作方法 基础样式设置 链接的默认状态可以通过a选择器设置。常见的属性包括color、text-decoration和font-weight。 a { color: #0066cc…