当前位置: 首页 > news >正文

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这里就是1000height: 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>

http://www.lryc.cn/news/278627.html

相关文章:

  • Google I/O大会:Android 13
  • VUE指令(一)
  • 微信小程序开发学习笔记《7》全局配置以及小程序窗口
  • Vue、uniApp、微信小程序、Html5等实现数缓存
  • 如何将ArcGIS工程文件迁移到ArcGIS Pro内
  • Jenkins基础篇--添加用户和用户权限设置
  • C语言基础内容(七)——第08章_C语言常用函数
  • CRM系统针对销售管理有哪些功能?如何帮助销售效率增长?
  • 基于Pixhawk和ROS搭建自主无人车(一):底盘控制篇
  • 部署 Spring Boot 应用中文文档
  • 【数据库原理】(23)实际应用中的查询优化方法
  • MySQL中datetime和timestamp的区别
  • 2024年如何使用WordPress构建克隆Udemy市场
  • (leetcode)Z字形变换 -- 模拟算法
  • STM32--基于STM32F103的MAX30102心率血氧测量
  • Qt/C++音视频开发63-设置视频旋转角度/支持0-90-180-270度旋转/自定义旋转角度
  • Python电能质量扰动信号分类(五)基于CNN-Transformer的一维信号分类模型
  • 基于Vue组合式API的实用工具集
  • 065:vue中将一维对象数组转换为二维对象数组
  • mysql 字符串分割
  • 解决Windows11 “我们无法设置移动热点”
  • python tcp socket中实现SSL/TLS认证
  • SQL-修改表操作
  • 【Node.js学习 day3——http模块】
  • 初探UAF漏洞(3)
  • C++学习笔记(二十一)
  • Java版企业电子招投标系统源代码,支持二次开发,采用Spring cloud技术
  • 01、Kafka ------ 下载、安装 ZooKeeper 和 Kafka
  • Spark: 检查数据倾斜的方法以及解决方法总结
  • 基于JavaWeb+BS架构+SpringBoot+Vue“共享书角”图书借还管理系统系统的设计和实现