带你亲手实现网页轮播图!!! 这感觉完美!!!

效果图

静态的

带你亲手实现网页轮播图!!! 这感觉完美!!!_第1张图片

动态的

图片素材

带你亲手实现网页轮播图!!! 这感觉完美!!!_第2张图片

带你亲手实现网页轮播图!!! 这感觉完美!!!_第3张图片带你亲手实现网页轮播图!!! 这感觉完美!!!_第4张图片带你亲手实现网页轮播图!!! 这感觉完美!!!_第5张图片

图片素材取自小米官网

说明

  • HTML/CSS/JS代码分别保存在index.html/index.css/index.js 文件,而index.css和index.js文件又分别保存在 css 和 js 文件夹下
  • 图片素材依次命名为:pic_01.jpg pic_02.jpg 等等,所有图片都保存在 img 文件夹中
  • 示意图如下
    带你亲手实现网页轮播图!!! 这感觉完美!!!_第6张图片

源代码

HTML代码


<html>
	<head>
		<meta charset="utf-8" />
		<title>轮播图title>
		
		<link rel="stylesheet" href="css/index.css" />
	head>
	
	<body>
		<div class="wrap">
			<ul class="list">
				<li class="item active"><img src="img/pic_01.jpg" alt="" />li>
				<li class="item"><img src="img/pic_02.jpg" alt="" />li>
				<li class="item"><img src="img/pic_03.jpg" alt="" />li>
				<li class="item"><img src="img/pic_04.jpg" alt="" />li>
			ul>
			<button class="btn" id="pre"><button>
			<button class="btn" id="next">>button>
			<ul class="point_list">
				
				<li tab_index="0" class="point active">li>
				<li tab_index="1" class="point">li>
				<li tab_index="2" class="point">li>
				<li tab_index="3" class="point">li>
			ul>
		div>
		
		<script src="js/index.js" type="text/javascript">script>
	body>
html>

CSS代码

*{
	margin: 0;
	padding: 0;
}


.wrap{
	position: absolute;
	left: 50%;
	transform: translateX(-50%);
	
	width: 1226px;
	height: 460px;
	margin-top: 20px;
}
ul{
	list-style: none;
	position: absolute;
	top: 0;
	left: 0;
	width: 1226px;
	height: 460px;
	
}

img{
	width: 1226px;
	height: 460px;
}
.item{
	position: absolute;
	top: 0;
	left: 0;
	z-index: 1;
	opacity: 0;
	transition: 0.5s;
}
/*被激活的图片,放置到最上面*/
.item.active{
	z-index: 666;
	opacity: 1;
}

.point_list{
	position: absolute;
	left: 580px;
	top: 430px;
	z-index: 999;
}
.point{
	float: left;
	width: 8px;
	height: 8px;
	margin-right: 8px;
	border-radius: 50%;
	background: rgba(0,0,0,0.5);
	border: 2px solid rgba(255,255,255,0.6);
	/* background: pink; */
}
.point.active , .point:hover{
	background: rgba(255,255,255,0.8);
	border: 2px solid rgba(0,0,0,0.4);
}

.btn{
	width: 40px;
	height: 70px;
	/* 垂直居中 */
	position: absolute;
	top: 50%;
	margin-top: -35px;
	left: 0;
	z-index: 999;
	border: none;
	outline: none;
	font-size: 28px;
	color: #333;
	background: none;
}
.btn:hover{
	background: rgba(0,0,0,0.5);
	color: #fff;
}

#pre{
	
}
/*下一张*/
#next{
	left: 100%;
	margin-left: -40px;
}

JS代码

// 获取所有图片
var items = document.getElementsByClassName("item");
// 获取所有圆点
var points = document.getElementsByClassName("point");
// 获取按钮
var pre = document.getElementById("pre");
var next = document.getElementById("next");

var index = 0;
var time = 0;

// 点击上一页
pre.onclick = function(){
	items[index].className = "item";
	points[index].className = "point";
	if(index <= 0){
		index = items.length;
	}
	
	index--;
	items[index].className = "item active";
	points[index].className = "point active";
}

// 点击下一页
next.onclick = function(){
	points[index].className = "point";
	items[index].className = "item";
	index++;
	index = index % items.length;
	
	items[index].className = "item active";
	points[index].className = "point active";
}

for(var i=0; i<points.length; i++){
	points[i].onclick = function(){
		points[index].className = "point";
		items[index].className = "item";
		
		index = Number(this.getAttribute("tab_index"));
		
		items[index].className = "item active";
		points[index].className = "point active";
		
		time = 0;
	}
}

setInterval(function(){
	
	if(time >= 30){
		next.onclick();
		time = 0;
	}else{
		time++;
	}
},100);

你可能感兴趣的:(带你亲手实现网页轮播图!!! 这感觉完美!!!)