Vue 手搓轮播效果

tiptop: 为啥需要写这个功能,因为我遇到了每个轮播层内要放3个左右的商品块,如果使用element自带的轮播就需要将一维数组切成二维数组,导致处理一些情况下就会变得很麻烦,当然那种我也写了如果你们有需要,在下方留言我会跟进代码。
使用我采用这种位移加动画模拟轮播来写这样数据还是一级数组到时候操作起来比较方便。
Vue 手搓轮播效果_第1张图片

DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Documenttitle>
    <style>
      .box {
        width: 1000px;
        height: 300px;
        margin: 0 auto;
        background-color: purple;
        overflow: hidden;
        display: flex;
        align-items: center;
        position: relative;
      }
      .box:hover .left,
      .box:hover .right {
        display: block;
      }
      .list {
        /* clac(注意这里 / 几就是想将每个div切成几个) */
        min-width: calc((100% - 20px) / 3);
        background-color: skyblue;
        height: 250px;
        margin-right: 10px;
        transition: 0.5s all ease-out;
      }
      .list:nth-child(3n) {
        margin-right: 0;
      }
      .left,
      .right {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 40px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        background-color: #fff;
        display: none;
        z-index: 666;
      }
      .left {
        left: 0;
      }
      .right {
        right: 0;
      }
    style>
  head>
  <body>
    <div id="app">
      <div class="box" ref="box">
        <div
          class="list"
          :style="{transform: `translateX(${containerLeft}px)`}"
          ref="list"
          v-for="v in 7"
          :key="v"
        >div>
        <div class="left" @click.stop="changeAddress('left')">div>
        <div class="right" @click.stop="changeAddress('right')">div>
      div>
    div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js">script>
    <script>
      var vm = new Vue({
        el: "#app",
        data() {
          return {
            containerLeft: 0,
          };
        },
        methods: {
          // 轮播功能
          changeAddress(no) {
            let total, is;

            this.$nextTick(() => {
              // 获取现在所有的list总长度
              total =
                this.$refs.list[0].clientWidth * (this.$refs.list.length - 1);
              // 获取盒子的宽度
              is = this.$refs.box.clientWidth;
              const isNo = {
                left: () => {
                  if (this.containerLeft == 0) {
                    return;
                  }
                  this.containerLeft += is;
                },
                right: () => {
                  if (total + this.containerLeft > 0) {
                    this.containerLeft -= is;
                  }
                },
              };
              isNo[no]();
            });
          },
        },
      });
    script>
  body>
html>

你可能感兴趣的:(vue.js,javascript,css3)