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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 树莓派3B 系统安装及初始化配置教程[通俗易懂]

    树莓派3B 系统安装及初始化配置教程[通俗易懂]本文仅供学习交流使用,如侵立删!企鹅:1033383881相关软件下载链接SD卡格式化工具、系统烧录工具、Raspbian系统镜像https://pan.baidu.com/s/1o5j_uD31hxLsPP–GRZ4Bw提取码:9nhv1.烧录系统1.1SD卡格式化安装SD卡格式化工具,格式化SD卡1.2写入系统镜像至SD卡点击写入后会有个确认覆盖弹窗提示,YES即…

    2022年6月25日
    28
  • 给力者基于51单片机的C语言教程,给力者单片机开发教程

    给力者基于51单片机的C语言教程,给力者单片机开发教程资源介绍给力者单片机开发教程给力者51单片机视频教程01:51单片机学前的准备工作1.mp4给力者51单片机视频教程01:51单片机学前的准备工作2.mp4给力者51单片机视频教程02:51单片机的C语言程序框架.mp4给力者51单片机视频教程03:51单片机的数字量输出1.mp4给力者51单片机视频教程03:51单片机的数字量输出2.mp4给力者51单片机视频教程04:51单片机的查表操作1.m…

    2022年6月7日
    28
  • kubernetes k8s删除namespaces 时一直处在Terminating的问题解决

    kubernetes k8s删除namespaces 时一直处在Terminating的问题解决

    2021年5月13日
    145
  • arm Linux_arch linux

    arm Linux_arch linux/* * __flush_dcache_all()* FlushthewholeD-cache. * Corruptedregisters:x0-x7,x9-x11 */ENTRY(__flush_dcache_all)//保证之前的访存指令的顺序   dsbsy                 //读cachelevelidregi

    2022年10月8日
    0
  • python深度学习库系列教程——python调用opencv库教程

    python深度学习库系列教程——python调用opencv库教程全栈工程师开发手册(作者:栾鹏)python教程全解OpenCV安装pipinstall–upgradesetuptoolspipinstallnumpyMatplotlibpipinstallopencv-pythonOpenCV的结构和Python一样,当前的OpenCV也有两个大版本,OpenCV2和OpenCV3。相比OpenCV2,OpenCV3提供了

    2022年9月26日
    0
  • c语言中strstr函数怎么实现_c语言strstr函数怎么写

    c语言中strstr函数怎么实现_c语言strstr函数怎么写首先,我们来看strstr函数的使用可见,strstr函数是用来查找字串的一个函数。因为字符串中有“cde”子串,所以代码运行起来结果就是”cdef”。现在,我们已经将strstr函数的作用大概了解了一下,下面开始函数的模拟实现。首先,在cplusplus中搜索strstr可以了解到她返回的类型及参数相关链接:strstr-C++Reference(cplusplus.com)接下来,我们分析实现思路直到s1到了下图的位置,两者相等当*s1==*s2时进入while循环,s1+

    2022年10月9日
    0

发表回复

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

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