vue幻灯片组件(一页多图)
加班到类似,就不一一介绍了,话不多说,直接上代码
<template> <div class="slider-body" ref="body"> <div class="toLeft" @click="toLeft"> <a-icon type="left"></a-icon> </div> <div class="slide-content" ref="content"> <!-- 就突出一个套娃 --> <div class="slide-chunk" v-for="(chunk, chunkindex) in Img_arr" :key="chunkindex" :style="chunk.length == 1 ? 'justify-content:center' : ''" > <el-image v-for="(item, index) in chunk" fit="cover" :src="img_url + item.filePath" :key="index"> </el-image> </div> </div> <div class="toRight" @click="toRight"> <a-icon type="right"></a-icon> </div> </div> </template> <script> export default { props: { //图片数组 imglist: { type: Array, default() { return [] }, }, //横向图片的数量(目前还没想好怎么给他弄成可以自定义几张的布局,目前只支持两张) hengxiang_number: { type: Number, default() { return 2 }, }, //是否自动滚动 autoplay: { type: Boolean, default() { return false }, }, //自动滚动时长 playtime: { type: Number, default() { return 3000 }, }, }, data() { return { img_url: process.env.VUE_APP_API_ASSET_URL, Img_arr: [], content_span: 0, } }, watch: { imglist: { deep: true, handler(v) { this.Img_arr = [] this.listFilter() }, }, }, mounted() { this.listFilter() let _this = this if (_this.autoplay) { setInterval(function () { let move_dis = _this.$refs.body.clientWidth let content_length = move_dis * _this.Img_arr.length console.log(_this.$refs) if (Math.abs(_this.content_span) !== content_length - move_dis) { _this.content_span -= move_dis _this.$refs.content.style.transform = `translateX(${_this.content_span}px)` } else { _this.content_span = 0 } }, _this.playtime) } }, methods: { //将图片数组切分为不同的块(比如10张图片,每页2张,会被分割为5页,11张会被分割为6页) listFilter() { let data = this.imglist for (var i = 0; i < data.length; i += this.hengxiang_number) { this.Img_arr.push(data.slice(i, i + this.hengxiang_number)) } }, toLeft() { //移动距离 = 块长度 let move_dis = this.$refs.body.clientWidth //滚动图片列表的长度 = 块长度*页面长度 let content_length = move_dis * this.Img_arr.length console.log(this.content_span, content_length - move_dis) // 向左最大距离 = 滚动图片列表的长度 - 最后一块的宽度 if (Math.abs(this.content_span) !== content_length - move_dis) { this.content_span -= move_dis this.$refs.content.style.transform = `translateX(${this.content_span}px)` } }, toRight() { //同上 let move_dis = this.$refs.body.clientWidth // 向左最大距离为0 if (this.content_span != 0) { this.content_span += move_dis this.$refs.content.style.transform = `translateX(${this.content_span}px)` } }, }, } </script> <style lang="less" scoped> .slider-body { width: 100%; overflow: hidden; line-height: 0; margin-top: 16px; position: relative; .toLeft, .toRight { position: absolute; z-index: 2004; top: 50%; width: 10%; font-size: 40px; color: rgba(240, 240, 240, 0.8); transform: translateY(-20px); cursor: pointer; } .toLeft { left: 0; height: 100%; } .toRight { right: 0; height: 100%; text-align: end; } } .slide-content { display: inline-flex; width: 100%; transition: all 0.8s; .slide-chunk { width: 100%; line-height: 0; display: flex; flex-shrink: 0; justify-content: space-between; .el-image { width: 48%; height: 240px; flex-shrink: 0; height: 160px; line-height: 0; background: #dddddd; } } } </style>
还不是太完善,等我不懒的时候再改
posted on 2022-04-10 14:11 你完全不写博客是吗? 阅读(69) 评论(0) 编辑 收藏 举报