0

我正在为我的背景幻灯片使用很棒的插件jQuery BackStretch 。这是如何将图像传递给它的常用方法。

$.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});

显然,这不起作用 - 根据 FireBug,未定义可变图像 (?) 。尽管如果我“警告”该变量,它会显示以逗号分隔的图像 url。

知道我在做什么错吗?谢谢!

4

1 回答 1

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>
于 2013-03-10T07:23:40.063 回答