我做了一个lynda.com的jquery教程,以设置一个java驱动(css格式)图像画廊,整齐的效果和灯箱。它工作正常。多个JavaScript驱动的图像在一个页面上的画廊
我现在想创建几个画廊的一个页面,并且每个画廊都只能显示与之相关的图像。问题是因为每个画廊显示相同的图像,标签/ ID都是相同的。 (有关该问题的示例,请参阅... http://www.chartoonz.com/portfolio/illustration.html)正如您所看到的基本功能一样。
如何获取java脚本来限制一个画廊的功能到该画廊并让其他人独处?我认为,因为jquery执行的是$(document).ready,它会在渲染时发生,所以如果我可以循环它,它一次只能做一个画廊,但只占用onload预览图像。我怎样才能将画廊分离开来,不管哪个缩略图被按下,他们都不会做同样的事情?我可以吗?
正如我所说我想我可以循环它,但我遇到了麻烦!我尝试将所有的gallery_content标签放入一个数组中,然后遍历数组,但我显然没有做到这一点,因为它不起作用。
我希望我明确表达了这个问题。任何帮助,将不胜感激。这是我的javascript有关...
// What to do when the document is ready
$(document).ready(function(){
// Capture the thumbnail links
$('.gallery_thumbnails a').click(function(e){
// Disable standard link behavior
e.preventDefault();
// Fade out thumbnails
// Commented out to be in their own function (/**/)
/*
$('.gallery_thumbnails a').removeClass('selected');
$('.gallery_thumbnails a').children().css('opacity', '1');
$(this).addClass('selected');
$(this).children().css('opacity', '.4');
*/
// Add variables to link thumbnail to preview
var photo_caption = $(this).attr('title');
var photo_fullsize = $(this).attr('href');
var photo_preview = photo_fullsize.replace('_fullsize', '_preview');
$('.gallery_caption').slideUp(500);
$('.gallery_preview').fadeOut(500, function(){
// preload
$('.gallery_preload_area').html('<img src="'+photo_preview+'"/>');
$('.gallery_preload_area img').imgpreload(function(){
// Write the HTML into the page
$('.gallery_preview').html('<a class="overlaylink" href="'+photo_fullsize+'"
title="'+photo_caption+'" style="background-image:url('+photo_preview+');"></a>');
// Update the html for the gallery caption
$('.gallery_caption').html('<p><a class="overlaylink zoom" href="'+photo_fullsize+'"
title="'+photo_caption+'">View larger</a></p><p>'+photo_caption+'</p>')
$('.gallery_preview').fadeIn(500);
$('.gallery_caption').slideDown(500);
setFancyboxLinks();
updateThumbnails();
});
});
}
// Initialize the gallery on load
var first_photo_caption = $('.gallery_thumbnails a').first().attr('title');
var first_photo_fullsize =$('.gallery_thumbnails a').first().attr('href');
var first_photo_preview = first_photo_fullsize.replace('_fullsize', '_preview');
$('.gallery_caption').slideUp(500);
$('.gallery_preview').fadeOut(500, function(){
// preload
$('.gallery_preload_area').html('<img src="'+first_photo_preview+'"/>');
$('.gallery_preload_area img').imgpreload(function(){
// Write the HTML into the page
$('.gallery_preview').html('<a class="overlaylink" href="'+first_photo_fullsize+'" title=
"'+first_photo_caption+'" style="background-image:url('+first_photo_preview+');"></a>');
// Update the html for the gallery caption
$('.gallery_caption').html('<p><a class="overlaylink zoom" href="'+first_photo_fullsize+'"
title="'+first_photo_caption+'">View larger</a></p><p>'+first_photo_caption+'</p>')
$('.gallery_preview').fadeIn(500);
$('.gallery_caption').slideDown(500);
setFancyboxLinks();
updateThumbnails();
});
});
尝试添加一个id这样每个 “galleryBodyWrapper” 分区: DIV CLASS = “galleryBodyWrapper” ID = “gallery1”> ... 那么你可以写为每个单独的选择这样的画廊: $('#gallery1 .gallery_thumbnails')。click(function(e){... 当然,您将不得不为每个您使用的画廊编写一次代码。一个更好的方法是将图库代码打包为可重用对象,并将jQuery选择器传递到其构造函数中。 我该怎么做?我是新来的... – 2012-07-13 17:16:26
顺便说一句我希望不必硬编码重复功能。我们的目标是在本地推动画廊功能,同时能够处理任意数量的画廊。我希望我能解决这个问题。 我试过了ID标签,并尝试创建一个可以找到各种ID的数组,但不能在getElementById中引用通配符。我也一直在寻找jquery ...我几乎不是一个专家,但迄今nothings工作...任何帮助将不胜感激。 – 2012-07-13 21:58:48
您没有指定您想避免原始问题中的代码重用。我修改了我的答案。 – underscorePez 2012-07-22 10:28:57