• Vue组件>一个简单人物介绍轮播组件


    效果图

    在这里插入图片描述

    子组件

    <template>
      <div
        class="card_box"
        ref="cardbox"
        @mouseenter="swiperStop()"
        @mouseleave="swiper()"
      >
        <div
          class="card_item"
          :class="index == boxindex ? 'card_active' : 'card_actives'"
          v-for="(item, index) in list"
          :key="index"
          :style="{ backgroundImage: 'url(' + item + ')' }"
          @click="cardClick(index)"
        >
          <div>{{ index + 1 }}div>
        div>
      div>
    template>
    
    <script>
    export default {
      name: "cardbox",
      props: {
        //数据
        list: {
          type: Array,
          default: () => []
        },
        //自动播放
        bool: {
          type: Boolean,
          default: false
        }
      },
      data() {
        return {
          boxindex: 0,
          timer: null
        };
      },
      mounted() {
        this.swiper();
        this.$refs.cardbox.style.width = 300 + (this.list.length - 1) * 105 + "px";
      },
      methods: {
        //卡片点击事件
        cardClick(index) {
          this.boxindex = index;
        },
        //自动轮播
        swiper() {
          if (this.bool == true) {
            this.timer = setInterval(() => {
              this.boxindex++;
              if (this.boxindex > this.list.length - 1) {
                this.boxindex = 0;
              }
            }, 2000);
          }
        },
        //停止轮播
        swiperStop() {
          if (this.bool == true) {
            clearInterval(this.timer);
          }
        }
      },
      //去除轮播自动播放
      beforeDestroy() {
        this.swiperStop();
      }
    };
    script>
    
    <style lang="scss" scoped>
    .card_box {
      display: flex;
      align-items: center;
      //   overflow: hidden;
      .card_item {
        // width: 90px;
        height: 300px;
        margin-right: 15px;
        border-radius: 10px;
        box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3);
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        cursor: pointer;
        color: #fff;
        font-weight: 700;
        position: relative;
        div {
          position: absolute;
          bottom: 10px;
          width: 100%;
          text-align: center;
        }
      }
      .card_active {
        animation: card 0.5s cubic-bezier(0.39, 0.575, 0.565, 1) both;
      }
      @keyframes card {
        0% {
          width: 90px;
        }
        100% {
          width: 300px;
          border-radius: 15px;
        }
      }
      .card_actives {
        animation: cards 0.5s cubic-bezier(0.39, 0.575, 0.565, 1) both;
      }
      @keyframes cards {
        0% {
          width: 300px;
        }
        100% {
          width: 90px;
          border-radius: 10px;
        }
      }
    }
    style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127

    父组件

    //普通模式
     <card :list="listimg" >card>
    //自动播放
     <card :list="listimg" bool>card>
    <script>
    export default {
     data() {
        return {
        //图片数据
          listimg: [
            "https://s3.bmp.ovh/imgs/2022/07/25/3825abda2959a12c.jpg",
            "https://s3.bmp.ovh/imgs/2022/07/25/84dfb8476150d96f.jpg",
            "https://s3.bmp.ovh/imgs/2022/07/25/00c16a1b9f2dd206.jpg",
            "https://s3.bmp.ovh/imgs/2022/07/25/19c0f6bcff1017fc.jpg",
            "https://s3.bmp.ovh/imgs/2022/07/25/004af39d0666d17b.jpg"
          ]
        };
      },
       components: {
       //注册挂载之组件
       card: () => import("./components/card.vue"),
       }
      }
      script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    MATLAB基础学习笔记
    如何在 Java 中实现无向环和有向环的检测
    UE4-常见的宏-UFUNCTION
    [vue] .sync实现父子组件的双向绑定数据
    LandingSite电子标签Quuppa固件进入DFU状态说明
    Qt的简单应用:翻金币游戏( 含源码 )
    Python得到字符的阿斯克码值 chr ord
    贝叶斯定理,先验信念,似然,后验概率
    PyTorch - 大模型多卡训练 “CUDA error: an illegal memory access was encountered”
    基于智能分析网关的小区电动车AI检测方案设计与应用
  • 原文地址:https://blog.csdn.net/weixin_57748960/article/details/125994614