Vue购物车渲染案例

购物车渲染案例

在这里插入图片描述

<template>
  <div id="app">
    <h3>我的购物车</h3>
    <hr>
    <div class="list">
      <div @click.stop="current($event)" class="book" v-for="(item,index) in list" :key="index">
        <a href="javascript:;">
          <div class="pic">
            <img :src="item.img" alt="">
          </div>
          <h3>{{item.name}}</h3>
          <p>{{item.price}}</p>
        </a>
        <button @click="del(index)" class="del">X</button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      list: []
    }
  },
  methods: {
    async getbook () {
      const res = await axios({
        url: 'https://www.fastmock.site/mock/37d3b9f13a48d528a9339fbed1b81bd5/book/api/books',
        method: 'get'
      this.list = res.data.data
    },
    current (e) {
      if (!e.currentTarget.classList.contains('select')) {
        e.currentTarget.classList.add('select')
      } else {
        e.currentTarget.className = 'book'
      }
    },
    del (index) {
      this.list.splice(index, 1)
    }
  },
  created () {
    this.getbook()
  }
}
</script>

<style>
#app {
  width: 820px;
  height: 700px;
  background-color: rgb(241, 241, 241);
  margin: 0 auto;
  padding: 30px;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
}
html,
body {
  background-color: rgb(226, 225, 225);
}
h3 {
  text-align: center;
  margin: 0px;
  color: rgb(105, 105, 224);
  font-size: 16px;
}
a {
  display: inline-block;
  text-decoration: none;
}
.list {
  display: flex;
  flex-wrap: wrap;
  box-sizing: border-box;
  margin: 20px;
}
.book {
  position: relative;
  width: 170px;
  height: 220px;
  background-color: #fff;
  text-align: center;
  margin: 0 20px 20px 0;
  padding: 10px 15px 0;
  transition: all 0.2s linear;
  border: 2px solid #fff;
}
.book:hover {
  margin-top: -2px;
  box-shadow: 5px 2px 20px rgb(105, 104, 104);
}
p {
  margin: 10px 0;
}
.del {
  position: absolute;
  text-align: center;
  top: 5px;
  right: 2px;
  height: 20px;
  width: 22px;
  color: brown;
  background-color: rgb(241, 239, 239);
  border: none;
  outline: none;
}
.del:hover {
  background-color: orange;
  cursor: pointer;
}
img {
  width: 150px;
}
.select {
  border: 2px solid #ccc;
  box-shadow: 5px 2px 20px rgb(105, 104, 104);
}
.select:hover {
  box-shadow: none;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值