前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue轮播图插件_element ui轮播图

vue轮播图插件_element ui轮播图

作者头像
全栈程序员站长
发布2022-08-04 12:36:10
3.9K0
发布2022-08-04 12:36:10
举报
文章被收录于专栏:全栈程序员必看

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

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

代码语言:javascript
复制
<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>

调用:

代码语言:javascript
复制
<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.cn/107072.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档