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


相关推荐

  • 宽度 & 深度学习 特点对比

    宽度 & 深度学习 特点对比宽度&深度学习特点对比推荐系统模型介绍实验过程与实验结果实验总结  为了提高神经网络的性能,是应该增加宽度呢?还是应该增加深度呢?增加宽度和增加深度各有什么样的效果呢?本文对论文《Wide&DeepLearningforRecommenderSystemsHeng-Tze》中关于宽度模型和深度模型的对比实验进行介绍。推荐系统  本论文基于推荐系统,推荐系…

    2022年5月11日
    35
  • 2021 PHP程序员修炼秘籍

    2021 PHP程序员修炼秘籍

    2022年2月18日
    45
  • Qt学习笔记#4:QTimer和QTime

    QTimerClassQTimer是一个计时器类它的使用分三步,创建对象,连接signal和slot函数,start()QTimer*timer=newQTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(update()));timer->start(1000);其中,SIGNAL(timeou

    2022年4月16日
    48
  • Java面试之字符串常量池「建议收藏」

    Java面试之字符串常量池「建议收藏」来源:https://segmentfault.com/a/1190000009888357作为最基础的引用数据类型,Java设计者为String提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三个问题,去理解字符串常量池:字符串常量池的设计意图是什么?字符串常量池在哪里?如何操作字符串常量池?字符串常量池的设计思想a.字符串的分…

    2022年7月28日
    10
  • 详解布隆过滤器的原理、优缺点

    详解布隆过滤器的原理、优缺点什么是布隆过滤器?本质上布隆过滤器是一种数据结构,比较巧妙的概率型数据结构(probabilisticdatastructure),特点是高效地插入和查询,可以用来告诉你“某样东西一定不存在或者可能存在”,它是用多个哈希函数,将一个数据映射到位图结构中。此种方式不仅可以提升查询效率,也可以节省大量的内存空间。HashMap的问题讲述布隆过滤器的原理之前,我们先思考一下,通常你判断某…

    2022年9月1日
    6
  • IDEA插件之颜值篇Material Theme UI

    俗话说,工欲善其事必先利其器。工具的颜值也很重要,好的主题让人赏心悦目,有码代码的欲望。今天推荐一个IDEA颜值类插件:MaterialThemeUI大致效果安装方式IDEA插件官网地址:https://plugins.jetbrains.com/plugin/8006-material-theme-uiGITHUB地址:https://github.com/C…

    2022年4月6日
    1.3K

发表回复

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

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