资讯专栏INFORMATION COLUMN

Vue实现可复用轮播组件的方法

3403771864 / 237人阅读

  vue可以简单的就实现一个轮播图的基础功能,不仅重复用,且可抽离出来作为一个公共组件。

  html和js部分

  <template>
  <div
  class="img-box"
  ref="img-box"
  :style="{width: styles.width, height: styles.height}"
  >
  <div v-for="(item, index) in imgList"
  :key="index"
  class="img-item"
  :ref="'img-item-' + index"
  :class="{'active': index === active}"
  >
  <img
  :src="item"
  style="width:100%"
  :style="{height: styles.height}"
  />
  </div>
  <div
  class="img-position"
  v-if="isShowPosition"
  >
  <template v-for="(item, index) in imgList">
  <span :key="index"
  class="img-position-item"
  :ref="'img-position-' + index"
  :class="[
  {'active': index === active},
  isCircle ? 'circle' : '',
  isNums ? 'nums' : ''
  ]"
  @click="clickSpan(index)"
  >
  {{isNums ? index + 1 : ''}}
  </span>
  </template>
  </div>
  <div
  class="left-btn"
  v-if="isShowLeftOrRightBtn"
  @click="clickBtn('left')"
  >
  <i class="iconfont roll-zuo"></i>
  </div>
  <div
  class="right-btn"
  v-if="isShowLeftOrRightBtn"
  @click="clickBtn('right')"
  >
  <i class="iconfont roll-you"></i>
  </div>
  </div>
  </template>
  <script>
  export default {
  name: 'Roll',
  props: {
  imgList: { // 图片列表 src数组
  type: Array,
  default: () => []
  },
  isShowPosition: { // 是否显示下方小圆点
  type: Boolean,
  default: true
  },
  positionInner: { // 圆点内容
  type: String,
  default: 'circle' // 默认圆点,可选值 circle => 圆点 num => 数字 both => 
圆点+数字
  },
  isShowLeftOrRightBtn: { // 是否显示左右按钮
  type: Boolean,
  default: true
  },
  duration: { // 切换间隔
  type: [Number, String],
  default: 3000
  },
  styles: { // 自定义轮播图片宽高 默认500*300
  type: Object,
  default: () => {
  return {
  width: '500px',
  height: '300px'
  }
  }
  }
  },
  data () {
  return {
  active: 0, // 当前轮播图片
  timer: null // 定时器
  }
  },
  computed: {
  isCircle () {
  return ['circle', 'both'].includes(this.positionInner)
  },
  isNums () {
  return ['num', 'both'].includes(this.positionInner)
  }
  },
  updated () {
  if (this.timer) this.clearTimer()
  this.setTimer()
  },
  created () {
  this.setTimer()
  },
  methods: {
  clickSpan (index) {
  this.clearTimer()
  this.active = index
  },
  clickBtn (arg) {
  this.clearTimer()
  if (arg === 'left') {
  this.active = this.active - 1 < 0 ? this.imgList.length - 1 : 
this.active - 1
  } else {
  this.active = this.active + 1 === this.imgList.length ? 0 : this.active + 
1
  }
  this.setTimer()
  },
  setTimer () {
  this.timer = setInterval(() => {
  this.clickBtn('right')
  }, Number(this.duration))
  },
  clearTimer () {
  clearInterval(this.timer)
  this.timer = null
  }
  }
  }
  </script>

  css样式部分  

  <style scoped>
  @import url('//at.alicdn.com/t/font_1451815_senarwrqu6.css');
  * {
  margin: 0;
  padding: 0;
  }
  .img-box {
  position: relative;
  margin: 0 auto;
  }
  .img-item {
  height: 100%;
  width: 100%;
  opacity: 0;
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: -5;
  text-align: center;
  }
  .img-item.active {
  z-index: 0;
  opacity: 1;
  transition: .3s;
  }
  .img-position {
  position: absolute;
  bottom: 5px;
  left: 50%;
  display: flex;
  transform: translate(-50%, 0);
  }
  .img-position-item {
  display: inline-block;
  width:10px;
  height:10px;
  box-sizing: border-box;
  cursor: pointer;
  }
  .img-position-item.circle {
  border-radius: 50%;
  border: 1px solid #606266;
  }
  .img-position-item.nums {
  width: 18px;
  height: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #606266;
  font-size:14px;
  }
  .img-position-item:hover, .img-position-item.active {
  border-color: #d1d2d3;
  color: #d1d2d3;
  transition: .3s;
  }
  .img-position-item + .img-position-item {
  margin-left: 10px;
  }
  .left-btn, .right-btn {
  position: absolute;
  top: 50%;
  bottom: 0;
  width: 20px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: #d1d2d3;
  font-size: 20px;
  transform: translate(0, -50%);
  }
  .left-btn:hover, .right-btn:hover {
  color: #fff;
  transition: .3s;
  }
  .left-btn {
  left: 5px;
  }
  .right-btn {
  right: 5px;
  }
  </style>

  用上述方式只是用Vue作为一个组件,可以现在一个轮播图比较基础的部分,大家可以多多参照。


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/127614.html

相关文章

  • 面试官(6): 写过『通用前端组件』吗?

    摘要:很久没上掘金发现草稿箱里存了好几篇没发的文章最近梳理下发出来往期面试官系列如何实现深克隆面试官系列的实现面试官系列前端路由的实现面试官系列基于数据劫持的双向绑定优势所在面试官系列你为什么使用前端框架前言设计前端组件是最能考验开发者基本功的测 很久没上掘金,发现草稿箱里存了好几篇没发的文章,最近梳理下发出来 往期 面试官系列(1): 如何实现深克隆 面试官系列(2): Event Bus...

    waltr 评论0 收藏0
  • vue-music(1)音乐播发器 项目开发记录

    摘要:在中新建组件许文瑞正在吃屎。。。。在中添加如下代码三歌手组件开发歌手首页开发数据获取数据获取依旧从音乐官网获取歌手接口创建我们和以前一样,利用我们封装的等发放,来请求我们的接口,返回给。 Vue-Music 跟学一个网课老师做的仿原生音乐APP跟学的笔记,记录点滴,也希望对学习vue初学小伙伴有点帮助 showImg(https://segmentfault.com/img/remot...

    happen 评论0 收藏0
  • Vue 实现网易云音乐 WebApp

    摘要:基于等开发一款移动端音乐,界面参考了安卓版的网易云音乐布局适配常见移动端。图标使用阿里巴巴图标库,中间的唱片旋转动画使用了实现。搜索功能实现功能搜索歌手歌单歌曲热门搜索数据节流上拉刷新保存搜索记录。 基于 Vue(2.5) + vuex + vue-router + vue-axios +better-scroll + Scss + ES6 等开发一款移动端音乐 WebApp,UI ...

    Karuru 评论0 收藏0

发表评论

0条评论

3403771864

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<