JavaScript---图片轮播(loop)多个设备展示图片一致,而非重新开始,根据当前时间计算展示图片

79 篇文章 3 订阅
18 篇文章 0 订阅

标题解释

简单说就是,根据当天的时间来动态切换图片src,不论设备多少,开机运行顺序。进去看到的保证都是在某个相同时间端内的图片是相同的,而非不同设备进去都是从0秒到duration秒采取切换图片!!!

需要的插件支持: dayjs、jquery(提前引入再去测试下面代码)

1.干货代码

<!-- 根据时间来展示切换图片src -->

    // 根据当前时间保证所有设备展示图片一致
    // 插件支持 dayjs
    // window.dayjs  cdn的话可以直接这样写,import、require的话需要挂载到window上
     function ComputedImgIndexOrLength(duration, length) {
      /**
       * duration 图片轮询间隔
       * length   图片长度
       */
      var defaultCount = typeof (duration * length) !== 'number' ? 0 : duration * length
      // 如果计算值非数字直接暂停,返回默认第一张
      if (!defaultCount) {
        console.error('===%s 当前值为 %s===', !duration ? '切换时间' : !length ? '图片长度' : '', !duration ? duration + '' : !length ? 'length' + '' : '')
        return 0
      }
      const currentTime = window.dayjs().hour() * 60 * 60 + window.dayjs().minute() * 60 + window.dayjs().second()
      console.log('===currentTime===', currentTime + 's')
      const reset = currentTime % (duration * length)
      //  循环一轮的总时长, duration * length
      console.log('===返回值为:%s===',  '索引值:' +  Math.floor(reset / duration))
      return Math.floor(reset / duration)
    }

效果图如下

修复上图切换图片有明显的不一致的情况,定时代码也贴出来,供大家学习使用!!!

2.定时器函数(关键点时定时器时间)

 // 这里说明下,300ms的调用时间我测试过,基本满足统一文件在不同时候不同人打开
 // 其展示的图片基本保持一致
 // 码友们测试时如果发现更加精确的时间可以评论区回复,看到会立马回复并检测修正!    

// 设置轮播图片
 function handleImgSwipper(duration, imgArr, id) {
      const length = imgArr.length
      if (window['timer' + id]) window.clearInterval(window['timer' + id]);
      $(`#img-swipper-${id}`).attr("src", imgArr[0]);
      window['timer' + id] = setInterval(function () {
$(`#img-swipper-${id}`).attr("src",imgArr[ComputedImgIndexOrLength(duration,length)]);
      }, 300);
}

 素材图片:

 

改动定时时间后的效果如下:

素材数组:
let imgAr = [
  'https://img-blog.csdnimg.cn/02c6efd47efc448bb799044359fe02c2.jpeg', 
   'https://img-blog.csdnimg.cn/06c0660e76ee437a81968267e51805cf.jpeg', 
   'https://img-blog.csdnimg.cn/74d08501d28f4770b2bbc74449fcd0a1.jpeg', 
   'https://img-blog.csdnimg.cn/3c9fc6160aa846f4a4bf70f65a0b11ba.jpeg'
]

使用函数:  记得先引入 jquery和dayjs

 // 设置轮播图片 id 的目的是当多个img标签控件使用是保证定时器和对应id盒子不会出现覆盖的情况
 // duration :  切换时长,单位:s
 // imgArr   :  图片数组
 // id       :  区分不同img标签盒子
    function handleImgSwipper(duration, imgArr, id) {
      const length = imgArr.length
      if (window['timer' + id]) window.clearInterval(window['timer' + id]);
      $(`#img-swipper-${id}`).attr("src", imgArr[0]);
      window['timer' + id] = setInterval(function () {
      $(`#img-swipper-${id}`).attr("src", imgArr[ComputedImgIndexOrLength(duration, length)]);
      }, 300);
    }

调用函数:handleImgSwipper(3, imgAr, 1)

看下图效果:

优化时间后效果图如下

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Vue图片轮播的代码示例: HTML模板: ```html <template> <div class="slider"> <div class="slider-images"> <img v-for="(image, index) in images" :src="image" :key="index"> </div> <div class="slider-controls"> <span class="slider-control" v-for="(image, index) in images" :key="index" @click="currentImage = index" :class="{ active: index === currentImage }"></span> </div> </div> </template> ``` CSS样式: ```css .slider { position: relative; width: 100%; height: 400px; } .slider-images { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .slider-images img { display: none; width: 100%; height: 100%; object-fit: cover; } .slider-controls { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); } .slider-control { display: inline-block; width: 10px; height: 10px; margin-right: 10px; border-radius: 50%; background-color: #999; cursor: pointer; } .slider-control.active { background-color: #fff; } ``` JavaScript代码: ```js export default { data() { return { currentImage: 0, images: [ 'https://picsum.photos/id/1015/600/400', 'https://picsum.photos/id/1016/600/400', 'https://picsum.photos/id/1018/600/400', 'https://picsum.photos/id/1019/600/400', ], }; }, mounted() { setInterval(() => { this.currentImage = (this.currentImage + 1) % this.images.length; }, 5000); }, }; ``` 说明: 上述代码中,`<template>` 部分定义了图片轮播的 HTML 结构,使用 `v-for` 指令根据数据循环渲染图片和圆点导航。`<style>` 部分定义了样式,使图片和导航圆点排列合理,并实现了基本的动画效果。`<script>` 部分定义了 Vue 组件的数据和方法,包括当前显示的图片序号 `currentImage` 和图片列表 `images`,以及自动轮播的定时器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

绝世唐门三哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值