Web开发基础:JavaScript轮播图

超详细的Java知识点路线图


图片轮播

在这里插入图片描述

功能点

1、页面效果HTML+CSS
2、图片横向滚动
3、每滚动一张图片,暂停一下
4、下面的圆点跟着图片变化

代码


<!DOCTYPE html>
<html>
<head>
	<title>广告轮播</title>
	<meta charset="utf-8">
	<style type="text/css">
		*{
			margin: 0px;
			padding:0px;
		}
		#container{
			width: 780px;
			height:455px;
			margin: 0px auto;
			background: #cccccc;
			position: relative;
		}
		#banner{
			width: 780px;
			height:455px;
			overflow: hidden;
			/*将ul的父容器设置为相对定位*/
			position: relative;
			text-align: center;
		}
		#imgs{
			width:4680px;
			height: 100%;
		}
		#imgs img{
			width: 780px;
			height: 455px;
			display: inline-block;
		}
		#container ul{
			list-style: none;
			/*叠放在banner上,将ul设置为绝对定位*/
			position: absolute;
			left:342px;
			bottom: 10px;
		}
		#container ul li{
			float: left;
			width: 12px;
			height: 12px;
			border-radius: 50%;
			background: #ffffff;
			margin-right: 4px;
		}
		.slide_btn{
			width: 60px;
			height: 45px;
			background: #000000;
			/*透明度 0 ~ 1*/
			opacity: 0.3;
			position: absolute;
			top:200px;
		}
		#pre_btn{
			left:0px;
			border-radius: 0px 4px 4px 0px;
		}
		#next_btn{
			left:720px;
			border-radius: 4px 0px 0px 4px;
		}
		#next{
			width: 100%;
			height: 100%;
			background:url(img/oeD55470539oS.jpg) -110px -49px;
		}
		#pre{
			width: 100%;
			height: 100%;
			background:url(img/oeD55470539oS.jpg) -110px -113px;
		}
	</style>
</head>
<body>
	<div id="container">
		<div id="banner">
			<div id="imgs">
				 <img src="img/kQH63660936yu.jpg"><img src="img/gqh63659406pp.jpg"><img src="img/OIl61434592QN.jpg"><img src="img/wnM59961793kx.jpg"><img src="img/tfs58753019wI.jpg"><img src="img/kQH63660936yu.jpg">  
			</div>
		</div>
		<ul id="indicator"><li></li><li></li><li></li><li></li><li></li></ul>
		<div id="pre_btn" class="slide_btn">
			<div id="pre"></div>
		</div>
		<div id="next_btn" class="slide_btn">
			<div id="next"></div>
		</div>
	</div>
	<script type="text/javascript">
		var timer;
		//红色圆点的索引
		var index = 0;
		//获得ul指示器中所有的li
		var lis = document.getElementById("indicator").childNodes;
		//当前的圆点对象
		var current;
		//移动广告层
		function moveBanner(){
			var banner = document.getElementById("banner");
			var imgs = document.getElementById("imgs");
			//将banner层向左滚动
			banner.scrollLeft++;
			//每滚动一个图片宽度,就停顿下
			if(banner.scrollLeft % banner.offsetWidth == 0){
				//停止周期任务
				clearInterval(timer);
				//延时2秒重新启动周期任务
				setTimeout("startBanner()",2000);
				//将圆点的索引加一
				index++;
			}
			//如果banner滚动到最左边,设置banner的scrollLeft为0,继续滚动
			if(banner.scrollLeft >= imgs.offsetWidth - banner.offsetWidth){
				banner.scrollLeft = 0;
				//将圆点索引设置为0
				index = 0;
			}
			//将之前的圆点对象设置为白色
			if(current != null){
				current.style.background = "#ffffff";
			}
			//将当前的圆点设置为红色
			lis.item(index).style.background = "#ff0000";
			//将当前的圆点保存在current中,便于下次将这个点设为白色
			current = lis.item(index);
		}
		//启动滚动
		function startBanner(){
			timer = setInterval("moveBanner()",0.1);
		}
		startBanner();
	</script>
</body>
</html>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恒哥~Bingo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值