老师帮我看看,当我点击圆点索引的时候,为什么切换不到对应的图片
var index=0,
prev=byId("prev"),//上一张
next=byId("next"),//下一张
pics=byId("banner").getElementsByTagName("a"),
dots=byId("dots").getElementsByTagName("i"),
size=pics.length;
function byId(id){
return typeof (id)==="string" ? document.getElementById(id):id;
}
//切换图片
function changeImg(){
//遍历所有图片,将图片隐藏,将圆点上的类清除
for (var i=0;i<size;i++){
pics[i].style.display="none";
//dots[i].className="";
dots[i].className = "slider_indicators_btn"
}
//显示当前图片
pics[index].style.display="block";
//dots[index].className = "active";
//当前圆点高亮显示
dots[index].className = "slider_indicators_btn active";
}
//点击上一张按钮显示上一张图片
prev.addEventListener("click",function () {
index--;
if (index<0) index = size-1;
changeImg();
});
//点击下一张按钮显示下一张图片
next.addEventListener("click",function () {
index++;
if (index>=size) index = 0;
changeImg();
})
//点击圆点索引切换图片
for(var d = 0;d<size;d++){
dots[d].setAttribute("data-id",d);
dots(dots[d],"click",function () {
index =this.getAttribute("data-id");
changeImg();
});
}
*{
margin: 0;
padding: 0;
}
body{
font-family:"微软雅黑";
color: #14191e;
}
@font-face {
font-family: 'iconfont';
src: url('iconfont.eot');
src: url('iconfont.eot?#iefix') format('embedded-opentype'),
url('iconfont.woff2') format('woff2'),
url('iconfont.woff') format('woff'),
url('iconfont.ttf') format('truetype'),
url('iconfont.svg#iconfont') format('svg');
}
.main{
width: 590px;
height: 470px;
margin: 10px auto;
overflow: hidden;
position: relative;
}
.banner{
width: 590px;
height: 470px;
overflow: hidden;
}
.banner-slide{
display: inline-block;
width: 590px;
height: 470px;
display: none;
}
.slide-active{
display: block;
}
.focus-item-img{
display: block;
width: 100%;
height: 100%;
transition: opacity .2s;
}
img{
border: 0;
vertical-align: middle;
}
.slider_control:hover {
color: #fff;
background-color: #999;
background-color: rgba(0,0,0,.4);
}
#prev{
left: 0;
border-top-right-radius: 18px;
border-bottom-right-radius: 18px;
}
#next{
right: 0;
border-top-left-radius: 18px;
border-bottom-left-radius: 18px;
}
.slider_control{
position: absolute;
top: 50%;
border-radius: 0;
width: 25px;
height: 35px;
line-height: 35px;
background-color: #d9d9d9;
background-color: rgba(0,0,0,.15);
margin-top: -20px;
font-size: 15px;
z-index: 2;
border: none;
outline: none;
transition: background-color .2s ease;
}
.prev i{
left: -3px;
}
.next i{
right: -3px;
}
.slider_control i {
position: relative;
display: block;
width: 100%;
height: 100%;
color: #fff;
color: rgba(255,255,255,.8);
transition: color .2s ease;
}
.iconfont {
font-family: iconfont,sans-serif;
font-style: normal;
-webkit-text-stroke-width: .2px;
}
button{
cursor: pointer;
}
.slider_indicators i.active{
width: 9px;
height: 9px;
top: 2px;
left: 0;
border: 3px solid rgba(0,0,0,.1);
}
.banner .slider_indicators_btn{
width: 8px;
height: 8px;
margin-right: 4px;
border: 1px solid rgba(0,0,0,.05);
background: rgba(255,255,255,.4);
}
.slider_indicators_btn{
position: relative;
display: inline-block;
width: 4px;
height: 4px;
margin-right: 4px;
border-radius: 50%;
background: #999;
transition: background .2s ease;
}
.slider_indicators{
position: absolute;
left: 50%;
font-size: 0;
z-index: 2;
margin-left: 0!important;
left: 30px;
bottom: 20px;
text-align: center;
animation: skeletonShow .2s ease .1s both;
}
.active:before{
content: "";
width: 9px;
height: 9px;
position: absolute;
border-radius: 50%;
left: 0;
background-color: white;
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>轮播图</title>
<link rel="stylesheet" href="style4.css">
</head>
<body>
<div class="main">
<div class="banner" id="banner">
<button class="slider_control prev"id="prev" aria-hidden="true" tabindex="-1">
<i class="iconfont"></i>
</button>
<a href="#" class="banner-slide slide1 slide-active">
<img class="focus-item-img" src="img/d474fc22a9d71105.jpg">
</a>
<a href="#" class="banner-slide slide2">
<img class="focus-item-img" src="img/d40e34d428cf67f1.jpg">
</a>
<a href="#" class="banner-slide slide3">
<img class="focus-item-img" src="img/3a89552a02f32a19.jpg">
</a>
<a href="#" class="banner-slide slide4">
<img class="focus-item-img" src="img/075149efd33cf8fe.jpg">
</a>
<a href="#" class="banner-slide slide5">
<img class="focus-item-img" src="img/16a08ecb2ab64f5a.jpg">
</a>
<a href="#" class="banner-slide slide6">
<img class="focus-item-img" src="img/6f21c7beb4406512.jpg">
</a>
<a href="#" class="banner-slide slide7">
<img class="focus-item-img" src="img/34b6b971d0dfa459.jpg">
</a>
<a href="#" class="banner-slide slide8">
<img class="focus-item-img" src="img/657dcc0790348e1d.jpg">
</a>
<button class="slider_control next" id="next" aria-hidden="true" tabindex="-1">
<i class="iconfont"></i>
</button>
<div class="slider_indicators" id="dots" style="margin-left: -56.5px">
<i class="slider_indicators_btn active"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
</div>
</div>
</div>
<script src="script4.js"></script>
</body>
</html>
正在回答
同学你好,代码中并没有使用老师封装的addHandler方法,所以和视频中一样直接调用方法是不可以的。有两种修改方式:
1、和圆点点击事件一样,使用addEventListener:
2、和视频中一致,使用封装的方法,这段可以直接在资料源码中下载使用,放在js最前面:
然后调用:
这两个不要弄混哦,祝学习愉快!
var index=0,
timer = null, //定时器
main =byId("main"),
prev=byId("prev"),//上一张
next=byId("next"),//下一张
pics=byId("banner").getElementsByTagName("a"),
dots=byId("dots").getElementsByTagName("i"),
size=pics.length;
function byId(id){
return typeof (id)==="string" ? document.getElementById(id):id;
}
//清除定时器,停止自动轮播
function stopAutoPlay() {
if (timer) {
clearInterval(timer);
}
}
//自动轮播
function startAutoPlay() {
//间隔调用
timer = setInterval(function () {
index++;
if (index>=size)index=0;
changeImg();
},2000)
}
//切换图片
function changeImg(){
//遍历所有图片,将图片隐藏,将圆点上的类清除
for (var i=0;i<size;i++){
pics[i].style.display="none";
//dots[i].className="";
dots[i].className = "slider_indicators_btn"
}
//显示当前图片
pics[index].style.display="block";
//dots[index].className = "active";
//当前圆点高亮显示
dots[index].className = "slider_indicators_btn active";
}
//点击上一张按钮显示上一张图片
prev.addEventListener("click",function () {
index--;
if (index<0) index = size-1;
changeImg();
})
//点击下一张按钮显示下一张图片
next.addEventListener("click",function () {
index++;
if (index>=size) index = 0;
changeImg();
})
//点击圆点索引切换图片
for(var d = 0;d<size;d++){
dots[d].setAttribute("data-id",d);
dots[d].addEventListener("click",function () {
index =this.getAttribute("data-id");
changeImg();
});
}
//鼠标滑入main,停止轮播
main(main,"mouseover",stopAutoPlay);
//鼠标滑入main,继续轮播
main(main,"mouseout",startAutoPlay);
//自动开启轮播
startAutoPlay();
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>轮播图</title>
<link rel="stylesheet" href="style4.css">
</head>
<body>
<div class="main" id="main">
<div class="banner" id="banner">
<button class="slider_control prev"id="prev" aria-hidden="true" tabindex="-1">
<i class="iconfont"></i>
</button>
<a href="#" class="banner-slide slide1 slide-active">
<img class="focus-item-img" src="img/d474fc22a9d71105.jpg">
</a>
<a href="#" class="banner-slide slide2">
<img class="focus-item-img" src="img/d40e34d428cf67f1.jpg">
</a>
<a href="#" class="banner-slide slide3">
<img class="focus-item-img" src="img/3a89552a02f32a19.jpg">
</a>
<a href="#" class="banner-slide slide4">
<img class="focus-item-img" src="img/075149efd33cf8fe.jpg">
</a>
<a href="#" class="banner-slide slide5">
<img class="focus-item-img" src="img/16a08ecb2ab64f5a.jpg">
</a>
<a href="#" class="banner-slide slide6">
<img class="focus-item-img" src="img/6f21c7beb4406512.jpg">
</a>
<a href="#" class="banner-slide slide7">
<img class="focus-item-img" src="img/34b6b971d0dfa459.jpg">
</a>
<a href="#" class="banner-slide slide8">
<img class="focus-item-img" src="img/657dcc0790348e1d.jpg">
</a>
<button class="slider_control next" id="next" aria-hidden="true" tabindex="-1">
<i class="iconfont"></i>
</button>
<div class="slider_indicators" id="dots" style="margin-left: -56.5px">
<i class="slider_indicators_btn active"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
<i class="slider_indicators_btn"></i>
</div>
</div>
</div>
<script src="script4.js"></script>
</body>
</html>
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星