我用拇指和主图像创建了fancybox,但我有问题,如果我关闭它,下次打开,那么标记的拇指不是实际的一个,而是最后一个。
我想通过比较主图像和拇指图像的源代码,使代码将根据实际图像改变它,但我找不到通过 AJAX 动态添加的拇指图像。
你能给我点建议吗?
// Show fancybox after click on a elemet of gallery
gallery.find('a').on('click', function(event) {
event.preventDefault();
// Show fancybox
overlay.appendTo('body');
// Define variables
var galleryImgWrapperHref = $(this).attr('href');
// Show chosen image in fancybox
image.attr('src', galleryImgWrapperHref);
// Show loadin image before image is loaded
loadingImg.appendTo('#fancybox__img-wrapper');
// Image will show after it will be loaded, before loading image is showed
image.on('load', function() {
image.appendTo('#fancybox__img-wrapper');
loadingImg.hide(); // Hide loading image afet image was loaded
});
// Make images slider for thumbnails from gallery
if (thumbWrapper.length == 0) { // add thumbs just if it was not already added
// Get id of current item
var itemId = $('.item-detail').attr('id');
// LOAD photos data to object
$.ajax({
url: '_inc/getPhotosFromDb.php',
type: 'post',
data: {
item_id: itemId
}
}).done(function(html) {
galleryImgs = $(html);
galleryImgs.each(function() {
var src = $(this).attr('src'),
href = src.replace('thumbs/','');
thumbWrapper = $('<a/>', {id: 'fancybox__img-thumb-wrapper'}),
thumbImg = $('<img>', {id: 'fancybox__img-thumb'});
// Add thumb image src and id from original image and href to a element from original e element in gallery
thumbWrapper.attr('href', href);
thumbImg.attr('src', src);
thumbWrapper.html(thumbImg); // Put image into thumbImgWrapper
thumbWrapper.appendTo('#fancybox__thumb-slider');
// If src of thumb and main image is the same then mark thumb as active
if ((thumbImg.attr('src').substring(thumbImg.attr('src').lastIndexOf("/") + 1) == image.attr('src').substring(image.attr('src').lastIndexOf("/") + 1))) {
thumbWrapper.addClass('img-active');
}
})
});
}
});
我想把这段代码放在开始,但即使我使用$(document).find(...)
它甚至找不到它。
// If src of thumb and main image is the same then mark thumb as active
if (($(document).find('#fancybox__img-thumb').attr('src').substring($(document).find('#fancybox__img-thumb').attr('src').lastIndexOf("/") + 1) == image.attr('src').substring(image.attr('src').lastIndexOf("/") + 1))) {
thumbWrapper.addClass('img-active');
}
1条答案
按热度按时间ukqbszuj1#
在Ouroborus的评论后,我找到了解决方案: