超详细——vue.js 实现轮播图、JS实现轮播效果

首先看图,是不是你想要的结果(录制动画,把时间调快了)

在这里插入图片描述

代码:(vue.js、图片自己找)

vue官网:https://cn.vuejs.org/v2/guide/installation.html

或:直接引入vue.js:

  	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .img{
      width: 100%;
      height: 100%;
      position: absolute;
    }
    .imgItems{
      position: relative;
      width: 800px;
      height: 400px;
      margin: auto;
    }
    /*圆圈居中水平排列*/
    .imgIndexs{
      display: flex;
      justify-content: space-between;
      position: absolute;
      left: 360px;
      bottom: 5px;
      width: 80px;
    }
    /*图片所对应的圆圈*/
    .imgIndex{
      width: 8px;
      height: 8px;
      border: 1px solid #EEEEEE;
      border-radius: 50%;
      background-color: #EEEEEE;
      z-index: 100;
    }
    /*设置层级,显示图片*/
    .indexZ{
      z-index: 10;
    }
    /*选中对应的圆圈设置背景颜色*/
    .indexBGC{
      background-color: blue;
    }
  </style>
</head>
<body>

  <div id="app">
    <div class="imgItems" @mouseover="mouseOver" @mouseout="mouseOut">
      <img src="img1.png" alt="" class="img indexZ">
      <img src="img2.jpg" alt="" class="img">
      <img src="img3.jpg" alt="" class="img">
      <img src="img4.jpg" alt="" class="img">
      <img src="img5.jpg" alt="" class="img">

      <div class="imgIndexs">
        <div class="imgIndex" v-for="(item,index) in imgCount"
             @mouseover="indexBtn(index)" :class="{indexBGC:currentIndex == index}">
        </div>
      </div>
    </div>
  </div>

  <script src="../vuejs/vue.js"></script>
  <script>
   const app = new Vue({
     el:"#app",
     data:{
       imgCount:0, //图片个数
       currentIndex:0, //当前图片
       intervalID:'', //停止interval的唯一id
     },
     mounted(){
       this.imgCount = document.getElementsByTagName("img").length;
       setTimeout(()=>{
         // 开始播放
         this.startTime();
       },10)
     },
     methods:{
       /**
        * 鼠标移动到图片上
        */
       mouseOver(){
         this.stopTime()
       },
       /**
        * 鼠标移出图片
        */
       mouseOut(){
         this.startTime();
       },
       /**
        * 点击圆圈
        */
       indexBtn(index){
         this.currentIndex = index
         this.selectImg(index);
       },
       /**
        * 选中对应的图片设置层级类
        */
       selectImg(index){
         let imgList = document.getElementsByClassName("img");
         this.clearClass(imgList);
         imgList[index].className = "img indexZ"
       },
       /**
        * 层级复位
        */
       clearClass(array){
         for (let i=0 ; i<array.length ; i++){
           array[i].className = "img"
         }
       },

       /**
        * 开始循环播放图片
        */
       startTime(){
         this.intervalID = window.setInterval(()=>{
           this.currentIndex++;
           if(this.currentIndex > 4){
             this.currentIndex = 0
           }
           this.selectImg(this.currentIndex)
           },1000)
       },
       /**
        * 鼠标移到图片上时停止图片播放
        */
       stopTime(){
         window.clearInterval(this.intervalID)
       }
     }
   })
 </script>

</body>
</html>
  • 11
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue.js可以通过使用第三方插件或者自己编写组件来实现轮播图。以下是一个常用的方式: 1. 使用vue-awesome-swiper插件 vue-awesome-swiper是一个基于Swiper的轮播图插件,可以快速实现轮播图功能。 安装: ``` npm install vue-awesome-swiper --save ``` 使用: ```html <template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="item in list" :key="item.id"> <img :src="item.imgUrl" alt=""> </div> </div> <div class="swiper-pagination"></div> </div> </template> <script> import 'swiper/dist/css/swiper.css' import { swiper, swiperSlide } from 'vue-awesome-swiper' export default { components: { swiper, swiperSlide }, data() { return { list: [ { id: 1, imgUrl: 'http://xxx/1.jpg' }, { id: 2, imgUrl: 'http://xxx/2.jpg' }, { id: 3, imgUrl: 'http://xxx/3.jpg' } ], swiperOption: { pagination: { el: '.swiper-pagination' }, loop: true } } }, mounted() { console.log('mounted') } } </script> ``` 2. 自己编写组件 如果需要自定义轮播图的样式和交互,可以自己编写组件实现。 ```html <template> <div class="carousel"> <div class="carousel-item" v-for="(item, index) in list" :key="index"> <img :src="item.imgUrl" alt=""> </div> <div class="carousel-nav"> <span class="carousel-nav-item" v-for="(item, index) in list" :key="index" :class="{active: currentIndex === index}" @click="handleClick(index)"> </span> </div> </div> </template> <script> export default { data() { return { list: [ { id: 1, imgUrl: 'http://xxx/1.jpg' }, { id: 2, imgUrl: 'http://xxx/2.jpg' }, { id: 3, imgUrl: 'http://xxx/3.jpg' } ], currentIndex: 0 } }, mounted() { this.autoPlay() }, methods: { autoPlay() { setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.list.length }, 2000) }, handleClick(index) { this.currentIndex = index } } } </script> <style scoped> .carousel { position: relative; width: 500px; height: 300px; overflow: hidden; } .carousel-item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.3s; } .carousel-item.active { opacity: 1; } .carousel-nav { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); display: flex; } .carousel-nav-item { width: 10px; height: 10px; margin-right: 10px; border-radius: 50%; background-color: #fff; cursor: pointer; } .carousel-nav-item.active { background-color: #f00; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值