首页 > 其他分享 >用vue2封装轮播图组件

用vue2封装轮播图组件

时间:2023-05-02 16:13:54浏览次数:39  
标签:封装 轮播 color imgStatus background moveFun vue2 require center

日常练习

用vue2封装轮播图组件,传入图片信息数组。

实现思想:

  图片组添加translate动画,通过轮播到第几张图片作为参数,让图片组整体移动。

Carousel.vue
<template>
  <div class="carousel">
    <div class="carouselList">
      <!-- 图片列表 -->
      <ul class="img">
        <li v-for="(item,index) in imageList" :key="index" :style="move">
          <img :src="item.src" alt="">
        </li>
      </ul>
      <!-- 圆点列表 -->
      <ul class="circle">
        <li v-for="(item,index) in imageList.length" :key="index"
          :style="{'backgroundColor':index==imgStatus?'#ffffffc9':'#ffffff7b'}" @click="circleMove(index)"></li>
      </ul>
      <!-- 左箭头 -->
      <div class="leftArrow" @click="leftMove">&lt;</div>
      <!-- 右箭头 -->
      <div class="rightArrow" @click="rightMove">&gt;</div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Carousel',
    props: ['imageList'],
    data() {
      return {
        imgStatus: 0, //浏览到第几张图片
        move: null, //图片translate样式
        actionTimer: null, //防抖定时器
        beginTime: 0, //节流
        autoInterval: null //自动轮播定时器
      }
    },
    methods: {
      leftMove() {
        clearInterval(this.autoInterval);
        if (!this.actionTimer) {
          this.imgStatus -= 1;
          if (this.imgStatus <= 4 && this.imgStatus >= 0) this.moveFun();
          if (this.imgStatus == -1) this.imgStatus = 3;
          this.moveFun();
          this.actionTimer = setTimeout(() => {}, 1000);
        } else {
          if (this.actionTimer) clearTimeout(this.actionTimer);
          this.actionTimer = setTimeout(() => {
            this.imgStatus -= 1;
            if (this.imgStatus <= 4 && this.imgStatus >= 0) this.moveFun();
            if (this.imgStatus == -1) this.imgStatus = 3;
            this.moveFun();
          }, 1000);
        };
        this.autoMove();
      },
      rightMove() {
        clearInterval(this.autoInterval);
        let now = new Date().getTime()
        if (now - this.beginTime > 1000) {
          this.imgStatus += 1;
          if (this.imgStatus == 4) this.imgStatus = 0;
          this.moveFun();
          this.beginTime = now;
        };
        this.autoMove();
      },
      circleMove(index) {
        clearInterval(this.autoInterval);
        this.imgStatus = index;
        this.moveFun();
        this.autoMove();
      },
      moveFun() {
        this.move = `transform:translateX(-${this.imgStatus*490}px)`;
      },
      autoMove() {
        this.autoInterval = setInterval(() => {
          if (this.imgStatus >= 0 && this.imgStatus < 3) this.imgStatus += 1;
          else if (this.imgStatus == 3) this.imgStatus = 0;
          else if (this.imageList == -1) this.imgStatus = 0;
          this.moveFun()
        }, 2000);
      }
    },
    mounted() {
      this.autoMove();
    }
  }
</script>

<style scoped>
  .carousel {
    position: relative;
    width: 490px;
    background-color: lightblue;
    display: flex;
    align-items: center;
    justify-content: center;

  }

  .carouselList {
    background-color: lightcoral;
    overflow: hidden;
  }

  .carouselList .img {
    width: 400%;
  }

  .carouselList .img li {
    float: left;
    height: 290px;
    transition: transform 0.3s ease-in-out;
  }

  .carouselList .circle {
    position: absolute;
    bottom: 0;
    left: 195px;
    width: 100px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .carouselList .circle li {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: #ffffff7b;
    float: left;
    cursor: pointer;
  }

  .leftArrow {
    position: absolute;
    top: 125px;
    width: 50px;
    height: 50px;
    background-color: #cccccc68;
    font-size: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }

  .leftArrow:hover {
    background-color: #ccccccc3;
  }

  .rightArrow {
    position: absolute;
    top: 125px;
    right: 0px;
    width: 50px;
    height: 50px;
    background-color: #cccccc68;
    font-size: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }

  .rightArrow:hover {
    background-color: #ccccccc3;
  }
</style>

App.vue

<template>
  <div id="app">
    <Carousel :imageList="imageList"></Carousel>
  </div>
</template>

<script>
import Carousel from './components/Carousel.vue'
export default {
  name: 'App',
  components: {
    Carousel
  },
  data() {
    return {
      imageList: [{
            src: require('./images/1.png')
          },
          {
            src: require('./images/2.png')
          },
          {
            src: require('./images/3.png')
          },
          {
            src: require('./images/4.png')
          }
        ],
    }
  },
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
*{
  box-sizing: border-box;
  margin: 0 auto;
  padding: 0 ;
  user-select: none;
}
li{
  list-style: none;
}
</style>

 

 

开发遇到的问题

使用v-for循环图片时,item是图片路径,一开始设置为↓,但是找不到图片资源。

'../images/1.png'

后来将途径改为↓,生效。

require( '../images/1.png' ) 

原因

vue 项目通过webpack 的 devServer 运行之后,默认的 vue-cli 配置下,图片会被打包成 name.hash 的图片名,在这种情况下,如果我们使用固定的 字符串路径则无法找到该图片,所以需要使用 require 方法来返回 图片的编译路径。

在HTML中可以直接使用普通路径,但在js中需要通过require编译路径。

 

标签:封装,轮播,color,imgStatus,background,moveFun,vue2,require,center
From: https://www.cnblogs.com/karle/p/17367793.html

相关文章

  • Rust: CTP的rust版本如何手工封装
    https://blog.csdn.net/wowotuo/article/details/86669758这里指的手工封装,是指不用外部类似swig专用的库。一、库、配置1、DLL交互的库(1)libloadinghttps://github.com/nagisa/rust_libloading(2)libcRawFFIbindingstoplatformlibrarieslikelibc.https://github.com/rust-......
  • Cygwin、MSYS2 Bash封装函数restart,重启Windows程序进程向导,输入序号一键重启对应进程
    概述作用:终端输入restart,根据菜单提示输入序号重启特定的软件或进程,定义的重启函数太多不便于记忆的情况,特别是手机远程终端(如:JuiceSSH)连接的情况下,减少手动输入和误操作,其中判定浏览器进程使用了另一篇文章中预定的函数wmicps,ps2为指向wmicps的alias(详见:https://www.cnblog......
  • js封装深拷贝方法
    deepCopy:function(data){ if(data===null||data===undefined){ returnnull; } letresult=Array.isArray(data)?[]:{}; if(data&&typeofdata==='object'){ for(letkeyindata){ if(data[key]&&typeof......
  • jQuery轮播图(模仿滑动窗口算法)
    conststatus=["left:0px;","left:10px;","left:20px;","left:30px;","left:40px;",];constlist=$("#carousel>ul>li");constlen=lis......
  • vue2源码-十七、Vue组件间传值的方式及之间区别
    Vue组件间传值的方式及之间区别通过props传递:父组件传递数据给子组件使用//chilid,vueprops:{//字符串形式name:String//接收的类型参数//对象形式age:{type:Number,//接收的类型为数值defaule:18,//默认值为18r......
  • vue3 + ts + vite 封装 request
    npmiaxios目录 request.ts (直接复制可用)importaxiosfrom"axios";import{showMessage}from"./status";//引入状态码文件import{ElMessage}from"element-plus";//引入el提示框,这个项目里用什么组件库这里引什么//设置接口超时时间axios.defa......
  • Vue2.0和3.0区别
    一、项目初始化2.0初始化,vueinit<模板名称(webpack比较常用)>[项目名称]vueinitwebpackcli2-test3.0初始化,vuecreate[项目名称]vuecreatecli3-test二、目录结构对比2.0目录结构 3.0目录结构 3.0版本中项目环境变量配置文件没有了(dev.env.js/prod.env.js......
  • Vue2.0版本升级到vue3.0
    vue版本的升级主要步骤:一、首先需要卸载你之前的vue2.0版本输入cmd–>回车–>进入dos界面输入命令查询vue的版本:vue-Vorvue-Version输入命令卸载目前vue版本:npmuninstall-gvue-cli再输入vue版本查询命令,提示“不是可执行的命令”则表示卸载成功了。二、安装新......
  • JavaFx 生成二维码工具类封装
    原文地址:JavaFx生成二维码工具类封装-Stars-One的杂货小窝之前星之音乐下载器有需要生成二维码功能,当时用的是一个开源库来实现的,但是没过多久,发现那个库依赖太多,有个http-client的依赖,把软件都搞大了一倍,而且有时候开发的时候下载依赖还报错,就想换个方案于是在网上......
  • vue2源码-十六、异步组件
    异步组件Vue中异步组件的写法有很多,主要用作大的组件异步加载的markdown组件editor组件。就是先渲染一个注释标签,等组件加载完毕,最后再重新渲染forceUpdate(图片懒加载)使用异步组件会配合webpack原理:异步组件默认不会调用Vue.extend()方法所有Ctor上没有cid属性,没有cid属......