ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vue小练习 (二)

2021-10-02 15:03:25  阅读:170  来源: 互联网

标签:vue color margin 练习 padding background fff border


购物车渲染案例:

我做出的效果:

由于接口的问题,目前只有三本书

案例的要求:

  1. 鼠标经过时会有阴影和小动画
  2. 点击X 可以删除图书
  3. 从接口请求图书

 以下是我写的代码:

(写的不好请见谅)

因为代码太多,我只截了主要部分。

请求图书接口等其他数据我写在了vuex的store里

具体vuex的请求方式和使用方式,我在另一篇博客有仔细详述

<template>
  <div id="app">
    <h2>我的购物车</h2>
    <!-- <button @click="$store.dispatch('getBooks')">请求图书</button> -->
    <div class="list">
      <!-- v-for="(循环变量,索引变量 in 数组)" -->
      <div
        v-for="(item, index) in $store.state.books"
        :key="index"
        class="book"
        :title="item.name"
      >
        <a href="" @click.prevent>
          <div class="Img">
            <img :src="item.img" :alt="item.name" />
          </div>
          <!-- 动态绑定属性值 -->
          <h4 class="title" v-bind:title="item.name">{{ item.name }}</h4>
          <p class="desc"></p>
          <p class="price">
            <span class="num">{{ item.price }}</span>
          </p>
        </a>
        <button class="btn" title="移出购物车" @click="del(index)">X</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    del (index) {
      console.log('删除', index)
      // splice移除boosks数组下标为index
      this.$store.state.books.splice(index, 1)
    }
  }
}
</script>

<style scoped>
h4,
p {
  margin: 0;
  font-weight: normal;
}

a {
  text-decoration: none;
}

body {
  background-color: #eee;
}

#app {
  background-color: #fff;
  width: 800px;
  height: 700px;
  margin: 50px auto;
  text-align: center;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.5);
  padding: 1em 2em;
}

.list {
  padding: 1em;
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
}

.book {
  position: relative;
  flex-basis: 22%;
  margin-right: 2%;
  margin-bottom: 1em;
  text-align: center;
  background-color: #fff;
  padding: 1em 0;
  transition: all 0.2s linear;
  border: 2px solid #fff;
}

.book:hover {
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  transform: translate3d(0, -2px, 0);
}

.title {
  line-height: 2em;
  margin: 0;
  padding: 0;
  color: #000;
}

.Img img {
  width: 150px;
  height: 150px;
}

.desc {
  margin: 0;
  font-size: 16px;
}

.price {
  margin: 0;
  font-size: 14px;
}

.btn {
  cursor: pointer;
  position: absolute;
  right: 0.8em;
  top: 0.8em;
  border: none;
  color: #ff6700;
  padding: 0.5em 0.5em;
  font-size: 12px;
  user-select: none;
  outline: 0 none !important;
}
.btn:active {
  border: none;
}

.btn:hover {
  background-color: #ff6700;
  color: #fff;
}

.selected {
  border: 2px solid #ccc;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
}

.cart {
  border: 1px solid #eee;
  background-color: #fff;
  text-align: center;
  position: fixed;
  padding: 1em;
  right: 100px;
  top: 200px;
}
</style>

 

标签:vue,color,margin,练习,padding,background,fff,border
来源: https://blog.csdn.net/JINGinC/article/details/120577937

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有