当前位置:首页 > 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)增强交互体验。

vue实现收藏样式

<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>

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

相关文章

jquery样式

jquery样式

jQuery 样式操作基础 jQuery 提供了多种方法来操作 HTML 元素的样式,包括直接修改 CSS 属性、添加/移除类名等。以下是常见的样式操作方法: 修改 CSS 属性 使用 css()…

vue实现样式切换

vue实现样式切换

Vue 样式切换实现方法 动态 class 绑定 通过 v-bind:class 或简写 :class 实现动态样式切换,适用于需要根据条件切换多个 class 的场景: <template&…

vue实现点击添加样式

vue实现点击添加样式

实现点击添加样式的方法 在Vue中实现点击添加样式可以通过多种方式完成,以下是几种常见的方法: 使用v-bind:class动态绑定类名 通过v-bind:class可以动态切换CSS类名,结合点…

React中js如何添加元素样式

React中js如何添加元素样式

添加行内样式 在React中可以通过style属性直接添加行内样式,需要传递一个JavaScript对象。对象的属性名采用驼峰命名法,例如backgroundColor而非background-col…

怎么制作css链接样式

怎么制作css链接样式

基础链接样式设置 使用a选择器为所有链接设置基础样式,包括颜色、字体、下划线等属性: a { color: #0066cc; text-decoration: none; font-fa…

css制作样式

css制作样式

CSS 样式制作方法 CSS(层叠样式表)用于控制网页的样式和布局。以下是几种常见的 CSS 样式制作方法。 内联样式 直接在 HTML 元素的 style 属性中编写 CSS 规则。 <p…