2013-03-08 81 views
0

我正在使用jquery cyclyer在圖像之間循環使用nextprevious按鈕。我騎自行車的照片位於我的頁面中央。除了圖片之外,還有文字(與圖片相匹配)在圖片循環時應該改變。一個例子可以看到herejQuery Cycle圖像和文本

我不能在圖像上使用alt='',只顯示該文本,因爲我想將某些樣式應用於顯示文本的div。有沒有辦法使用jQuery來完成文本更改?

+0

如果您使用的是jQuery Cycle,您可以使用'div'或'figure'將圖像和文本全部封裝在一個塊級容器中;該插件會將每個容器視爲一張幻燈片。有關Cycle2的[示例](http://jquery.malsup.com/cycle/int2.html)(或[本示例](http://jquery.malsup.com/cycle2/demo/non-image.php)) )。 – Blazemonger 2013-03-08 15:16:01

+0

您應該添加該答案@Blazemonger;) – Simon 2013-03-08 15:18:12

回答

0

你能創建的文本的數組,並更新文本框中的文本...

var textArray = []; 
textArray[0] = "This is the text for the first image"; 
textArray[1] = "This is the text for the second image"; 
textArray[2] = "This is the text for the third image"; 

然後給每個圖像像一個唯一的標識符:

<img id="image_0" src="picture1.jpg" /> 
<img id="image_1" src="picture2.jpg" /> 
<img id="image_2" src="picture3.jpg" /> 

而當圖像循環,做一個簡單的查找和替換

//get the identifier from the image ID, assuming that 'this' 
//refers to the image you just cycled 

    var text_index = this.id.replace('image_',''); 

    $("p#text_box").html(textArray[text_index]); 

你可以風格p#text_box,但你喜歡與CSS

+0

這工作,但我用這個選項來包裝它在div和設置固定的位置。但非常感謝你,我將來會使用它。 – robsik 2013-03-08 16:01:09

0

如果您使用的是jQuery Cycle,則可以使用div s或figure s將圖像和文本全部封裝在一個塊級容器中;該插件會將每個容器視爲一張幻燈片。請參閱example(或Cycle2的this example)。

<div id="slideshow2" class="pics"> 
    <figure> 
     <a href="images/1.jpg" rel="lightbox"><img src="images/1m.jpg"></a> 
     <figcaption>Text text text text</figcaption> 
    </figure> 
    <figure> 
     <a href="images/2.jpg" rel="lightbox"><img src="images/2m.jpg"></a> 
     <figcaption>Text text text text</figcaption> 
    </figure> 
    <figure> 
     <a href="images/3.jpg" rel="lightbox"><img src="images/3m.jpg"></a> 
     <figcaption>Text text text text</figcaption> 
    </figure> 
</div> 

然後通過使用CSS的定位,你可以把figcaption其他任何你想要的頁面上。

+0

非常感謝。我用div包裹它並設置固定的位置來放置我需要的位置。這其實很簡單,但我沒有弄明白。再次感謝! – robsik 2013-03-08 16:00:05