vue实现自动滑动轮播图片

本文实例为大家分享了vue实现自动滑动轮播图片的具体代码,供大家参考,具体内容如下

效果如图:(悬浮时暂停,移出后自动轮播)

①创建图片滑动轮播组件ImageSlider.vue,可设置轮播间隔interval,当页面没被激活(用户没在浏览当前页面)时,自动暂停,重新浏览当前页面(被激活)时,自动轮播

<template>
  <div class="m-slider" @mouseenter="onStop" @mouseleave="onStart">
    <div class="m-panel" ref="slider" :style="`width: ${width}px;`">
      <div
        v-for="(item, index) in imageData"
        :key="index"
        class="m-image">
        <img :src="item.src" :alt="item.title"/>
        <p class="u-img-title" :title="item.title">{{ item.title }}</p>
      </div>
      <div class="m-image">
        <img :src="imageData[0].src" :alt="imageData[0].title"/>
        <p class="u-img-title" :title="imageData[0].title">{{ imageData[0].title }}</p>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'ImageSlider',
  props: {
    imageData: { // 图片数组
      type: Array,
      default: () => {
        return []
      }
    },
    interval: { // 滑动轮播间隔
      type: Number,
      default: 3000
    }
  },
  data () {
    return {
      visibilityChangeEvent: '',
      timer: null,
      imgWidth: 400, // 图片宽度,用于生成容器宽度
      activeIndex: 0 // 当前展示的图片
    }
  },
  computed: {
    width () {
      return (this.imageData.length + 1) * this.imgWidth
    }
  },
  created () {
    var hiddenProperty = 'hidden' in document ? 'hidden'
      : 'webkitHidden' in document ? 'webkitHidden'
        : 'mozHidden' in document ? 'mozHidden'
          : null
    this.visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange')
    var onVisibilityChange = () => {
      if (!document[hiddenProperty]) {
        this.onStart()
        console.log('页面激活')
      } else {
        this.onStop()
        console.log('页面非激活')
      }
    }
    document.addEventListener(this.visibilityChangeEvent, onVisibilityChange)
    setTimeout(() => {
      this.timer = setInterval(() => { // 自动切换
        this.onMove()
      }, this.interval)
    }, this.interval)
  },
  methods: {
    // 滑动动画效果
    slideEffect (target) {
      const offsetLeft = this.$refs.slider.offsetLeft // 求出元素的当前偏移位置
      let step = (target - offsetLeft) / 10 // 由快到慢的过渡效果
      step = step > 0 ? Math.ceil(step) : Math.floor(step) // 对每次移动的距离取整,ceil:向上取整,floor:向下取整
      setTimeout(() => {
        if (target !== offsetLeft) {
          this.$refs.slider.style.left = offsetLeft + step + 'px' // 移动
          this.slideEffect(target)
        }
      }, 15) // 每隔15ms执行一次
    },
    onSlider (moveX) {
      const offset = this.$refs.slider.offsetLeft
      const target = offset + moveX // 要移动的目标位置
      this.slideEffect(target)
    },
    onMove () {
      if (this.activeIndex === this.imageData.length - 1) { // 最后一张
        this.activeIndex = 0 // 显示第一张
        this.$refs.slider.style.left = 0
        this.onSlider(-this.imgWidth)
      } else {
        this.activeIndex++ // 显示下一张
        this.onSlider(-this.imgWidth)
      }
    },
    onStop () {
      clearInterval(this.timer)
      this.timer = null
    },
    onStart () {
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.onMove()
      }, this.interval)
    },
    beforeDestroy () {
      document.removeEventListener(this.visibilityChangeEvent)
      clearInterval(this.timer)
      this.timer = null
    }
  }
}
</script>
<style lang="less" scoped>
@themeColor: #D93844;
.m-slider {
  margin: 100px auto;
  width: 400px;
  height: 300px;
  overflow: hidden;
  position: relative;
  .m-panel {
    position: absolute;
    top: 0;
    left: 0;
    // width: 1600px; // (图片数组长度+1) * 图片宽度
    height: 300px;
    .m-image {
      float: left;
      width: 400px;
      height: 300px;
      img {
        width: 400px;
        height: 270px;
        cursor: pointer;
      }
      .u-img-title {
        width: 400px;
        font-size: 16px;
        color: #333;
        line-height: 30px;
        overflow: hidden;
        text-align: left;
        cursor: pointer;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
        &:hover {
          font-size: 16px;
          color: @themeColor;
        }
      }
    }
  }
}
</style>

②在要使用滑动轮播图片的页面引入使用组件,并传入图片数组

<ImageSlider :imageData="imageData" :interval="3000" />
import ImageSlider from '@/components/ImageSlider'
components: {
    ImageSlider
}
data () {
    return {
      imageData: [
        {
          title: 'image-1,image-1,image-1,image-1,image-1...',
          src: '图片地址'
        },
        {
          title: 'image-2,image-2,image-2,image-2,image-2...',
          src: '图片地址'
        },
        {
          title: 'image-3,image-3,image-3,image-3,image-3...',
          src: '图片地址'
        }
      ]
   }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 使用Vue制作图片轮播组件思路详解

    之前一直都没有认真的写过一个组件.以前在写业务代码的过程中,都是用的别人封装好的组件,这次尝试着写了一个图片轮播组件,虽然比不上知名的轮播组件,但它的功能基本完整,而且在写这个组件的过程中,学的东西也很多,在这里也给大家分享出来,如有疏漏,欢迎指正! 在制作这个组件之前,笔者google了不少关于轮播的文章,发现实现一个轮播的思路虽然各有不同,但是大的逻辑其实差不多,本文主要依据慕课网上焦点轮播图特效这节课,不过慕课网主要用原生JS写,而笔者则用Vue进行了重构,并且进行了一点修改.完成后的组件

  • Vue 过渡实现轮播图效果

    Vue 过渡 Vue 的过渡系统是内置的,在元素从 DOM 中插入或移除时自动应用过渡效果. 过渡的实现要在目标元素上使用 transition 属性,具体实现参考Vue2 过渡 下面例子中我们用到列表过渡,可以先学习一下官方的例子 要同时渲染整个列表,比如使用 v-for,我们需要用到 <transition-group> 组件 Vue 轮播图 我们先看这样一个列表 <ul> <li v-for="list in slideList"> <i

  • Vue项目中使用better-scroll实现一个轮播图自动播放功能

    前言 better-scroll是一个非常非常强大的第三方库 在移动端利用这个库 不仅可以实现一个非常类似原生ScrollView的效果 也可以实现一个轮播图的效果 这里就先记录一下自己实现这个效果的一些过程吧 思路 1.首先要确定自己的HTML结构 基本结构就是一个wrapper包含一个content 2.其次需要明白的一个页面可以滚动的原理在于 当内容的高度超出了容器的高度才可以实现滚动 如果没有超出 那么就没有滚动的必要 因此第一点需要实现的就是 获取到所有内容的高度 由于实现的是一个轮播

  • vue-music 使用better-scroll遇到轮播图不能自动轮播问题

    根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播 这是因为,better-scroll发布新版本之后,参数设置发生改变 这是旧版本: 组件为slider <template> <div class="slider" ref="slider"> <div class="slider-group" ref="sliderGroup"&

  • 基于vue.js实现图片轮播效果

    轮播图效果: 1.html <template> <div class="shuffling"> <div class="fouce fl"> <div class="focus"> <ul class="showimg"> <template v-for='sd in shufflingData'> <li v-if='shufflingId==$

  • 基于vue.js轮播组件vue-awesome-swiper实现轮播图

    一般做移动端轮播图的时候,最常用的就是Swiper插件了,而vue.js也有一个轮播组件vue-awesome-swiper,用法跟swiper相似. 1.安装vie-awesome-swiper nam install vue-awesome-swiper --save-dev 2.引用vie-awesome-swiper组件,这里我是用vie-cli创建的项目,在main.js: import VueAwesomeSwiper from 'vue-awesome-swiper'; Vue.u

  • vue.js+elementUI实现点击左右箭头切换头像功能(类似轮播图效果)

    1.效果图如下 2.vue代码如下 <el-carousel type="card" arrow="always" :loop="false" :initial-index="1" indicator-position="none" :autoplay="false"> <el-carousel-item v-for="(items, index) in it

  • Vue实现首页banner自动轮播效果

    本文实例为大家分享了Vue实现首页banner自动轮播的具体代码,供大家参考,具体内容如下 效果如图: ①创建Banner.vue组件,需传入banner数组,可设置轮播间隔ms <template>   <div class="m-banner-wrap" v-if="bannerData.length">     <div class="m-banner-list">       <div      

  • vue中引用swiper轮播插件的教程详解

    有时候我们需要在vue中使用轮播组件,如果是在vue组件中引入第三方组件的话,最好通过npm安装,从而进行统一安装包管理. 申明:本文所使用的是vue.2x版本. 通过npm安装插件:  npm install swiper --save-dev 在需要使用swiper的组件里引入swiper,swiper的初始化放在mounted里 Slider.vue源码: <template> <div class="swiper-container"> <div

  • Vue+ECharts实现中国地图的绘制及各省份自动轮播高亮显示

    目录 实现效果 完整代码+详细注释 要点小结 实现效果 完整代码+详细注释 <template> <div class="echart"> <div class="content"> <div id="map_cn"></div> </div> </div> </template> <script> import echarts from

随机推荐