js实现焦点图片

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			* {
				padding: 0px;
				margin: 0px;
			}

			.box {
				width: 600px;
				height: 400px;
				margin: 50px auto;
				border: solid 10px #333;
				position: relative;
				background-color: #f00;
			}

			.box a {
				position: absolute;
				top: 50%;
				display: block;
				width: 30px;
				border: solid 5px #fff;
				background-color: #666;
				text-align: center;
				font-size: 28px;
				color: white;
				opacity: 0.4;
				font-weight: bold;
			}
			#prev {
				left: 10px;
			}
			#next {
				right: 10px;
			}
			.box a:hover {
				opacity: 0.8;
			}
			.box p {
				width: 600px;
				height: 30px;
				line-height: 30px;
				background-color: #333;
				color: white;
				position: absolute;
				text-align: center;
			}
			.box #picNum {
				top: 20px;
			}
			.box #picName {
				bottom: 20px;
			}
			.box #img {
				display: block;
				width: 600px;
				height: 400px;
				transition: opacity 0.5 ease-in-out;
			}
		</style>
		<script>
			window.onload = function() {
				//准备工作
				var box = document.getElementById("box")
				var arrPic = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg"];
				var arrText = ["城市的温暖", "又见抽烟", "秋日私欲", "梨花开"];
				var picNNum = document.getElementById("picNum"); //图片的数量
				var picName = document.getElementById("picName"); //图片的名称
				var img = document.getElementById("img"); //图片路径
				var prev = document.getElementById("prev"); //上一个
				var next = document.getElementById("next"); //下一个
				var num = 0; //计数器
				function changPic() {
					picNNum.innerHTML = num + 1 + "/" + arrPic.length;
					img.src = arrPic[num];
					picName.innerHTML = arrText[num];
				}
				function auto() {
					changPic();
					num++;
					if (num == 4) {
						num = 0;
					}
					t = setTimeout(auto, 1000)
				}
				auto();
				next.onclick = function() {
					num++;
					if (num == 4) {
						num = 0;
					}
					changPic();
				}
				prev.onclick = function() {
					num--;
					if (num == -1) {
						num = arrPic.length - 1;
					}
					changPic();
				}
				box.onmouseover = function() {
					clearTimeout(t);
				}
				box.onmouseout = function() {
					t = setTimeout(auto, 1000);
				}
			}
		</script>
	</head>
	<body>
		<div class="box" id="box">
			<p id="picNum"></p>
			<p id="picName"></p>
			<img src="img/1.jpg" alt="图片1" id="img">
			<a href="javascript:" id="prev"> < </a>
			<a href="javascript:" id="next"> > </a>
		</div>
	</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值