vue轮播图插件_element ui轮播图

vue轮播图插件_element ui轮播图vue轮播图插件

大家好,又见面了,我是你们的朋友全栈君。

自己写了一个vue轮播图插件,自己感觉还可以,但不怎么熟悉vue希望大神们能指出错误或不好的地方。

效果:
vue轮播图插件_element ui轮播图

<template>
  <div class="vuecarousel">
    <div class="contain" 
         @mouseenter="stop" 
         @mouseleave="start"
         :style="{width: imgWidth + 'px', height: imgHeight + 'px'}"
         //显示区域(图片)大小
    >
      <ul class="ul">
        <li class="items" 
            v-for="(img, index) in imgs" :key="index"  
            v-show="index == showIndex"
        >
          <img :src="img.src" alt="轮播图">
        </li>
      </ul>
      <ul class="dots" 
          :style="{width: imgs.length * (dotWidth + 10) + 'px',  height: dotWidth + 'px'}"
          //显示小圆点容器大小
      >
        <li v-for="(img, index) in imgs" :key="index"  
            :class="index == showIndex ? 'active' : ''" 
            @click="showIndex = index"
            :style="{width: dotWidth + 'px', height: dotWidth + 'px'}"
            //显示小圆点大小
        >
        </li>
      </ul>
      <div class="control" v-show="show">
        <span class="left"  @click="previous"><</span>
        <span class="right" @click="next">></span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VueCarousel',
  created () {
    this.timer = setInterval(() => {
      this.next();
    }, this.delay)
  },
  beforeDestroy () {
   clearInterval(this.timer); 
  },
  props: {
    imgs:{
      type: Array,
      required: true
    },
    delay:{
      type: Number,
      default: function(){
        return 2000;
      }
    },
    imgWidth:{
      default: function(){
        return 400;
      }
    },
    imgHeight:{
      default: function(){
        return 302;
      }
    },
    dotWidth:{
      default: function(){
        return 20;
      }
    }
  },
  data(){
    return {
      showIndex: 0, //显示第几个图片
      timer: null,  // 定时器
      show: false   // 前后按钮显示
    }
  },
  methods: {
    previous(){
      if(this.showIndex <= 0){
        this.showIndex = this.imgs.length - 1;
      }else{
        this.showIndex --;
      }
    },
    next () {
      if(this.showIndex >= this.imgs.length - 1){
        this.showIndex = 0;
      }else{
        this.showIndex ++;
      }
    },
    start(){
      this.show = false;
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.next();
      }, this.delay)
    },
    stop () {
      this.show = true;
      clearInterval(this.timer);
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss" scoped>
.contain {
   position: relative;
   top: 50%;
   left: 50%;
   transition: all .8s;
   transform: translateX(-50%);
   color: #fff;
   overflow: hidden;
   cursor: pointer;
  .ul {
    height: 100%;
    list-style: none;
    .items {
      position: absolute;
      top: 0px;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .dots {
    position: absolute;
    left: 50%;
    bottom: 30px;
    height: 10px;
    transform: translateX(-50%);
    li {
      float: left;
      width: 10px;
      height: 10px;
      margin: 0px 5px;
      border-radius: 50%;
      transition: all .3s;
      background-color: antiquewhite;
      list-style: none;
    }
    .active {
      background-color: blue;
    }
  }
  .control {
    .left {
      position: absolute;
      top: 50%;
      left: 10px;
      padding: 5px;
      transform: translateY(-50%);
      font-size: 20px;
      cursor: pointer;
      &:hover {
        background-color: rgba($color: #000000, $alpha: 0.3);
      }
    }
    .right {
      position: absolute;
      top: 50%;
      right: 10px;
      padding: 5px;
      transform: translateY(-50%);
      font-size: 20px;
      cursor: pointer;
      &:hover {
        background-color: rgba($color: #000000, $alpha: 0.3);
      }
    }
  }
}
</style>

调用:

<template>
  <div id="app">
    <VueCarousel 
        :imgs="imgs"  //图片
        :delay="2000"  //延时
        :imgWidth="400"  //图片宽度
        :imgHeight="302"  //图片高度
        :dotWidth="20" //下方小圆点大小
    />
  </div>
</template>

<script>
import VueCarousel from './components/VueCarousel.vue'

export default {
  name: 'app',
  components: {
    VueCarousel
  },
  data () {
    return {
      imgs:[
            {src: require('@/static/images/1.png')},
            {src: require('@/static/images/2.png')},
            {src: require('@/static/images/3.png')},
            {src: require('@/static/images/4.png')},
            {src: require('@/static/images/5.png')}
           ]
    }
  }
}
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107072.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • centos7 top命令_top命令查看cpu使用率超过100%

    centos7 top命令_top命令查看cpu使用率超过100%top命令Linuxtop命令用于实时显示process的动态。top参数详解第一行,任务队列信息**系统当前时间:**13:52:56**系统开机后到现在的总运行时间:**up66

    2022年7月31日
    7
  • Mac 电脑连上 wifi 却打不开网页的解决办法

    Mac 电脑连上 wifi 却打不开网页的解决办法系统偏好设置–>网络–>高级–>代理–>取消代理☑️

    2022年6月22日
    99
  • 计算机网络基本知识汇总「建议收藏」

    计算机网络基本知识汇总「建议收藏」概述OSI分层(7层)物理层、数据链路层、网络层、运输层、会话层、表示层、应用层TCP/IP分层(4层)网络接口层、网络层、运输层、应用层五层协议(5层)物理层、数据链路层、网络层、运输层、应用层五层结构的概述应用层:通过应用进程间的交互来完成特定网络应用数据:报文协议:HTTP,SMTP(邮件),FTP(文件传送)运输层:向两个主机进程之间的通信提供通用的数据传输服务。

    2022年7月18日
    19
  • xsync 集群同步脚本「建议收藏」

    xsync 集群同步脚本「建议收藏」scp和rsync均是远程拷贝,rsync远程分发xsync集群分发脚本循环复制文件到所有节点相同的目录下新建一个脚本touchxsync.shvimxsync.sh#!/bin/bash#$#:表示传递给脚本或函数的参数个数。#1获取输入参数个数,如果没有参数,直接退出pcount=$#if((pcount==0));thenechonoargs…

    2022年6月2日
    53
  • bool函数忘记写return(false)会怎样[通俗易懂]

    bool函数忘记写return(false)会怎样[通俗易懂]http://hi.baidu.com/pope123/item/9d473e7f3ea341376f29f674这篇文章用汇编告诉了我返回值有可能是true也可能是false,但我看不懂,以后攻。 我为什么会接触到这个问题,请看实际例子:原来我漏写了代码中有颜色的两条语句,发现程序运行结果出乎我的意料。我原来以为不写就是默认返回false。其实 bool work()原来是voi

    2022年6月2日
    58
  • 2022年G3锅炉水处理找解析及G3锅炉水处理考试试卷[通俗易懂]

    题库来源:安全生产模拟考试一点通公众号小程序安全生产模拟考试一点通:G3锅炉水处理找解析是安全生产模拟考试一点通生成的,G3锅炉水处理证模拟考试题库是根据G3锅炉水处理最新版教材汇编出G3锅炉水处理仿真模拟考试。2022年G3锅炉水处理找解析及G3锅炉水处理考试试卷1、【多选题】水垢对锅炉的危害主要有浪费燃料()。(ABD)A、.损坏锅炉受热面B、.降低锅炉出力C、.减少供汽时间D、.缩短锅炉使用寿命E、.提高了环境温度2、【多选题】特种设备作业人员应当遵守()规…

    2022年4月14日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号