[英]Jquery gallery next/previous buttons
我在半年前创建了一个jquery画廊(SO社区帮助了我很多,因为我对js / jquery不够熟悉),现在我想向该画廊添加下一个/上一个按钮。 我尝试将其与其他画廊结合使用,但没有正常工作。
这是js:
<script type="text/javascript">
function showPic (whichpic) {
if (document.getElementById) {
$('#actimg').fadeOut(170);
setTimeout(function() {
document.getElementById('actimg').src = whichpic.href;
$('#actimg').fadeIn(170);
}, 170);
return false;
} else {
return true;
}
}
</script>
和html:
<img id="actimg" src="" width="600" height="400" alt="main" />
<a onclick="return showPic(this)" href="example_1.jpg">
<img height="39px" width="58px" src="example_1.jpg" alt="thumbnail" />
</a>
<a onclick="return showPic(this)" href="example_2.jpg">
<img height="39px" width="58px" src="example_2.jpg" alt="thumbnail" />
</a>
<a onclick="return showPic(this)" href="example_3.jpg">
<img height="39px" width="58px" src="example_3.jpg" alt="thumbnail" />
</a>
画廊看起来像http://www.rafsimons.com/collections/aw-11/,但是它没有闪烁,并且现在没有我要制作的下一步/上一步按钮。 在此先感谢您的帮助。
首先将一个class属性添加到您的所有拇指( <a>
标签),以便可以从jQuery轻松引用它们:
<a class="gallery_item" onclick="return showPic(this)" href="example_1.jpg">
<img height="39px" width="58px" src="example_1.jpg" alt="thumbnail" />
</a>
...
我们需要一种方法来知道哪个是当前图像。 我们可以将该信息存储在您的<a>
标记本身的自定义属性中。 为此,请修改showPic函数,例如:
function showPic (whichpic) {
if (document.getElementById) {
$('.gallery_item').removeAttr('current'); // <- remove 'current' attribute from all thumbs
$('#actimg').fadeOut(170);
setTimeout(function() {
document.getElementById('actimg').src = whichpic.href;
$('#actimg').fadeIn(170);
$(whichpic).attr('current', '1'); // <- set this one as current
}, 170);
return false;
} else {
return true;
}
}
现在为下一个/上一个按钮添加2个链接,并将它们放置在适当的位置。 将其ID分别设为“下一个”和“上一个”
<a href="#" id="prev" onclick="return nextPic()">Prev</a>
<a href="#" id="next" onclick="return prevPic()">Next</a>
现在添加2个js函数nextPic()和prevPic(),例如:
function nextPic() {
var current = null;
$('.gallery_item').each(function() {
if($(this).attr('current')=='1') {
current = $(this);
return false;
}
});
if(current!=null) {
if(current.parent().next().length!=0) {
showPic(current.parent().next().find('.gallery_item')[0]); // <- show next pic
}
else {
showPic($('.gallery_item:first')[0]); // if no next pic, show first pic
}
}
return false;
}
function prevPic() {
var current = null;
$('.gallery_item').each(function() {
if($(this).attr('current')=='1') {
current = $(this);
return false;
}
});
if(current!=null) {
if(current.parent().prev().length!=0) {
showPic(current.parent().prev().find('.gallery_item')[0]); // <- show next pic
}
else {
showPic($('.gallery_item:last')[0]); // if no next pic, show first pic
}
}
return false;
}
还要添加它,以默认将第一张图像初始化为当前图像。
$().ready(function() {
$('.gallery_item:first').attr('current', '1');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.