我爲我的網站構建了一個燈箱畫廊。我在選中的當前圖像的左側和右側添加了箭頭。我需要一些關於如何使左箭頭選擇上一張圖像和右箭頭選擇圖庫中下一張圖像的指導。我一直在試驗和尋找,但我一直無法自己弄清楚。任何幫助,將不勝感激。謝謝!如何在我的燈箱畫廊中添加導航?
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){將畫廊帶回到開頭的東西} –