2013-03-10 65 views
0

我为我的背景幻灯片使用了真棒插件jQuery BackStretch。这是如何将图像传递给它的常用方法。从HTML动态提供幻灯片图片(jQuery地图/获取)

$.backstretch([ 
    "http://dl.dropbox.com/u/515046/www/outside.jpg", 
    "http://dl.dropbox.com/u/515046/www/garfield-interior.jpg", 
    "http://dl.dropbox.com/u/515046/www/cheers.jpg" 
], {duration: 3000, fade: 750}); 

我不想通过硬编码为它提供图像,但我希望函数能够抓住我的PHP在该容器内生成的内容。这是迄今为止我所拥有的。

var images = $('#background img').map(function() { return this.src; }).get(); 
$.backstretch([$images], {duration: 3000, fade: 750}); 

而且,很显然,不能正常工作 - 根据萤火,可变图像没有定义(?)。虽然如果我“提醒”这个变量,它会显示逗号分隔的图像。

任何想法我做错了什么?谢谢!

+0

什么php的样子 – 2013-03-10 07:18:34

+0

还是你想用'从'images'右侧列表backstretch' ?假设这是'#background img'选择器。 – 2013-03-10 07:19:39

回答

1

这应该就够了。 backstretch期望第一个参数是一个引用图像的字符串数组。

$(document).ready(function() { 
    var images = []; 

    // iterate over your selection 
    $('#background img').each(function() { 
     // save source to list 
     images.push(this.src); 
    }); 

    // use plugin 
    $.backstretch(images, {duration: 3000, fade: 750}); 
}); 

而且我假设你的HTML是这样的......

<div id="background"> 
    <img src="http://dl.dropbox.com/u/515046/www/outside.jpg" /> 
    <img src="http://dl.dropbox.com/u/515046/www/garfield-interior.jpg" /> 
</div> 
+0

非常感谢,这个作品! – 2013-03-10 11:04:45