vue2-手写轮播图

 轮播图5长展示,点击指示器向右移动一个图片,每隔2秒移动一张照片!

 

<template>
    <div class="top-app">
        <div class="carousel-container">
            <div class="carousel" ref="carousel">
                <div v-for="(item, index) in imgUrl">
                    <img :src="item.url" :key="index">
                    <div>
                        {{ item.content }}
                    </div>
                </div>
            </div>
        </div>

        <div class="arrow left" @click="moveImg(indexImg - 1)">
            <img src="@/assets/logo.png" alt="left arrow">
        </div>
        <div class="arrow right" @click="moveImg(indexImg + 1)">
            <img src="@/assets/logo.png" alt="right arrow">
        </div>
    </div>
</template>
  
<script>
export default {
    name: 'topApp',
    data() {
        return {
            indexImg: 0,
            imgUrl: [{
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "1"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "2"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "3"
            }
                ,
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "4"
            }
                ,
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "5"
            }
                ,
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "6"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "7"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "8"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "9"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "10"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "11"
            },
            {
                url: 'https://ossdcd.nyncjxx.com/202305/19/1684490486433/f3e40d56315da571ddaa05f6d155e37.jpg',
                content: "12"
            }
            ]
        }
    },
    created() {
        setInterval(() => {
            this.moveImg(this.indexImg + 1);
        }, 2000);
    },
    methods: {
        moveImg(index) {
            // 一行展示几张图片,减几就行
            if (index > this.imgUrl.length - 5) {
                this.indexImg = -1;
            } else if (index < 0) {
                this.indexImg = 0;
            } else {
                if (this.$refs.carousel) {
                //这里是每张图片的宽度的倍数
                    this.$refs.carousel.style.transform = `translateX(-${index * 2}00px)`;
                    this.indexImg = index;
                }
            }

        }
    }
}
</script>
  
<style scoped>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.carousel-container {
    width: 1000px;//5长,一张图片200px这里就是1000
    height: 300px;
    margin: 10px auto;
    overflow: hidden;
    position: relative;
}

.carousel {
    width: 100%;
    height: 100%;
    display: flex;
    transition: 0.5s;
}

.carousel-container .carousel img {
    width: 200px;
    height: 200px;
}

.carousel-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.arrow {
    position: absolute;
    top: 30%;
    transform: translateY(-50%);
    width: 30px;
    height: 30px;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.arrow img {
    width: 15px;
    height: 15px;
    object-fit: contain;
    filter: invert(1);
}

.arrow.left {
    left: 20px;
}

.arrow.right {
    right: 20px;
}
</style>
  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值