Open willson-wang opened 6 years ago
<template> <div class="swiper-wrap" ref="swiper" :style="{height}"> <div class="swiper-item clearfix" :style="{width: width * list.length + 'px'}"> <div v-for="(item, index) in newList" :key="index"> <img :src="item.img" :alt="'banner' + index" :style="{width: width + 'px', height}"> </div> </div> <div class="swiper-dots"> <span v-for="(item, index) in list" :key="index" :class="[index === currentIndex ? 'active' : '']"></span> </div> <div class="swiper-prev" @click="changeItem('prev')"><</div> <div class="swiper-next" @click="changeItem('next')">></div> </div> </template> <script> import Swiper from './swiper' export default { name: 'swiper', props: { list: { type: Array, default: [] }, direction: { type: String, default: 'horizontal' }, showDots: { type: Boolean, default: false }, dotsPosition: { type: String, default: 'center' }, height: { type: String, default: '180px' }, auto: { type: Boolean, default: false }, loop: { type: Boolean, default: false }, duration: { type: Number, default: 300 }, interval: { type: Number, default: 2000 } }, computed: { width() { console.log(typeof window !== 'undefined', window.innerWidth); return typeof window !== 'undefined' && window.innerWidth }, newList() { if (this.loop) { const tempArr = JSON.parse(JSON.stringify(this.list)); tempArr.unshift(this.list[this.list.length - 1]); tempArr.push(this.list[0]); return tempArr; } return this.list; } }, data() { return { currentIndex: 0 } }, methods: { changeItem(key) { this.swiper.changeItem(key); } }, moundted() { const { direction, auto, loop, duration, interval } = this; const options = { direction, auto, loop, duration, interval, data: this.newList } this.swiper = new Swiper(this.$refs.swiper, options); } } </script> <style lang="less" scoped> .swiper-wrap { width: 100%; position: relative; overflow: hidden; .swiper-item { >div { float: left; } } .swiper-dots { position: absolute; left: 50%; bottom: 10px; transform: translate(-50%, 0); font-size: 0; span { display: inline-block; vertical-align: middle; width: 8px; height: 8px; border-radius: 50%; background-color: transparent; margin-right: 5px; border: 1px solid #fff; &:last-of-type { margin-right: 0; } } .active { background-color: rgb(0, 174, 133); border-color: rgb(0, 174, 133) } } .swiper-prev, .swiper-next { position: absolute; top: 50%; width: 30px; height: 30px; line-height: 30px; font-size: 20px; text-align: center; margin-top: -15px; border-radius: 50%; background-color: rgba(255, 255, 255, 0.8); } .swiper-prev { left: 5%; } .swiper-next { right: 5%; } } </style>
const DEFAULT_OPTIONS = { direction: 'horizontal', auto: false, loop: false, duration: 300, interval: 2000 } class Swiper { constructor(wrap, options) { this.options = Object.assign({}, DEFAULT_OPTIONS, options); this.wrap = wrap; this.initSwiper(); this.initEvent(); } initSwiper() { } initEvent() { } changeItem(key) { } } export default Swiper; autoPlay () { const { interval } = this.options this.timer1 && clearTimeout(this.timer1) var _move = () => { this.timer1 = setTimeout(() => { this.go(++this.currentIndex, 'next') _move() }, interval) } _move() } focusHandler () { this.focusTime = +new Date() console.log('focus', +new Date(), this.focusTime - this.blurTime) this.options.auto && this.autoPlay() } blurHandler () { this.blurTime = +new Date() console.log('blur', +new Date()) this.stop(); } init () { if (!this.data.length) return this.getListWidthsOrHeight() this.bindEvent() this.stop() this.options.auto && this.autoPlay() } recomputed() { const rect = this.$refs.swiper.getBoundingClientRect() this.width = rect.width + 'px' const initTransform = this.direction === 'horizontal' ? rect.width : parseInt(this.height) this.transformx = this.loop && this.isTransition ? `-${initTransform}px` : 0 this.left = this.loop && !this.isTransition ? `-${initTransform}` : 0 } mounted () { this.$nextTick(() => { this.recomputed() this.initSwiper() }) },