卡在了寻找切换图片的索引

卡在了寻找切换图片的索引

感觉难点就在于$('.pic')的索引


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>imooc</title>

<link rel="stylesheet" type="text/css" href="css/reset.css" />

<link rel="stylesheet" type="text/css" href="css/css.css" />

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="js/jquery.js"></script>

</head>

<body>

<div class="main">

<h1>jQuery实现轮播图</h1>

<div class="mainmin">

<!--banner图-->

<div id="mainmin">

<a href="#" class="pic one activePic"></a>

<a href="#" class="pic two"></a>

<a href="#" class="pic three"></a>

<a href="#" class="pic four"></a>

<a href="#" class="pic five"></a>

</div>

<!--圆点切换-->

<ul>

<li class="tip activeTip"></li>

<li class="tip second"></li>

<li class="tip third"></li>

<li class="tip fourth"></li>

<li class="tip fifth"></li>

</ul>

<!--上、下切换-->

<div id="qiehuan">

<a href="#" class="pre left">

<a href="#" class="pre right">

</div>

</div>

</div>

</body>

</html>



.main{

width: 100%;

height: 800px;

}


/*标题样式*/

h1{

width: 500px;

font-size: 26px;

margin: 30px auto;

font-family: "微软雅黑";

text-align: center;

}

.mainmin{

width: 1200px;

height: 460px;

border: 10px solid #bbb;

margin: 50px auto;

position: relative;

}


/*banner样式*/

.mainmin .pic{

display: block;

width: 100%;

height: 100%;

position: absolute;

top:0;

left: 0;

display: none;

}

.mainmin .one{

background: url("../img/1.jpg");

}

.mainmin .two{

background: url("../img/2.jpg");

}

.mainmin .three{

background: url("../img/3.jpg");

}

.mainmin .four{

background: url("../img/4.jpg");

}

.mainmin .five{

background: url("../img/5.jpg");

}

.mainmin .activePic{

display: block;

}


/*切换圆点样式*/

.tip{

width: 20px;

height: 20px;

border-radius: 10px;

position: absolute;

bottom: 20px;

right: 160px;

z-index: 1;

background: rgba(187,187,187,0.6);

cursor: pointer;

box-shadow: 0 0 0 3px rgba(255,255,255,0.8) inset;

}

.second{

right: 130px;

}

.third{

right: 100px;

}

.fourth{

right: 70px;

}

.fifth{

right: 40px;

}

.activeTip{

background: rgba(255,255,255,0.8);

box-shadow: 0 0 0 3px rgba(187,187,187,0.6) inset;

}


/*上、下切换样式*/

.pre{

width: 40px;

height: 90px;

position: absolute;

z-index: 2;

top: 50%;

margin-top: -45px; 

}

.pre:hover{

background: rgba(0,0,0,0.4);

}

.left{

background: url("../img/pre2.png") no-repeat center center;

left: 0;

}

.right{

background: url("../img/pre.png") no-repeat center center;

right: 0;

}


//给上一张绑定事件,并切换图片

$('.left').on('click',function(){

var index=0;

index--;

if(index<0){

index=$('.pic').length-1;

}

$('.pic').each(function(index,ele){

$('.pic').eq(index).removeClass('activePic');

})

$('.pic').eq(index).addClass('activePic');

})



正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,代码中有以下几个问题。

1、左右按钮是用a标签写的,但是a标签没有写结尾,参考下图:

http://img1.sycdn.imooc.com//climg/5c4ab9e30001a7a305430150.jpg

2、建议把图片轮播的效果单放在一个函数里面,方便调用,参考下图:

http://img1.sycdn.imooc.com//climg/5c4aba0d00012b2512240113.jpg

  • 当轮播到哪张图片的时候,就添加上activePic类名,然后使用slblings()方法,找到相邻的兄弟元素并去掉这个类名。

3、建议把右侧切换的效果添加上,因为图片显示的就是第一张,所以点击左侧切换的时候,效果不变,当点击了右侧,图片进行了更换,再点击左侧的话,图片就会再切换回去,参考下图:

http://img1.sycdn.imooc.com//climg/5c4ababa0001e9bd10660824.jpg

4、建议把点击圆点按钮的效果也封装在changeImg()函数里面,这样哪里需要这个效果,直接调用函数名就可以了,节省代码量。

自己测试一下,如果帮助到了你,欢迎采纳!
祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码