vue实现循环滚动图片

这篇文章主要为大家详细介绍了vue实现循环滚动图片,多图片轮播,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

<template>
    <div class="swiperBox">
        <img class="imgLeft" @click="clickLeft" src="../../../assets/img/左.png" alt="">
        <img class="imgRight" @click="clickRight" src="../../../assets/img/右.png" alt="">
        <div id="swiper">
            <div class="imgBox">
                <div class="imgDiv" v-for="(item,index) of imgList" :key="index">
                    <img :src="item" />
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'BaseSwiper',
    props: {
        speed: Number,
        direction: String,
    },
    data() {
        return {
            imgList: [
                require('../../../assets/img/组 14.png'),
                require('../../../assets/img/组 15.png'),
                require('../../../assets/img/组 17.png'),
                require('../../../assets/img/组 18.png'),
                require('../../../assets/img/组 24.png'),
            ],
            timer: null,
            theSpeed: this.speed,
            imgWidth: 260,
            theDirection: this.direction,
        }
    },
    methods: {
        clickLeft() {
            this.theDirection = 'left';
        },
        clickRight() {
            this.theDirection = 'right';
        },
    },
    mounted() {
        let imgBox = document.getElementsByClassName('imgBox')[0];
        //将imgBox下的图片进行拼接 循环展示图片
        imgBox.innerHTML += imgBox.innerHTML;
        let imgDiv = document.getElementsByClassName('imgDiv');
        imgBox.style.width = imgDiv.length * this.imgWidth + 'px';//设置div的宽度使图片可以放下
        let self = this;
        console.log(imgBox.offsetWidth,imgBox.style.width )
        function autoScroll() {
            if (imgBox.offsetLeft < -(imgBox.offsetWidth / 2)) {//提前更新left值,实现循环展示
                imgBox.style.left = 0;
            }
            if (imgBox.offsetLeft > 0) {//向右滚动 提前更新left值,实现循环展示
                imgBox.style.left = -(imgBox.offsetWidth / 2) + 'px';
            }
            if (self.theDirection == 'left') { //向左滚动,值为负
                self.theSpeed = -Math.abs(self.theSpeed)
            } else { //向右滚动
                self.theSpeed = Math.abs(self.theSpeed)
            }
            // 求出总的left值,等于left值加上移动的速度(px值)
            imgBox.style.left = imgBox.offsetLeft + self.theSpeed + 'px';
        }
        this.timer = setInterval(autoScroll, 30);//全局变量 ,保存返回的定时器
    },
    beforeDestroy() {
        clearInterval(this.timer);
        this.timer = null;
    }
}
</script>
<style scoped lang='less'>
.swiperBox {
    height: 100%;
    width: 100%;
    position: relative;
    .imgLeft {
        left: 0;
        top: 40%;
    }
    .imgLeft,
    .imgRight {
        width: 27px;
        height: 38px;
        position: absolute;
        cursor: pointer;
    }
    .imgRight {
        right: 0;
        top: 40%;
    }
    .directionIcon:hover {
        opacity: 1;
    }
    #swiper {
        width: 90%;
        height: 100%;
        margin: 0 auto;
        overflow: hidden;
        position: relative;
        .imgBox {
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            overflow: hidden;
            display: flex;
            .imgDiv {
                width: 100%;
                margin-left: 15px;
                img {
                    height: 100%;
                    width: 280px;
                    // width: 260px;
                    // height: 120px;
                }
            }
        }
    }
}
</style>

父组件调用,只贴出关键代码

<Swiper :speed="2" :direction="'left'"></Swiper>
 
//引用
import Swiper from '../BaseSwiper/BaseSwiper'
 
components: { Swiper },

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue实现图片无缝循环滚动并且可以手动滑动滚动,可以使用第三方的滑动组件库如 `vue-awesome-swiper`。以下是一个简单的实现示例: 首先,安装 `vue-awesome-swiper`: ```bash npm install vue-awesome-swiper --save ``` 然后,在Vue组件中引入并使用滑动组件: ```vue <template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="img in images" :key="img.id"> <img :src="img.src" :alt="img.alt"> </div> </div> <div class="swiper-scrollbar"></div> </div> </template> <script> import Vue from 'vue' import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(VueAwesomeSwiper) export default { data() { return { images: [ { id: 1, src: 'image1.jpg', alt: 'Image 1' }, { id: 2, src: 'image2.jpg', alt: 'Image 2' }, { id: 3, src: 'image3.jpg', alt: 'Image 3' }, { id: 4, src: 'image4.jpg', alt: 'Image 4' } ] } }, mounted() { new Swiper('.swiper-container', { loop: true, scrollbar: { el: '.swiper-scrollbar', hide: true } }) } } </script> <style> .swiper-container { width: 100%; height: 300px; } .swiper-slide img { width: 100%; height: 100%; object-fit: cover; } </style> ``` 在这个示例中,我们首先引入 `vue-awesome-swiper` 并将其注册到Vue中。然后,我们在模板中创建一个 `.swiper-container` 容器,并在其中嵌套一个 `.swiper-wrapper` 容器和一个 `.swiper-scrollbar` 容器。我们使用 `v-for` 指令循环遍历图片数组,并将每张图片作为一个 `.swiper-slide` 容器的子元素。 在 `mounted` 钩子函数中,我们使用 `new Swiper()` 函数创建一个新的滑动对象,并传入相关配置项。在这个示例中,我们设置了 `loop` 为 `true`,以实现图片的无缝循环滚动。我们还使用 `scrollbar` 属性添加了一个滚动条,并将其隐藏。 最后,我们在样式中设置了容器的宽度和高度,并将图片的 `object-fit` 属性设置为 `cover`,以便在容器中显示整张图片。 需要注意的是,以上示例中的滑动效果是自动播放的,如果需要手动滑动,则需要添加一些额外的配置项。例如,可以使用 `touchStartPreventDefault: false` 来禁止在触摸屏上阻止默认事件,以便支持手动滑动。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值