【问题标题】:Issue Clicking on Array Thumbnails for full-size Image, with Next and Previous buttons问题单击阵列缩略图以获取全尺寸图像,带有“下一个”和“上一个”按钮
【发布时间】:2015-12-02 03:58:00
【问题描述】:

我已经知道如何通过单击按钮来替换我的背景图像,但现在我想在“图库”中添加缩略图图像,您也可以单击它。我已经想出了如何通过单击“下一个”和“上一个”按钮来更改图像,但是每当我尝试同时单击缩略图时,它真的把事情搞砸了。基本上,我希望能够单击缩略图并更改索引号,因此当您点击“下一个”按钮时,它会转到下一个图像,而在它返回到第二个图像之前,不管您点击的缩略图。为简洁起见,我只包含了“下一张图片”功能。

问题是当我点击缩略图时,它会转到正确的图像,但是如果你点击“下一步”按钮,它只会转到第二个图像,不管你是否在第 7 个图像上,第5个等等。所以,基本上,我不知道如何获取下一个图像函数来读取当前图像的索引号。

谢谢!!!

//beginning of the whole background slideshow function
$(function() {

//setting the arrays
var colorBackgrounds = new Array(
'url(backgrounds/photoTest/20140714-_MG_0604.jpg)',
'url(backgrounds/photoTest/20140714-_MG_0860.jpg)',
'url(backgrounds/photoTest/20140716-IMG_1296.jpg)'
);
 var backgroundThumbs = new Array(
'/backgrounds/photoTest/20140714-_MG_0604_TN.jpg)',
'/backgrounds/photoTest/20140714-_MG_0860_TN.jpg)',
'/backgrounds/photoTest/20140716-IMG_1296_TN.jpg)'
);

 var colorCurrent = 0;
 var tnCurrent = 0;

//populating the div with the thumbnails & click function (works)
 backgroundThumbs.forEach(function(value,index) {
    $('#thumbID_' + index).html('<img src="' + value + '" class="thumbImage" />');
    $('#thumbID_' + index).click(function() {
        var tnCurrent = index;
        var colorCurrent = tnCurrent;
    $('.bodyBackground').fadeOut(500, function() { 
        $('.bodyBackground').css({
            'background':colorBackgrounds[colorCurrent],
            'background-position':'center center',
            'background-repeat':'no-repeat',
            'background-attachment':'fixed'});
            $('.bodyBackground').fadeIn(500);           
        }); 
    });
});

//beginning of nextBackground function (works, but doesn't take into account thumbnail click changes)
$('.nextImageWrapper').click(function() {
    $('.bodyBackground').fadeOut(500, function() { 
        $('.bodyBackground').css({
            'background':colorBackgrounds[colorCurrent = ++colorCurrent % colorBackgrounds.length],
            'background-position':'center center',
            'background-repeat':'no-repeat',
            'background-attachment':'fixed'});
            $('.bodyBackground').fadeIn(500);

    }); 
});

【问题讨论】:

  • 您已经在全局范围内定义了colorCurrenttnCurrent。您应该在内部引用它而不是再次定义。这可能是问题所在。
  • 嗯,它们实际上在另一个函数中,所以它们并不是真正的全局,对吧?那么我应该将它们设为全局,然后使用 getElementById 和警报在变量中调用它们吗?这就是我展示我对其中的一些缺乏经验的地方!
  • 它们实际上是全球性的。您正在做的是在 document ready function 中定义它们,即当文档准备好时,您正在为这些变量分配一些东西,而 forEach 函数是不同的。在里面,你会想要引用那些而不是再次定义它们。然后检查您的问题是否已解决。
  • Ahhh... 我理解这个问题 - 我只是将变量移到这个函数之外,以便它们是全局的,然后看看我是否可以引用它们,看看会发生什么。谢谢!

标签: jquery html css image


【解决方案1】:
$(function() {

 var colorCurrent = 0;
 var tnCurrent = 0;

 // rest code

backgroundThumbs.forEach(function(value,index) {

 // rest code    

 tnCurrent = index;
 colorCurrent = tnCurrent;

 // rest code

});

});

看看这是否有效。

【讨论】:

  • 我刚刚这样做了,它可以工作,只是当您单击“nextImage”按钮时,它现在会跳过两个图像。我尝试了“ colorCurrent = tnCurrent - 1; ”并且它有效,但只是第一次,然后它再次跳过。非常令人沮丧-我以为我舔了它!
  • 我想通了 - 我将运算符添加到下一个和上一个函数的开头,然后在 css 转换器中引用它。效果很好!
  • 太好了。我知道您在以某种方式引用时遇到问题。除此之外,代码看起来还不错。
猜你喜欢
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 2016-11-10
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 2014-02-24
相关资源
最近更新 更多