我为我的网站构建了一个灯箱画廊。我在选中的当前图像的左侧和右侧添加了箭头。我需要一些关于如何使左箭头选择上一张图像和右箭头选择图库中下一张图像的指导。我一直在试验和寻找,但我一直无法自己弄清楚。任何帮助,将不胜感激。谢谢!如何在我的灯箱画廊中添加导航?
https://jsfiddle.net/fabfivefebby/2z9unx02/2/
var $overlay = $('<div id="overlay"></div>');
var $image = $("<img>");
var $caption = $("<p></p>");
var $exit = $("<div><button id='exit'> x </button></div>");
var $buttonLeft = $("<button id='buttonLeft'> < </button>");
var $buttonRight = $("<button id='buttonRight'> > </button>");
$overlay.append($exit);
$overlay.append($buttonLeft);
$overlay.append($buttonRight);
$overlay.append($image);
//2.2 A Caption
$overlay.append($caption);
//2. Add overlay
$("body").append($overlay);
var updateImage = function (imageLocation, captionText) {
//1.2 Update overlay with the imagelinked in the link
$image.attr("src", imageLocation);
//1.3 Add Caption from alt. Get child's alt att and set Caption
$caption.text(captionText);
}
//1. Capture the click event on a link to an image
$("#stillGallery a").click(function(event) {
event.preventDefault();
var imageLocation = $(this).attr("href");
var captionText = $(this).children("img").attr("alt");
$index = $(this).parent().index();
//this is calling that new Update overlay function above
updateImage(imageLocation, captionText);
//1.1 Show the overlay
$overlay.show();
});
$buttonRight.click(function() {
console.log("testing");
});
$exit.click(function(){
$overlay.hide();
});
$overlay.click(function(){
$overlay.hide();
});
谢谢。我现在已经有了这个工作,但是当我到达画廊的开头或结尾时,它似乎会冻结。我的愿景是让它循环并重新开始。我用index();获取元素的位置。我正在考虑使用if语句:if(temp.index()=== -1){将画廊带回到开头的东西} –