图片轮播—盒子翻转效果

本文介绍了如何使用CSS3的3D翻转效果创建一款炫酷的图片轮播,详细阐述了其运行原理和展示效果。
摘要由CSDN通过智能技术生成

炫酷的图片轮播

通过3D翻转效果,实现图片轮播

<template>
  <div id="carousel">
    <div class="block">
      <img v-for="item in img" :key="item" :src="item" :style="styleVar">
    </div>
    <ul>
      <li @click="a" :class="{active: index == 0}"></li>
      <li @click="b" :class="{active: index == 1}"></li>
      <li @click="c" :class="{active: index == 2}"></li>
      <li @click="d" :class="{active: index == 3}"></li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      img: [
        'https://pic3.zhimg.com/v2-cb8040f0ab6652ec6091eff75f5096d3_r.jpg?source=1940ef5c',
        'https://pic1.zhimg.com/v2-dc33c3e2a3664dffd5459bf832a8bacb_r.jpg?source=1940ef5c',
        'http://inews.gtimg.com/newsapp_bt/0/14119718995/1000',
        'https://pic1.zhimg.com/v2-f798ec3f3a171756cebf6491a4e45db3_r.jpg?source=1940ef5c'
      ],
      styleVar: {
        "--deg1": "0deg",
        "--deg2": "90deg",
        "--deg3": "180deg",
        "--deg4": "270deg"
     },
     deg: 0,
     timer: null,
     index: 0
    };
  },
  methods: {
    change() {
      if(this.deg == 270) {
        this.deg = -90
      }
      switch(this.deg) {
        case -90: this.index = 0; break;
        case 0: this.index = 1; break;
        case 90: this.index = 2; break;
        case 180: this.index = 3;break;
      }
      this.deg = this.deg + 90
      this.styleVar['--deg1'] = this.deg + 'deg'
      this.styleVar['--deg2'] = this.deg + 90 + 'deg'
      this.styleVar['--deg3'] = this.deg + 180 + 'deg'
      this.styleVar['--deg4'] = this.deg + 270 + 'deg'
    },
    a() {
      this.stop()
      this.deg = -90
      this.change()
      this.play()
    },
    b() {
      this.stop()
      this.deg = 0
      this.change()
      this.play()
    },
    c() {
      this.stop()
      this.deg = 90
      this.change()
      this.play()
    },
    d() {
      this.stop()
      this.deg = 180
      this.change()
      this.play()
    },
    play() {
      if(this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
      this.timer = setInterval(this.change, 3000)
    },
    stop() {
      clearInterval(this.timer)
      this.timer = null
    }
  },
  created() {
    this.play()
  }
}
</script>

<style scoped>
  #carousel {
    width: 100%;
    height: 580px;
    overflow: hidden;
  }
  .block {
    width: 1000px;
    height: 560px;
    margin: auto;
    display: flex;
    position: relative;
    transform-style: preserve-3d;
  }
  img {
    position: absolute;
    width: 1000px;
    height: 560px;
  }
  img:nth-child(1) {
    transform: rotateX(var(--deg1)) translateZ(280px);
    transition: transform 1s;
  }
  img:nth-child(2) {
    transform: rotateX(var(--deg2)) translateZ(280px);
    transition: transform 1s;
  }
  img:nth-child(3) {
    transform: rotateX(var(--deg3)) translateZ(280px);
    transition: transform 1s;
  }
  img:nth-child(4) {
    transform: rotateX(var(--deg4)) translateZ(280px);
    transition: transform 1s;
  }
  ul {
    width: 300px;
    display: flex;
    justify-content: space-evenly;
    margin: 5px auto;
    transform: translateZ(300px);
  }
  li {
    width: 50px;
    height: 5px;
    background: #949494;
  }
  li:hover {
    cursor:pointer
  }
  .active {
    background: cornflowerblue;
  }
</style>

运行效果

图片轮播—3D盒子翻转效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

呦呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值