<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
800px;
height: 300px;
position: relative;
overflow: hidden;
margin: 100px auto;
}
.box .slider{
100%;
height: 100%;
}
.box .slider ul{
1000%;
list-style: none;
position: absolute;
top: 0;
left: 0;
}
.box .slider img{
vertical-align: top;
}
.box .slider ul li{
float: left;
}
.box .scroll_nav{
list-style: none;
position: absolute;
right: 20px;
bottom: 10px;
}
.box .scroll_nav li{
float: left;
20px;
height: 20px;
background-color: #000;
color: #fff;
text-align: center;
font-size: 12px;
font-family: "微软雅黑";
line-height: 20px;
cursor: pointer;
border-radius: 50%;
margin-right: 5px;
}
.box .scroll_nav li.current{
background-color: #356acb;
}
.box .arr{
40px;
height: 50px;
font-size: 35px;
font-family: "黑体";
line-height: 50px;
background-color: rgba(0,0,0,0.3); /*这种方式能让背景透明而文本不收影响*/
color: #fff;
text-align: center;
position: absolute;
top: 125px;
font-weight: bold;
display: none;
cursor: pointer;
/* opacity: 0.1; opacity属性会让盒子半透明,与此同时它也会让黑子内部的文本半透明*/
}
.box .arr_prev{
left: 5px;
}
.box .arr_next{
right: 5px;
}
</style>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
window.onload = function(){
//1.鼠标进入轮播图区域,显示左右箭头
var sliderWidth = $('sl').offsetWidth;//获取到slider的宽度
$('scroll').onmouseover = function(){
var arrs = this.getElementsByTagName('span');
for(var i = 0; i < arrs.length; i++){
arrs[i].style.display = 'block';
}
}
$('scroll').onmouseout = function(){
var arrs = this.getElementsByTagName('span');
for(var i = 0; i < arrs.length; i++){
arrs[i].style.display = 'none';
}
}
//2.动态生成轮播图导航栏里的li
var imgCount = $('sl_ul').getElementsByTagName('img').length;//获取图的个数
for (var i = 0; i < imgCount; i++) {
var li = document.createElement('li');//动态创建出1个li标签
li.innerHTML = i + 1;
if(i == 0){
li.className = 'current';
}
//给每个创建出来的li,动态添加索引值
li.setAttribute('index', i);
//2.2注册li的点击事件:轮播图滚动
li.onclick = function(){
//排他思想:搞定li的样式
var lis = $('sc_nav').getElementsByTagName('li');
for (var i = 0; i < lis.length; i++) {
lis[i].className = '';
}
this.className = 'current';
//设置动画的target,让轮播图滚动起来
target = this.getAttribute('index') * (-sliderWidth);
}
$('sc_nav').appendChild(li);
}
//3.启动缓动动画定时器
var leader = 0;
var target = 0;
setInterval(function(){
leader = leader + (target - leader) / 20;
$('sl_ul').style.left = leader + 'px';
}, 10);
//右箭头点击事件:next
$('next').onclick = function(){
if(target > -sliderWidth * 4){
target -= sliderWidth;
}else{
target = 0;//如果已经是最后一张图,就直接滚动回第0张图
}
setNavCurrent();
}
//左箭头点击事件:prev
$('prev').onclick = function(){
if(target < 0){
target += sliderWidth;
}
setNavCurrent();
}
//让nav中的li跟随箭头的点击改变选中状态
function setNavCurrent(){
var index = Math.abs(target / sliderWidth);
var lis = $('sc_nav').getElementsByTagName('li');
for (var i = 0; i < lis.length; i++) {
lis[i].className = '';
}
lis[index].className = 'current';
}
}
</script>
</head>
<body>
<div class="box" id="scroll">
<div class="slider" id="sl">
<ul id="sl_ul">
<li><img src="imgs/1.jpg" alt=""></li>
<li><img src="imgs/2.jpg" alt=""></li>
<li><img src="imgs/3.jpg" alt=""></li>
<li><img src="imgs/4.jpg" alt=""></li>
<li><img src="imgs/5.jpg" alt=""></li>
</ul>
</div>
<ul class="scroll_nav" id="sc_nav">
</ul>
<span class="arr arr_prev" id="prev"><</span>
<span class="arr arr_next" id="next">></span>
</div>
</body>
</html>