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

vue3用js+css实现轮播图(可调整堆叠程度)

先看效果
在这里插入图片描述
html

 <divclass="outer"style="width: 650px;background: #fff;box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.1);border-radius: 15px;margin: 0 10px 15px 5px;">//这里用的是svg-icon,需要的可自行替换为其他图片<svg-iconid="btn_l"class="iconfont"style="cursor: pointer"v-show="imgList.length > 1"name="goleft"></svg-icon><svg-iconid="btn_r"class="iconfont"style="cursor: pointer"@click="frount()"v-show="imgList.length > 1"name="goright"></svg-icon><div class="wrapper" @mouseover="appMouseover()" @mouseout="appMouseout()"><!-- <el-carousel height="300px;" ><el-carousel-item v-for="(item, index) in imgList"  :key="index"><el-image style="width: 100%" :src="item.url" :fit="'fill'" /></el-carousel-item></el-carousel> --><divclass="divImg"v-for="(item, index) in imgList":style="appIsHover && current == index ? appStyle : ''"><img @click="appJump(item, index)" class="image" :src="item.url" /><span class="appName" @click="appJump(item, index)">{{ item.name }}</span></div></div></div>

js

//图片列表
const imgList = ref([{name: '',url: new URL('@/assets/banner3.png', import.meta.url).href},{name: '',url: new URL('@/assets/banner.png', import.meta.url).href},{name: '',url: new URL('@/assets/banner2.png', import.meta.url).href}
])const appStyle = 'width:330px!important;height:220px!important'
//鼠标悬浮时清除自动滚动,2秒后恢复
const appMouseover = (item, index) => {clearInterval(timer)
}
const appMouseout = (item, index) => {if (timer2) {clearTimeout(timer2)}timer2 = setTimeout(() => {autoplay()}, 2000)
}
//点击后直接跳到对应的图片
const appJump = (item, index) => {if (current == index) {}clearInterval(timer)if (timer2) {clearTimeout(timer2)}timer2 = setTimeout(() => {autoplay()}, 2000)current = indexfrount()
}const appJump = (item, index) => {if (current == index) {}clearInterval(timer)if (timer2) {clearTimeout(timer2)}timer2 = setTimeout(() => {autoplay()}, 2000)current = indexfrount()
}var imgs = document.getElementsByClassName('divImg')
var len = imgs.length
var current = 0var timer
var timer2
//页面初始调用
function wrapper() {imgs = document.getElementsByClassName('divImg')len = imgs.lengthcurrent = 0if (len != 0) {frount()bind()}autoplay()
}wrapper()
//点击图片跳转主要方法
function frount() {var mlen = Math.floor(len / 2)var limg, rimgfor (var i = 0; i < mlen; i++) {limg = len + current - i - 1rimg = current + i + 1if (limg >= len) {limg -= len}if (rimg >= len) {rimg -= len}// 这里70为叠加度,可自行调整imgs[limg].style.transform =`translateX(` + -70 * (i + 1) + `px) translateZ(` + (200 - i * 100) + `px) rotateY(30deg)`imgs[rimg].style.transform =`translateX(` + 70 * (i + 1) + `px) translateZ(` + (200 - i * 100) + `px) rotateY(-30deg)`}imgs[current].style.transform = `translateZ(300px)`
}
//给左右箭头添加点击效果
function bind() {// for (var i = 0; i < len; i++) {//     (function (i) {//         imgs[i].onclick = function () {//             current = i;//             frount();//         }//     })(i);// }var btn_l = document.getElementById('btn_l')var btn_r = document.getElementById('btn_r')btn_l.onclick = function () {clearInterval(timer)if (timer2) {clearTimeout(timer2)}timer2 = setTimeout(() => {autoplay()}, 2000)if (current <= 0) {current = len - 1} else {current--}frount()}btn_r.onclick = function () {clearInterval(timer)if (timer2) {clearTimeout(timer2)}timer2 = setTimeout(() => {autoplay()}, 2000)if (current >= len - 1) {current = 0} else {current++}frount()}
}
//自动播放
function autoplay() {timer = setInterval(function () {if (current >= len - 1) {current = 0} else {current++}frount()}, 2000)
}

css

.wrapper .divImg {float: left;width: 300px;height: 150px;position: absolute;left: 0;top: 0;right: 0;bottom: 0;margin: auto;border-radius: 8px;transition: transform 1s ease-in-out, width 0.5s ease-in-out, height 0.5s ease-in-out;cursor: pointer;
}
.wrapper .image {width: 100%;height: 100%;
}
.wrapper .appName {display: inline-block;position: absolute;bottom: 4px;width: 100px;font-size: 15px;color: #3fe2ff;text-align: center;left: calc(50% - 50px);
}.wrapper {position: relative;width: 100%;height: 100%;background-color: none;perspective: 800px;transform-style: preserve-3d;
}
#btn_r {position: absolute;right: 30px;top: 48%;// background: rgba(0, 0, 0, 0.1);border-radius: 50%;width: 30px;height: 30px;z-index: 999; 
}
.outer {color: #fff;position: relative;
}
#btn_l {position: absolute;left: 30px;top: 48%;// background: rgba(0, 0, 0, 0.1);border-radius: 50%;width: 30px;//  opacity: 0.5;height: 30px;z-index: 99999999999;
}
http://www.lqws.cn/news/520309.html

相关文章:

  • UI前端大数据处理技巧:如何高效处理海量异构数据?
  • DDNS-GO 使用教程:快速搭建属于自己的动态域名解析服务(Windows 版)
  • 如何在 Manjaro Linux 的图像界面上安装 Stremio 而不是使用命令行
  • 3 大语言模型预训练数据-3.2 数据处理-3.2.3 隐私消除——使用正则表示方法过滤个人隐私信息数据(包括邮件、电话、地址等)
  • 快速排序算法
  • 使用 Netty 实现 TCP 私有协议(解决粘包/拆包)
  • Python-文件管理
  • 领域驱动设计中的编程风格选择:面向对象与过程式的平衡艺术
  • 数学:向量的点积是什么?怎么计算?
  • 【EI会议征稿】东北大学主办第三届机器视觉、图像处理与影像技术国际会议(MVIPIT 2025)
  • 服务器开放端口如何设置,本地内网开通应用端口让外网访问连接步骤
  • OpenHarmony构建脚本build.sh解析
  • 【MongoDB】MongoDB从零开始详细教程 核心概念与原理 环境搭建 基础操作
  • 使用EasyExcel处理动态表头数据导入
  • AWS WebRTC:通过shell实现多进程启动viewer
  • Object.assign()
  • 获取YARN application 应用列表的几种方法
  • 2025年Java后端最新面试场景题 + 八股文高频面试题
  • Dagster数据管道构建指南:I/O管理与数据库连接实践
  • React Native【实战范例】账号管理(含转换分组列表数据的封装,分组折叠的实现,账号的增删改查,表单校验等)
  • rules写成动态
  • syncthing忘记密码怎么办(Mac版)?
  • 成都芯谷金融中心·文化科技园打造文化科技高地
  • 微服务思想与C++服务化框架
  • 跟着AI学习C#之项目实践Day7
  • sentinel 自定义 dashboard 用户名密码
  • 第⼀个与⼤模型交互的应⽤
  • Swagger 在 Spring Boot 中的详细使用指南
  • thinkphp8之文件上传
  • 用户体验驱动的3D设计:从功能实现到情感共鸣的设计升级