当前位置:首页 > VUE

vue实现商品卡片

2026-01-18 13:45:37VUE

Vue 实现商品卡片

在 Vue 中实现商品卡片可以通过组件化的方式完成,以下是一个完整的实现方案:

商品卡片组件

<template>
  <div class="product-card">
    <div class="product-image">
      <img :src="product.image" :alt="product.name" />
      <span v-if="product.discount" class="discount-tag">
        {{ product.discount }}% OFF
      </span>
    </div>
    <div class="product-info">
      <h3 class="product-name">{{ product.name }}</h3>
      <div class="price-section">
        <span class="current-price">${{ product.price }}</span>
        <span v-if="product.originalPrice" class="original-price">
          ${{ product.originalPrice }}
        </span>
      </div>
      <div class="rating">
        <span v-for="star in 5" :key="star">
          <i :class="star <= product.rating ? 'fas fa-star' : 'far fa-star'"></i>
        </span>
      </div>
      <button class="add-to-cart" @click="addToCart">Add to Cart</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    product: {
      type: Object,
      required: true
    }
  },
  methods: {
    addToCart() {
      this.$emit('add-to-cart', this.product)
    }
  }
}
</script>

<style scoped>
.product-card {
  width: 250px;
  border: 1px solid #eee;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
  margin: 10px;
}

.product-card:hover {
  transform: translateY(-5px);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.discount-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #ff5722;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 12px;
}

.product-info {
  padding: 15px;
}

.product-name {
  margin: 0 0 10px;
  font-size: 16px;
  color: #333;
}

.price-section {
  margin-bottom: 10px;
}

.current-price {
  font-size: 18px;
  font-weight: bold;
  color: #ff5722;
}

.original-price {
  font-size: 14px;
  text-decoration: line-through;
  color: #999;
  margin-left: 8px;
}

.rating {
  color: #ffc107;
  margin-bottom: 15px;
}

.add-to-cart {
  width: 100%;
  padding: 8px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.add-to-cart:hover {
  background-color: #45a049;
}
</style>

使用商品卡片组件

<template>
  <div class="product-list">
    <product-card 
      v-for="product in products" 
      :key="product.id" 
      :product="product"
      @add-to-cart="handleAddToCart"
    />
  </div>
</template>

<script>
import ProductCard from './ProductCard.vue'

export default {
  components: {
    ProductCard
  },
  data() {
    return {
      products: [
        {
          id: 1,
          name: 'Wireless Headphones',
          price: 99.99,
          originalPrice: 129.99,
          image: 'https://example.com/headphones.jpg',
          rating: 4,
          discount: 20
        },
        {
          id: 2,
          name: 'Smart Watch',
          price: 199.99,
          image: 'https://example.com/watch.jpg',
          rating: 5
        }
      ]
    }
  },
  methods: {
    handleAddToCart(product) {
      console.log('Added to cart:', product)
      // 这里可以添加购物车逻辑
    }
  }
}
</script>

<style>
.product-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
</style>

功能扩展建议

  1. 响应式设计:可以添加媒体查询使卡片在不同屏幕尺寸下自适应

  2. 动画效果:添加加载动画或悬停动画增强用户体验

  3. 骨架屏:在数据加载时显示骨架屏提高用户体验

  4. 懒加载:对图片实现懒加载优化性能

  5. 国际化:添加多语言支持

  6. 主题切换:实现暗黑模式等主题切换功能

    vue实现商品卡片

注意事项

  • 确保图片路径正确
  • 根据实际项目调整样式和结构
  • 产品数据应该来自API调用而非硬编码
  • 添加适当的错误处理机制

标签: 卡片商品
分享给朋友:

相关文章

vue实现卡片拖拽

vue实现卡片拖拽

Vue 实现卡片拖拽的方法 使用 HTML5 拖放 API HTML5 提供了原生拖放 API,结合 Vue 可以轻松实现拖拽功能。以下是一个基本实现示例: <template> &…

vue实现添加卡片

vue实现添加卡片

Vue 实现添加卡片功能 数据驱动渲染 在Vue中,卡片通常通过v-for循环数据数组渲染。定义cards数组存储卡片数据,每个卡片对象包含标题、内容等属性: data() { return {…

vue实现卡片效果

vue实现卡片效果

Vue实现卡片效果的方法 使用Vue实现卡片效果可以通过多种方式完成,包括使用原生CSS、UI框架或自定义组件。以下是几种常见方法: 使用原生CSS和Vue组件 创建一个简单的卡片组件,结合CSS实…

vue实现卡片左右滑动

vue实现卡片左右滑动

实现卡片左右滑动的方法 使用Vue实现卡片左右滑动可以通过多种方式,以下是几种常见的方法: 使用CSS和Touch事件 通过监听touchstart、touchmove和touchend事件…

vue实现商品详情讲解

vue实现商品详情讲解

Vue 实现商品详情页的核心步骤 搭建基础结构 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 vue-router、axios)。通过路由配置商品详情页路径(例如 /product…

vue实现商品列表总价

vue实现商品列表总价

实现商品列表总价计算 在Vue中实现商品列表总价计算,通常涉及数据绑定、计算属性和方法调用。以下是一种常见的实现方式: 数据准备 商品列表数据通常存储在组件的data或props中,每个商品对象应包…