2015-12-01 65 views
0

我已經想出瞭如何讓我的背景圖像替換爲點擊按鈕,但現在我想在「圖庫」中放置縮略圖圖像「你也可以點擊。我已經想出瞭如何通過單擊「下一個」和「上一個」按鈕來改變圖像,但是每當我嘗試合併單擊縮略圖時,它都會讓事情變得麻煩。基本上,我希望能夠點擊一個縮略圖並更改索引編號,因此當您點擊「下一步」按鈕時,它將按順序進入下一個圖像,而在它回到排隊的第二個圖像之前,無論你點擊了什麼縮略圖。爲了簡潔起見,我只包含了「下一張圖片」功能。問題點擊數組縮略圖查看全尺寸圖片,使用下一個和上一個按鈕

問題是,當我點擊縮略圖時,它會轉到正確的圖像,但如果您點擊「下一步」按鈕,它會轉到第二張圖像,無論您是否在第7張圖像上,第五等。所以,基本上,我不知道如何獲得下一個圖像函數來讀取當前圖像的索引號。

謝謝!!!

//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); 

    }); 
}); 
+0

您已經在全局範圍內定義了'colorCurrent'和'tnCurrent'。你應該在裏面引用它,而不是再次定義。這可能是問題所在。 – marukobotto

+0

那麼,他們實際上是在另一個功能,所以他們不是真正的全球化,對吧?那麼我應該讓它們成爲全局變量,然後使用getElementById和alert來在變量內部調用它們?這就是我展示我是如何缺乏經驗的地方! – EternalFootman

+0

他們實際上是全球性的。你正在做的是你在'document ready function'中定義它們,即當文檔準備就緒時,那麼你正在給這些變量賦值,而'forEach'函數是不同的。在裏面,你會想引用這些,而不是再次定義它們。然後檢查你的問題是否已經解決。 – marukobotto

回答

0
$(function() { 

var colorCurrent = 0; 
var tnCurrent = 0; 

// rest code 

backgroundThumbs.forEach(function(value,index) { 

// rest code  

tnCurrent = index; 
colorCurrent = tnCurrent; 

// rest code 

}); 

}); 

看看是否能工程。

+0

我只是這樣做,它的作品,除了當你點擊「nextImage」按鈕,它現在跳過兩個圖像。我嘗試了 「colorCurrent = tnCurrent - 1;」 ,這是有效的,但只有第一次,然後再跳過。非常令人沮喪 - 我以爲我舔了它! – EternalFootman

+0

我想通了 - 我將操作符添加到下一個和前一個函數的開頭,然後在css轉換器中引用它。它很棒! – EternalFootman

+0

太棒了。我知道你在以某種方式引用時遇到問題。除此之外,代碼看起來沒問題。 – marukobotto

相關問題