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


相关推荐

  • HTML点击按钮button跳转页面的几种实现方法

    HTML点击按钮button跳转页面的几种实现方法其实我比较喜欢第一种方法<buttononclick=”window.location.href=’../routeEdit/index.html'”type=”button”id=”add”>新增</button>方法一:在button标签中加上onclick属性,赋值为Javascript<inputtype=”button”name=”regi…

    2025年8月13日
    4
  • 御用导航官方网站提醒提示页_砼讯 | 河海大学官方网站全新改版上线!「建议收藏」

    御用导航官方网站提醒提示页_砼讯 | 河海大学官方网站全新改版上线!「建议收藏」上新了河海河海大学的官方网站上新啦!经过精心的筹备和技术人员不懈努力在105周年校庆来临之际河海大学官方网站全新改版上线!旧版网站全新改版新版网站高端大气的界面设计简洁明晰的板块分布河海元素的多重呈现超大大大图带来绝佳的视觉体验改版后的河海大学官网分为:河海新闻学术活动信息公告光影河海媒体河海五大板块信息门户、邮箱等在网站的右上角校园文化、校园景观、校园服务、图书档案、校历、校车等则在校…

    2022年5月30日
    161
  • pycharm 激活码 2021【2021.10最新】

    (pycharm 激活码 2021)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~2JTX0APX6F-eyJsaWNlbnNlSWQiOi…

    2022年3月30日
    50
  • php 自带过滤和转义函数

    php 自带过滤和转义函数

    2021年10月22日
    41
  • 实现图片懒加载的三种方式(前端路由懒加载原理)

    1.什么是图片懒加载图片懒加载就是鼠标滑动到哪里,图片加载到哪里。总的来说,一般页面打开,会同时加载页面所有的图片,如果页面的图片请求太多会造成很卡很慢的现象,为了避免这一现象,利用懒加载图片的方法,提高性能(典型:淘宝)2.实现图片懒加载的原理图片懒加载的实现原理:将图片的地址放在data-set属性中,由于图片并没有在src中,并不会发送http请求。比…

    2022年4月16日
    87
  • c# taskscheduler使用场合_hbase shell put

    c# taskscheduler使用场合_hbase shell put这里记录下TaskScheduler的简单用法。使用场景:在使用Task的时候,大家都知道用TaskFactory.StartNew可以用来创建一个Task。这里如果创建10个,那么这10个Task就各自放飞直接运行了。一般情况下是没什么大问题,如果这10个中的每个Task非常耗CPU或者内存,而公司的产品又是非常考验配置成本(比如一体机,移动设备等),就需要让这10个Task按照一定要求执行,比如串行执行,从而节省资源、让机器还可以顺畅去干别的事情。Task…

    2022年10月11日
    2

发表回复

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

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