js的缺点
Js不好的地方:
1.代码冗余
2.需要遍历,很麻烦
3.有兼容性的问题
4.添加动画很麻烦
5.会覆盖
jquery的优点
1.方便快捷,简单粗暴
2.不需要遍历:具有隐式迭代的特点,会自动遍历。
3.操作动画简单
4.完全没有兼容性
5.不会覆盖
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>京东轮播图</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
.slider {
height: 340px;
width: 790px;
margin: 100px auto;
position: relative;
}
.slider li {
position: absolute;
display: none;
}
.slider li:first-child {
display: block;
}
.arrow {
display: none;
}
.slider:hover .arrow {
display: block;
}
.arrow-left,
.arrow-right {
font-family: "SimSun", "宋体";
width: 30px;
height: 60px;
background-color: rgba(0, 0, 0, 0.1);
position: absolute;
top: 50%;
margin-top: -30px;
cursor: pointer;
text-align: center;
line-height: 60px;
color: #fff;
font-weight: 700;
font-size: 30px;
}
.arrow-left:hover,
.arrow-right:hover {
background-color: rgba(0, 0, 0, .5);
}
.arrow-left {
left: 0;
}
.arrow-right {
right: 0;
}
</style>
</head>
<body>
<div class="slider">
<ul>
<li><a href="#"><img src="images/1.jpg" alt=""></a></li>
<li><a href="#"><img src="images/2.jpg" alt=""></a></li>
<li><a href="#"><img src="images/3.jpg" alt=""></a></li>
<li><a href="#"><img src="images/4.jpg" alt=""></a></li>
<li><a href="#"><img src="images/5.jpg" alt=""></a></li>
<li><a href="#"><img src="images/6.jpg" alt=""></a></li>
<li><a href="#"><img src="images/7.jpg" alt=""></a></li>
<li><a href="#"><img src="images/8.jpg" alt=""></a></li>
</ul>
<!--箭头-->
<div class="arrow">
<span class="arrow-left"><</span>
<span class="arrow-right">></span>
</div>
</div>
<script src="js/jquery-1.12.4.js"></script>
<script>
var index=0;
$(".arrow-left").click(function () {
index--;
if(index==-1){
index=$(".slider>ul>li").length-1;
}
$(".slider>ul>li").eq(index).show(100).siblings().hide(100);
});
$(".arrow-right").click(function () {
index++;
if(index== $(".slider>ul>li").length){
index=0;
}
$(".slider>ul>li").eq(index).show(100).siblings().hide(100);
});
</script>
</body>
</html>