2013-03-08 77 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