Vue2+HTML5+CSS购物车实例:超实用购物车功能实现,附完整源代码和优惠券弹框」「新手必看!手把手教你用Vue2构建实用购物车——带有优惠券弹框的完整项目」「从零开始:Vue2 实现购物车功能

b站视频演示效果:

效果图:

完整代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Vue2 购物车界面</title>
  <script src="https://cdn.staticfile.net/vue/2.7.0/vue.min.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
    }
    .cart-container {
      width: 375px;
      margin: 20px auto;
      background-color: white;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
      padding: 10px;
    }
    .cart-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      border-bottom: 1px solid #ddd;
    }
    .cart-header h2 {
      font-size: 18px;
      font-weight: bold;
    }
    .cart-item {
      display: flex;
      align-items: center;
      padding: 10px 0;
      border-bottom: 1px solid #eee;
    }
    .cart-item img {
      width: 60px;
      height: 60px;
      border-radius: 4px;
      object-fit: cover;
    }
    .item-info {
      flex-grow: 1;
      margin-left: 10px;
    }
    .item-info h4 {
      font-size: 16px;
      margin-bottom: 5px;
    }
    .item-info p {
      color: #888;
      font-size: 14px;
    }
    .quantity-control {
      display: flex;
      align-items: center;
    }
    .quantity-control button {
      width: 25px;
      height: 25px;
      background-color: #ddd;
      border: none;
      cursor: pointer;
    }
    .quantity-control input {
      width: 40px;
      text-align: center;
      border: 1px solid #ddd;
      margin: 0 5px;
    }
    .recommend-section {
      background-color: #f5f5f5;
      padding: 10px;
    }
    .recommend-section h3 {
      margin-bottom: 10px;
    }
    .recommend-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 0;
    }
    .recommend-item img {
      width: 50px;
      height: 50px;
      border-radius: 4px;
    }
    .checkout-section {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px 10px;
      border-top: 1px solid #eee;
      background-color: #fff;
      margin-top: 10px;
    }
    .checkout-section button {
      background-color: #ff6700;
      color: white;
      padding: 10px 20px;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      cursor: pointer;
    }

    /* 弹框样式 */
    .overlay {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .coupon-modal {
      background-color: white;
      padding: 20px;
      border-radius: 8px;
      max-width: 300px;
      width: 100%;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    }
    .coupon-modal h3 {
      font-size: 18px;
      margin-bottom: 10px;
    }
    .coupon-list {
      list-style: none;
      padding: 0;
      margin: 0;
    }
    .coupon-item {
      padding: 10px;
      margin: 10px 0;
      border: 1px solid #ddd;
      cursor: pointer;
    }
    .coupon-item:hover {
      background-color: #f0f0f0;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="cart-container">
      <div class="cart-header">
        <h2>购物车</h2>
        <span @click="showCouponModal = true">优惠券({{ availableCoupons.length }})</span>
      </div>

      <div v-for="(item, index) in cartItems" :key="index" class="cart-item">
        <img :src="item.image" alt="商品图片">
        <div class="item-info">
          <h4>{{ item.name }}</h4>
          <p>¥ {{ item.price.toFixed(2) }}</p>
        </div>
        <div class="quantity-control">
          <button @click="decreaseQuantity(index)">-</button>
          <input type="text" v-model="item.quantity" readonly>
          <button @click="increaseQuantity(index)">+</button>
        </div>
      </div>

      <div class="recommend-section">
        <h3>您可能想买</h3>
        <div v-for="(recommend, index) in recommendedItems" :key="index" class="recommend-item">
          <img :src="recommend.image" alt="推荐商品">
          <div class="item-info">
            <h4>{{ recommend.name }}</h4>
            <p>¥ {{ recommend.price.toFixed(2) }}</p>
          </div>
          <button @click="addToCart(recommend)">+</button>
        </div>
      </div>

      <div class="checkout-section">
        <div>
          <p>券后合计: ¥ {{ finalPrice.toFixed(2) }}</p>
          <p>已减: ¥ {{ selectedDiscount.toFixed(2) }}</p>
        </div>
        <button @click="checkout">结算 ({{ totalItems }})</button>
      </div>
    </div>

    <!-- 优惠券弹框 -->
    <div v-if="showCouponModal" class="overlay" @click.self="showCouponModal = false">
      <div class="coupon-modal">
        <h3>选择优惠券</h3>
        <ul class="coupon-list">
          <li v-for="(coupon, index) in availableCoupons" :key="index" class="coupon-item" @click="selectCoupon(coupon)">
            {{ coupon.name }} - 减 ¥{{ coupon.discount }}
          </li>
        </ul>
        <button @click="showCouponModal = false">关闭</button>
      </div>
    </div>
  </div>

  <script>
    new Vue({
      el: '#app',
      data: {
        cartItems: [
          { name: '进口香蕉 600g', price: 7.60, quantity: 1, image: 'https://via.placeholder.com/60' },
          { name: '象牌 苏打水 325ml', price: 2.95, quantity: 2, image: 'https://via.placeholder.com/60' },
          { name: '洁柔抽纸 100张', price: 5.90, quantity: 1, image: 'https://via.placeholder.com/60' },
          { name: '乐事薯片 60g', price: 7.90, quantity: 3, image: 'https://via.placeholder.com/60' }
        ],
        recommendedItems: [
          { name: '进口香蕉 600g', price: 7.50, image: 'https://via.placeholder.com/50' },
          { name: '腰果 120g', price: 15.90, image: 'https://via.placeholder.com/50' },
          { name: '圣女果 500g', price: 9.80, image: 'https://via.placeholder.com/50' }
        ],
        availableCoupons: [
          { name: '满50减5', discount: 5 },
          { name: '满100减10', discount: 10 }
        ],
        selectedDiscount: 0,
        showCouponModal: false
      },
      computed: {
        totalPrice() {
          return this.cartItems.reduce((sum, item) => sum + item.price * item.quantity, 0);
        },
        finalPrice() {
          return this.totalPrice - this.selectedDiscount;
        },
        totalItems() {
          return this.cartItems.reduce((sum, item) => sum + item.quantity, 0);
        }
      },
      methods: {
        increaseQuantity(index) {
          this.cartItems[index].quantity++;
        },
        decreaseQuantity(index) {
          if (this.cartItems[index].quantity > 1) {
            this.cartItems[index].quantity--;
          }
        },
        addToCart(item) {
          const found = this.cartItems.find(cartItem => cartItem.name === item.name);
          if (found) {
            found.quantity++;
          } else {
            this.cartItems.push({ ...item, quantity: 1 });
          }
        },
        selectCoupon(coupon) {
          this.selectedDiscount = coupon.discount;
          this.showCouponModal = false;
        },
        checkout() {
          alert('结算功能还在开发中……');
        }
      }
    });
  </script>
</body>
</html>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

各位同学们:还有啥想看的功能或者特效不?欢迎在评论区留言哦!

本人承接网网站开发,如有需要,欢迎私信咨询!

如果您感觉文章对您有帮助~
那就打赏一下,请笔者喝杯咖啡吧~

给新手学习前端开发的建议:

  1. 了解基础知识

    • 学习HTML、CSS和JavaScript的基础知识。它们是前端开发的核心,构成了网页的基本结构和样式,以及交互功能。
    • 掌握HTML的标签和语义化,了解CSS的选择器和布局技巧,熟悉JavaScript的基本语法和DOM操作。
  2. 实践项目

    • 不要仅仅停留在理论学习上,通过实践项目来巩固和应用所学知识。
    • 可以从简单的静态页面开始,逐渐尝试添加交互效果和动态数据。
    • 参与开源项目或自己动手创建一个个人网站,将所学知识应用到实际场景中。
  3. 学习工具和框架

    • 了解并学习前端开发中常用的工具和框架,如构建工具(Webpack、Gulp等)、版本控制工具(Git)、前端框架(React、Vue、Angular等)。
    • 这些工具和框架能够提高开发效率,简化开发流程,是前端开发的重要组成部分。
  4. 关注前端趋势

    • 前端开发是一个快速发展的领域,新的技术和工具不断涌现。
    • 关注前端社区、博客和会议,了解最新的技术趋势和发展方向,保持学习的热情和动力。
  5. 培养解决问题的能力

    • 前端开发常常会遇到各种问题和挑战,学会独立思考和解决问题是非常重要的。
    • 遇到问题时,可以先尝试自己解决,通过查阅文档、搜索资料和社区讨论来找到答案。
    • 如果实在无法解决,可以向同事或社区求助,但也要学会总结和分享自己的经验和教训。
  6. 不断学习和提升

    • 前端开发是一个不断学习和提升的过程,要保持对知识的渴望和追求。
    • 可以通过阅读书籍、参加培训、参与开源项目等方式来不断提升自己的技能水平。
    • 同时,也要关注自己的职业发展,了解行业的需求和趋势,规划自己的职业道路。
  7. 注重代码质量和可维护性

    • 编写高质量的代码是前端开发的基本要求之一。
    • 学习并遵循代码规范,使用适当的命名和注释来提高代码的可读性。
    • 注重代码的结构和逻辑,避免过度嵌套和复杂的逻辑。
    • 考虑代码的可维护性,尽量编写可复用和可扩展的代码。
  8. 参与社区和交流

    • 加入前端开发的社区和论坛,与其他开发者进行交流和分享。
    • 通过参与社区活动、回答问题、分享经验等方式,不仅可以提升自己的技能水平,还可以结识更多的同行和朋友。

总之,学习前端开发需要耐心和毅力,要保持对技术的热情和兴趣,不断学习和提升自己。通过实践项目、学习工具和框架、关注前端趋势等方式,你可以逐渐成为一名优秀的前端开发者。

加油吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值