橙子lovesun
2015-12-06 18:01
想实现按下箭头图片切换。代码如下,实际却未实现。请大神看看问题出在哪?
<div>有关图片的定义:
<div id="container">
<div id="list" style="left: -600px;">
<img src="img/5.jpg" alt="1"/>
<img src="img/1.jpg" alt="1"/>
</div>
在<div>中定义一对左右箭头代码如下:
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
在CSS中设置样式代码如下:
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px; position: absolute; z-index: 2; top: 180px; background-color: RGBA(0,0,0,.3); color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#container:hover .arrow { display: block;}
#prev { left: 20px;}
#next { right: 20px;}
在<script>中定义当按下箭头切换到下张图片:
next.onclick=function(){
animate(-600);
}
prev.onclick=function(){
animate(600);
}
function animate(offset){
list.style.left=parseInt(list.style.left)+offset+'px';
}
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
.arrow {
cursor: pointer;
display: inline-block;
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 180px;
background-color: RGBA(0, 0, 0, .3);
color: #fff;
}
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
}
#container:hover .arrow {
display: block;
}
#prev {
left: 20px;
}
#next {
right: 20px;
}
#list{
height: 400px;
position: absolute;
top:0;
left: 0;
}
#list img{
width: 600px;
height: 400px;
float: left;
}
#container{
width: 600px;
height: 400px;
position: relative;
overflow: hidden;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="container">
<div id="list" style="left: 0">
<img src="img/5.jpg" alt="5"/>
<img src="img/1.jpg" alt="1"/>
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
<script type="text/javascript">
var next = document.getElementById('next');
var list = document.getElementById('list');
var imgs = list.getElementsByTagName('img');
next.onclick = function () {
if(parseInt(list.style.left)<= (-600 * (imgs.length-1))){
return ;
}
animate(-600);
}
var prev = document.getElementById('prev');
prev.onclick = function () {
if(parseInt(list.style.left)>= 0){
return ;
}
animate(600);
}
function animate(offset) {
list.style.left = parseInt(list.style.left) + offset + 'px';
}
</script>
</body>
</html>也不知道你的完整代码是什么样的,就尽量按照你的来写的
焦点图轮播特效
65348 学习 · 638 问题
相似问题