jQuery实现图片轮播效果

实现图片轮播效果,打开页面,每隔3秒切换至下一张图片;光标移入数字时,播放相应图片。

思路:

(1)获取需要轮播的图片和展示的div。

(2)使用animate设置left值,每次移动宽度为展示div的宽度。

(3)播放图片时,对应数字应用红色背景样式,其他数字移除该样式。

效果:

代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Document</title>
		<style>
			* {
				margin: 0;
				padding: 0;
			}

			img {
				width: 800px;
				height: 400px;
			}

			.banner {
				width: 800px;
				height: 400px;
				border: 1px solid black;
				margin: 50px auto;
				overflow: hidden;
				cursor: pointer;
				position: relative;
			}

			.banner .slide {
				width: 4800px;
				height: 400px;
				position: absolute;
				left: -600px;
			}

			.banner .slide .pic {
				width: 800px;
				height: 400px;
				line-height: 400px;
				text-align: center;
				float: left;
				font-size: 72px;
				color: white;
			}

			.banner .numbers {
				width: 150px;
				height: 30px;
				position: absolute;
				bottom: 1%;
				left: 85%;
				margin-left: -50px;
				z-index: 2;
			}

			.banner .numbers .number {
				width: 20px;
				height: 20px;
				float: left;
				margin: 5px 6px;
				cursor: pointer;
				background-color: white;
				color: black;
				text-align: center;
				border: 1px solid red;
			}

			.banner .numbers .on {
				background-color: red;
				color: #fff;
				font-weight: bolder;
			}
		</style>
	</head>
	<body>
		<div class="banner">
			<div class="slide">
				<div class="pic"><img src="./4.png"></div>
				<div class="pic"><img src="./1.png"></div>
				<div class="pic"><img src="./2.png"></div>
				<div class="pic"><img src="./3.png"></div>
				<div class="pic"><img src="./4.png"></div>
				<div class="pic"><img src="./1.png"></div>
			</div>

			<div class="numbers">
				<span class="number on">1</span>
				<span class="number">2</span>
				<span class="number">3</span>
				<span class="number">4</span>
			</div>

		</div>
		<script src="jquery-1.12.4.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var index = 1; 
			var timer = null;
			var size = $('.slide').children().size();
			var picWidth = $('.pic').width();
			$('.banner').mouseover(function() {
				clearInterval(timer);
			});
			$('.banner').mouseleave(function() {
				autoSlide();
			});
			$('.slide').mouseleave();
			function autoSlide() {
				timer = setInterval(function() {
					index++; 
					changeImg();
					changeNums();
				}, 3000); 
			};
			function changeImg() {
				var slideWidth = -1 * picWidth * index; 
				$('.slide').animate({
					'left': slideWidth + 'px'
				}, 500);
				if (index >= size - 1) {
					$('.slide').animate({
						'left': -picWidth + 'px'
					}, 0);
					index = 1;
				}
				if (index < 1) {
					$('.slide').animate({
						'left': -(size - 2) * picWidth + 'px'
					}, 0);
					index = size - 2;
				}
			}
			function changeNums() {
				$('.number').eq(index - 1).addClass('on').siblings().removeClass('on');
			}
			$('.number').mouseover(function(event) {
				var target = event.target;
				index = $(target).index() + 1;
				changeImg();
				changeNums();
			});
		</script>
	</body>
</html>
相关推荐
遗憾随她而去.6 分钟前
前端竞态问题
前端
代码游侠12 分钟前
应用——Web服务器项目代码解析
运维·服务器·开发语言·前端·笔记·html
C_心欲无痕1 小时前
网络相关 - 常用命令详解Telnet、Ping 及其他实用工具
前端·网络
JarvanMo1 小时前
没有人比我更懂Flutter第三方依赖鸿蒙化了之Sqflite
前端
子洋1 小时前
AI Agent 设计模式 - PlanAndExecute 模式
前端·人工智能·后端
web小白成长日记2 小时前
自定义 Hooks 的用法和意义详解(结合案例)
前端·css·面试·职场和发展·前端框架
小鸡脚来咯2 小时前
前端传输的数据格式的选择
java·开发语言·前端·后端
小二·2 小时前
【万字源码级剖析】深入理解 Vue 3 响应式系统:ref、reactive、computed 与 effect 的底层实现
前端·javascript·vue.js
Mintopia2 小时前
“开源”和“闭源“,AI 模型的发展方向
前端·人工智能·aigc
Mintopia2 小时前
哈珀·李的《**杀死一只知更鸟**》(*To Kill a Mockingbird*)是一部关于**人性、正义与道德成长**的小说
前端