老师你好请检查
来源:5-1 事件参数
weixin_慕粉6116347
2020-03-12 15:01:48
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>事件参数</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
background: #90B0C7;
}
a {
text-decoration: none;
}
span {
display: block;
width: 16px;
height: 16px;
border-radius: 50%;
background: #fff;
margin: 40px auto 45px;
}
nav {
display: flex;
justify-content: space-between;
width: 78.124vw;
margin: 0 auto 45px;
position: relative;
}
nav:before {
content: '';
width: 100%;
height: 15px;
background: #fff;
position: absolute;
top: 17px;
left: 0;
}
nav>a {
position: relative;
font-size: 20px;
padding: 10px;
background: #fff;
border: 2px solid #7295AA;
color: #929BA9;
}
div {
position: relative;
width: 78.124vw;
height: 75vh;
margin: 0 auto;
background: #fff;
overflow: hidden;
box-shadow: 0 0 30px 0 rgba(8, 1, 3, .3);
}
div>img {
width: 98%;
height: 96%;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
transition: all 1.5s;
}
div>img:not(:first-of-type) {
opacity: 0;
}
</style>
</head>
<body>
<span></span>
<nav>
<a href="#">泸沽湖</a>
<a href="#">丽江古城</a>
<a href="#">茶马古道</a>
<a href="#">就这家·云逸客栈</a>
<a href="#">西双版纳</a>
<a href="#">云南红酒庄</a>
<a href="#">轿子雪山</a>
<a href="#">普者黑</a>
<a href="#">海埂大坝</a>
<a href="#">玉龙湾</a>
<a href="#">昆明郊野公园</a>
<a href="#">欧洲风琴小镇</a>
</nav>
<div>
<img src="images/1.jpg">
<img src="images/2.jpg">
<img src="images/3.jpg">
<img src="images/4.jpg">
<img src="images/5.jpg">
<img src="images/6.jpg">
<img src="images/7.jpg">
<img src="images/8.jpg">
<img src="images/9.jpg">
<img src="images/10.jpg">
<img src="images/11.jpg">
<img src="images/12.jpg">
</div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
var index = 0;
$('a').mouseenter(function() {
index = $(this).index();
swiper();
})
//左键37 右键39
$(document).keydown(function(event) {
if (event.keyCode == 37) {
if (index==0) {
index = 11;
}else {
index--;
}
swiper();
}else if (event.keyCode == 39) {
if (index==11) {
index = 0;
}else {
index++;
}
swiper();
}
});
function swiper(index) {
$('img').eq(index).css({'opacity':'1'}).siblings()
.css({'opacity':'0'});
}
</script>
</body>
</html>
我的没有反应
2回答
同学你好,能够自己解决问题很棒 !另外,课程中是不需要传入参数的,因为swiper并没有定义参数,所以它使用的index是全局变量。
而同学的代码中给swiper定义了参数,内部定义了index就不会查找全局的变量index了。如果调用swiper不传入参数,index就是undefined ,所以不对。
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
weixin_慕粉6116347
提问者
2020-03-12
老师你好解决问题了,我发现老师一个问题,在调用方法时不传入index好像没有效果啊?