2014-02-25 99 views
0

我有这两个轮播随机选取图片。 [fiddle]他们每个人(#carousel1,#carousel2)从他们自己不同的内部阵列中挑选图片和链接。将附加值附加到jQuery中每个ID的变量

我的问题是轮播将有自己的图像路径和链接到不同的子域,但该脚本使用addImage("/pic/" + images[i][0], "/url/" + images[i][1]);}中的实际路径(“url”和“pic”)。唯一的解决办法我能想出使用的域名是作为一个变量

var domain = "www.picture.com"; 
实际路径的地方

“/ PIC /”和“/ URL /”,然后追加实际图像路径和链接根据轮播ID确定domain。我不知道如何做第二部分,如果这是正确的方式来做到这一点。谁会请给我一些解决方案?

function randomizeCarousel(selector, images) { 
    var i, l, carousel = $(selector); 

    function shuffle(images) { 
    var i, shuffled = []; 
    while(images.length) { 
     i = Math.floor(Math.random() * images.length); 
     shuffled = shuffled.concat(images.splice(i, 1)); 
    } 
    return shuffled; 
    } 

    function addImage(src, href) { 
    var container = $("<div>"), 
     link  = $("<a></a>").attr("href", href).appendTo(container), 
     img  = $("<img/>").attr("src", src).appendTo(link); 
    carousel.append(container); 
    } 

    images = shuffle(images.slice(0)); 
    for(i = 0, l = images.length; i < l; i++) { 
    addImage("/pic/" + images[i][0], "/url/" + images[i][1]); 
    } 

    carousel.jsCarousel({ 
    onthumbnailclick: function(src) { load(src); }, 
    autoscroll:  true, 
    circular:   true, 
    masked:   false, 
    itemstodisplay: 3, 
    orientation: 'h' 
    }); 
} 

$(function() { 
    randomizeCarousel("#carousel1", [...]); // add your 1st array here 
    randomizeCarousel("#carousel2", [...]); // add your 2nd array here 
    // ... etc. 
}); 
+0

我想我没有真正看到的问题在你的代码..为什么不只是创建2个域VAR和(域内,如果不同的路径),2路乏,并把它们作为合适? –

回答

0

是这样的?

var domain1,domain2,path1,path2; 
    domain1 = 'http://domain.tld'; 
    domain2 = 'http://domain2.tld'; 
    path1 = domain1+"/pic/"; 
    path2 = domain2+"/url/"; 
    addImage(path1 + images[i][0], path2 + images[i][1]); 
+0

有没有一种方法可以为'#carousel1'分配path1而只为'#carousel2'分配path2?从我所了解的情况来看,你的建议只会混合传送带中的所有动作。 – RedGiant

+0

我不知道你的插件是如何设置的,但你应该能够将它作为一个可选参数传递,或者在插件本身内部设置一些东西来处理它。 –