vue全屏轮播

自己造的一个Vue轮播图,支持全屏、无限滚动,功能有待完善

敬上代码,抛砖引玉

<template>
    <div class="swiper-wrap" ref="swiperWrap" :style='`height: ${config.height}px;`'>
        <div class="handle-direction">
            <span class="prve" @click="prve">&lt;</span>
            <span class="next" @click="next">&gt;</span>
        </div>
        <div class="showView" :style='`width: ${config.width}px; height: ${config.height}px;`'>
            <ul ref="list" class="list" :style='`left: -${left}px; transition: none`'>
                <li v-for="(item, i) in swiperList" :style='`width: ${config.width}px; height: ${config.height}px;`' :key="i">{{item}}</li>
            </ul>
        </div>
    </div>
</template>
 <script>
export default {
    data () {
        return {
            config: {
                height: 667,
                width: 1920,
                current: 1,
                transitionTime: 1,
                delay: 0
            },
            list: [1, 2, 3, 4]
        }
    },
    computed: {
        left () {
            return this.config.width * this.config.current
        },
        swiperList () {
            let swiperList = [this.list[this.list.length-1], ...this.list, this.list[0]]
            return swiperList
        }
    },
    mounted () {
        const $this = this
        this.resetWidth()
        window.addEventListener('resize', () => {
            $this.resetWidth()
        })
    },
    methods: {
        resetWidth () {
            const width = this.$refs.swiperWrap.clientWidth
            this.config.width = width
            this.config.height = width / 1920 * 667
        },
        prve () {
            if (new Date().valueOf() - this.config.delay <= this.config.transitionTime * 1000) {
                return
            }
            this.config.delay = new Date().valueOf()
            this.animation(false)
        },
        next () {
            if (new Date().valueOf() - this.config.delay <= this.config.transitionTime * 1000) {
                return
            }
            this.config.delay = new Date().valueOf()
            this.animation(true)
        },
        animation (isNext) {
            const $this = this
            const length = this.list.length
            let timer = null
            let $ul = this.$refs.list
            $ul.style.transition = `left ${this.config.transitionTime}s`
            isNext ? this.config.current ++ : this.config.current --
            clearTimeout(timer)
            if (this.config.current > length) {
                timer = setTimeout(() => {
                    $ul.style.transition = 'none'
                    $this.config.current = 1
                    clearTimeout(timer)
                }, 1050);
                timer = setTimeout(() => {
                    $ul.style.transition = `left ${this.config.transitionTime}s`
                    clearTimeout(timer)
                }, 1100);
            }
            if (this.config.current < 1) {
                timer = setTimeout(() => {
                    $ul.style.transition = 'none'
                    $this.config.current = length
                    clearTimeout(timer)
                }, 1050);
                timer = setTimeout(() => {
                    $ul.style.transition = `left ${this.config.transitionTime}s`
                    clearTimeout(timer)
                }, 1100);
            }
        }
    }
}
</script>
<style lang="stylus" scoped>
.swiper-wrap
    position relative
   &:hover
        .handle-direction
            transition opacity 0.3s
            opacity 1
.handle-direction
    opacity 0
    span
        display inline-block
        cursor pointer
        border-radius 3px
        width 20px
        height 20px
        line-height 20px
        background rgba(150, 150, 150, 0.2)
        z-index 2
        position absolute
        top 50%
        margin-top -10px
    .prve
        left 20px
    .next
        right 20px
.showView, .list
    position absolute
    top 0
    left 0
.showView
    overflow hidden
.list
    width 100000%
    text-align left
    li
        text-align center
        display inline-block
        &:nth-child(1)
            background #eee
        &:nth-child(2)
            background #bbb
        &:nth-child(3)
            background #ccc
        &:nth-child(4)
            background #ddd
        &:nth-child(5)
            background #eee
        &:nth-child(6)
            background #bbb
</style>

 

转载于:https://www.cnblogs.com/zhou195/p/9734780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值