vue实现图片轮播倒计时,天,时,分

<template>
  <div class="home-banner">
    <img class="home-banner-view" src="./img/主KV.png" alt="" />
    <img class="home-banner-IP" src="./img/IP.png" alt="" />
    <div class="home-banner_time">
      <div class="home-banner_time_box">
        <div class="home-banner_time_box-title">距离开幕式还有:</div>
        <div class="home-banner_time_box-time">
          <!-- <span v-for="item in timeLeft" :class="`time_${item}`" :key="item"></span> -->
          <div v-for="(item , index) in timeLeft" :class="`time_${item}`" :key="index"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        d: '',
        h: '',
        m: '',
        timeLeft: 'd + h + m', // 默认包含天,时,分6位数
        timer: null// 设置定时器默认为null
      }
    },
    methods: {
      toDou (n) { // n为行参,把对包含的字符串进行if判断
        if (n < 10) { // 设置补0,不然影响数组长度和顺序
          return '0' + n
        } else {
          return '' + n// 设置返回的一定是个字符串
        }
      },
      init () { // 在方法外面再创建一个方法开启定时器,是为了防止产生递归,会造成以2的n次方来同时开启定时器,达咩
        clearInterval(this.timer) // 每次开启前先关闭上次开启的定时器
        this.timer = setInterval(() => {
          this.countTime()
        }, 1000)
      },
      countTime: function () {
        const end = Date.parse(new Date('2021-4-29 14:10:10')) // 设置未来时间
        const now = Date.parse(new Date()) // 当前时间
        if (now >= end) { // 判断当前时间>=未来时间,因为时间到了,所以返回0
          this.d = 0
          this.h = 0
          this.m = 0
          return // 条件成立返回d,h,m为0
        }
        const msec = end - now// 设置msec为时差
        let d = parseInt(msec / 1000 / 60 / 60 / 24) // 一开始生成的时间的单位是毫秒,除以1000之后为1秒
        let h = parseInt(msec / 1000 / 60 / 60 % 24) // 以此类推,时差——秒——分——时-余24
        let m = parseInt(msec / 1000 / 60 % 60) // 时差——秒——分-余60
        this.timeLeft = this.toDou(d) + this.toDou(h) + this.toDou(m) // 通过转换设置该数组包含天,时,分6位数
      }
    },
    mounted () { // 在模板渲染成html后调用,通常是初始化页面完成后
      this.init()
    }
  }
</script>
<style lang="scss" scoped>
.home-banner {
  &-IP {
    position: fixed;
    right: 34px;
    bottom: 40px;
    z-index: 99;
  }
  &-view{
    width: 100%;
  }
  &_time {
    height: 162px;
    background: rgba(4, 8, 40, 1);
    &_box {
      width: 1200px;
      height: 100%;
      margin: 0 auto;
      display: flex;
      align-items: center;
      &-title {
        font-size: 32px;
        font-family: PingFangSC-Medium, PingFang SC;
        color: #ffffff;
        line-height: 45px;
      }
      &-time {
        margin-left: 20px;
        display: flex;
        :nth-child(3){
          margin-left: 37px;
        }
        :nth-child(4){
          margin-right: 48px;
        }
        div{
          width: 25px;
          height: 37px;
          margin-left: 5px;
        }
     }
    }
  }
}
  .time_0 {
    background-image: url("./img/0.png")
  }
  .time_1 {
    background-image: url("./img/1.png")
  }
  .time_2 {
    background-image: url("./img/2.png")
  }
  .time_3 {
    background-image: url("./img/3.png")
  }
  .time_4 {
    background-image: url("./img/4.png")
  }
  .time_5 {
    background-image: url("./img/5.png")
  }
  .time_6 {
    background-image: url("./img/6.png")
  }
  .time_7 {
    background-image: url("./img/7.png")
  }
  .time_8 {
    background-image: url("./img/8.png")
  }
  .time_9 {
    background-image: url('./img/9.png');
  }
</style>

图片效果:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Vue.js结合CSS3动画实现七张图片轮播图,具体实现步骤如下: 1. 在Vue实例中定义一个数组,存储七张图片的路径。 2. 使用v-for指令循环展示图片,并使用CSS3动画实现轮播效果。 3. 在Vue实例中定义一个计器,定更新图片的展示位置。 下面是完整的代码实现: ```html <template> <div class="carousel"> <div v-for="(img, index) in images" :key="index" class="img-container" :class="{'active': index === currentIndex}"> <img :src="img" alt=""> </div> </div> </template> <script> export default { data() { return { images: [ 'path/to/image1.jpg', 'path/to/image2.jpg', 'path/to/image3.jpg', 'path/to/image4.jpg', 'path/to/image5.jpg', 'path/to/image6.jpg', 'path/to/image7.jpg' ], currentIndex: 0, timer: null } }, mounted() { this.timer = setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.images.length }, 3000) }, beforeDestroy() { clearInterval(this.timer) } } </script> <style> .carousel { position: relative; width: 100%; height: 500px; overflow: hidden; } .img-container { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.5s ease-in-out; } .img-container.active { opacity: 1; } .img-container:nth-child(1) { animation: slide 21s infinite; } .img-container:nth-child(2) { animation: slide 21s infinite 3s; } .img-container:nth-child(3) { animation: slide 21s infinite 6s; } .img-container:nth-child(4) { animation: slide 21s infinite 9s; } .img-container:nth-child(5) { animation: slide 21s infinite 12s; } .img-container:nth-child(6) { animation: slide 21s infinite 15s; } .img-container:nth-child(7) { animation: slide 21s infinite 18s; } @keyframes slide { 0% { transform: translateX(0); } 20% { transform: translateX(0); } 25% { transform: translateX(-100%); } 45% { transform: translateX(-100%); } 50% { transform: translateX(-200%); } 70% { transform: translateX(-200%); } 75% { transform: translateX(-300%); } 95% { transform: translateX(-300%); } 100% { transform: translateX(-400%); } } </style> ``` 其中,CSS样式中的`.img-container`类实现了图片的定位和透明度控制,`.img-container.active`类控制当前展示图片的透明度为1,`@keyframes slide`定义了图片的滑动动画。 在Vue实例中,使用定实现了轮播效果,并在组件销毁前清除了计器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

火兰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值