使用swiper+动画实现轮播图自动播放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | <! DOCTYPE html> < html lang="en"> < head > < meta charset="UTF-8"> < meta http-equiv="X-UA-Compatible" content="IE=edge"> < meta name="viewport" content="width=device-width, initial-scale=1.0"> < title >Document</ title > <!-- <link rel="stylesheet" href="swiper-6.6.2/swiper-master/package/swiper-bundle.css"> <link rel="stylesheet" href="swiper-6.6.2/swiper-master/package/swiper-bundle.min.css"> <script src="swiper-6.6.2/swiper-master/package/swiper-bundle.js"></script> <script src="swiper-6.6.2/swiper-master/package/swiper-bundle.min.js"></script> --> < link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.css"> < link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"> < script src="https://unpkg.com/swiper/swiper-bundle.js"> </ script > < script src="https://unpkg.com/swiper/swiper-bundle.min.js"> </ script > </ head > < body > <!-- 用来盛整体内容的盒子 --> < div class="swiper-container container"> < div class="header"> < div class="headContent">我是轮播图</ div > </ div > <!-- 用来盛轮播图的盒子 --> < div class="swiper-wrapper"> <!-- 用来盛轮播图图片的盒子 --> < div class="swiper-slide"> < img class="moveImg" src="./image/logo1.jpg" alt=""> </ div > < div class="swiper-slide"> < img class="moveImg" src="./image/logo2.jpg" alt=""> </ div > </ div > <!-- 轮播图的下一页按钮 --> <!-- <div class="swiper-button-next"></div> --> <!-- 轮播图的上一页按钮 --> <!-- <div class="swiper-button-prev"></div> --> <!-- 轮播图的底下的导航小点点按钮 --> <!-- <div class="swiper-pagination"></div> --> < div class="foot"> < div class="footImg"> < img class="footImgStyle" src="./image/logoWhite.png"> </ div > < div class="footContent">版权所有 山ICP备160577863号</ div > </ div > </ div > < script > var ishidden=true var swiper = new Swiper(".container", { //绑定上下页切换按钮 navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev" }, //设置自动轮播切换 loop: true, speed:2500, autoplay: { delay: 4000, disableOnInteraction: false, waitForTransition: false, }, disableOnInteraction: true, //设置图片切换的形式 // effect : 'flip', effect : 'fade', fadeEffect: { crossFade: true, }, //添加分页小点点按钮功能 pagination: { el: ".swiper-pagination", clickable: true, } }) //鼠标移入时停止自动轮播 document.querySelector(".container").onmouseover = function () { // swiper.autoplay.stop(); } //鼠标移出时开始自动轮播 document.querySelector(".container").onmouseout = function () { // swiper.autoplay.start(); } //给分页小点点按钮绑定事件 for (var i = 0; i < swiper.pagination.bullets.length ; i++) { swiper.pagination.bullets[i].onmouseover = function () { this.click(); } } </script> < style > html{ height: 100%; } body{ height: 100%; margin: 0; } .container { width: 100%; height: 100%; } .swiper-slide img { width: 100%; height: 100%; } /* 动画缩放 */ @keyframes imgmove1 { 0% { transform:scale(1); /*开始为原始大小*/ } 50% { transform:scale(1.1); } 100% { transform:scale(1); } } .moveImg { /* 动画名称 */ animation-name: imgmove1; /* 动画花费时长 */ animation-duration: 16s; /* 动画速度曲线 */ /* animation-timing-function: ease-in-out; */ /* 动画等待多长时间执行 */ animation-delay: 1s; /* 规定动画播放次数 infinite: 无限循环 */ animation-iteration-count: infinite; /* 是否逆行播放 */ /* animation-direction: alternate; */ /* 动画结束之后的状态 */ animation-fill-mode: none; /* animation-fill-mode: forwards; */ } .header{ position: absolute; left: 0; top: 9%; width: 100%; height: 60px; margin: 0 auto; z-index: 12; color: white; } .headContent{ text-align: center; line-height: 60px; /* font-size: xx-large; */ font-size:2vw; font-weight: bold; } .foot{ position: absolute; left: 0; bottom: 9%; width: 100%; height: 60px; margin: 0 auto; z-index: 12; color: white; } .footImg{ /* width: 200px; */ width: 12%; margin: 0 auto; } .footImgStyle{ width: 100%; } .footContent{ /* font-size: 13px; */ font-size: 0.8vw; text-align: center; margin-top: 10px; } .swiper-button-prev, .swiper-container-rtl .swiper-button-next{ color: black; } .swiper-button-next, .swiper-container-rtl .swiper-button-prev{ color: black; } </ style > </ body > </ html > |
1. 轮播图播放的同时放大/缩小 @keyframes
2.支持图片宽度同窗口等比例缩放, 容器宽度设为100%
1 2 3 4 5 6 7 8 9 10 | 设置:< br >< meta name="viewport" content="width=device-width, initial-scale=1.0">< br >< br >样式: .container { width: 100%; height: 100%; } .swiper-slide img { width: 100%; height: 100%; } |
3.支持图片高度自适应窗口,让所有元素的高度设为100%,从html开始,包括body
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | html{ height: 100%; } body{ height: 100%; margin: 0; } .container { width: 100%; height: 100%; } .swiper-slide img { width: 100%; height: 100%; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用