网页轮播图
效果如下: 图可以自己找喜欢的用。
代码如下:
<!DOCTYPE html>
<html lang="zh-CN">
<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>轮播图</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
position: relative;
margin: 15px auto;
width: 1000px;
height: 500px;
overflow: hidden;
}
ul {
position: absolute;
background-color: pink;
width: 5000px;
height: 500px;
}
ul li {
list-style: none;
float: left;
width: 1000px;
height: 500px;
background-color: skyblue;
}
ul li img {
width: 100%;
height: 100%;
}
ol {
position: absolute;
padding: 1px;
left: 460px;
top: 470px;
}
ol li {
list-style: none;
float: left;
width: 9px;
height: 9px;
border: 2px solid rgba(255, 255, 255, 0.5);
border-radius: 50%;
cursor: pointer;
margin: 0 3px;
}
.current {
background-color: #fff;
}
.arrow-l,
.arrow-r {
display: none;
text-decoration: none;
position: absolute;
top: 50%;
margin-top: -20px;
width: 24px;
height: 40px;
background: rgba(0, 0, 0, .3);
text-align: center;
line-height: 40px;
color: #fff;
font-size: 18px;
z-index: 2;
}
.arrow-r {
right: 0;
}
a:hover {
color: red;
}
</style>
<script src="animate.js"></script>
</head>
<body>
<div>
<a href="javascript:;" class="arrow-l">
<
</a>
<a href="javascript:;" class="arrow-r">
>
</a>
<ul>
<li><a href='javascript:;'><img src="images/img1.jpg" alt=""></a></li>
<li><a href='javascript:;'><img src="images/img2.jpg" alt=""></a></li>
<li><a href='javascript:;'><img src="images/img3.jpg" alt=""></a></li>
<li><a href='javascript:;'><img src="images/img1.jpg" alt=""></a></li>
</ul>
<ol>
<li class="current"></li>
<li></li>
<li></li>
</ol>
</div>
<script>
var ul = document.querySelector('ul');
var ol = document.querySelector('ol');
var div = document.querySelector('div');
var arrowl = document.querySelector('.arrow-l');
var arrowr = document.querySelector('.arrow-r');
div.addEventListener('mouseenter', function () {
clearInterval(timer);
timer = null;
arrowl.style.display = 'block';
arrowr.style.display = 'block';
})
div.addEventListener('mouseleave', function () {
lunbo();
arrowl.style.display = 'none';
arrowr.style.display = 'none';
})
var i = 1;
var flag = true;
arrowr.addEventListener('click', function () {
if (flag == false)
i++;
if (i == 4) {
i = 1;
ul.style.left = 0;
}
animate(ul, -1000 * i);
circleChange()
i++;
flag = true;
})
arrowl.addEventListener('click', function () {
if (flag == true) {
i--;
flag = false;
}
if (i == 0) {
i = 2;
ul.style.left = '-3000px';
animate(ul, -1000 * i);
circleChange()
}
else {
i--;
animate(ul, -1000 * i);
circleChange();
}
})
lunbo();
function lunbo() {
timer = setInterval(function () {
if (i == 4) {
i = 1;
ul.style.left = 0;
}
animate(ul, -1000 * i);
circleChange();
i++;
flag = true;
}, 1500);
}
for (var k = 0; k < 3; k++) {
ol.children[k].setAttribute('index',k);
ol.children[k].addEventListener('click', function () {
i = this.getAttribute('index');
animate(ul, -1000 * i);
flag = false;
circleChange();
})
}
function circleChange() {
for (var j = 0; j < ol.children.length; j++) {
ol.children[j].className = '';
}
if (i == 3)
ol.children[0].className = 'current';
else
ol.children[i].className = 'current';
}
</script>
</body>
</html>
|