如何在jquery中添加带图像滑块的链接

时间:2013-10-21 17:41:26

标签: javascript jquery performance

我创建了一个图像滑块,但现在我想在每个图像上添加链接。

(function(){
 var srcs = [
"/images/options/family_4.jpg",
"/images/options/family_5.jpg",
"/images/options/family_3.jpg"
];
var links = [
"http://google.com",
"http://youtube.com",
"http://yahoo.com"
];

var currentIndex = srcs.length-1;
var changeBanner = function(){
    if(currentIndex == 0){
        da.jQuery(".banner-fade-img").fadeIn();
        currentIndex = srcs.length-1;
    } else {
        da.jQuery(da.jQuery(".banner-fade-img").get(currentIndex)).fadeOut();
        currentIndex--;
    }
}

var bannerSetup = function(){
_.each(srcs, function(src, index){
    da.jQuery(".image-container").append("<img class='banner-fade-img' style='top:0px;position:absolute; z-index:"+(index+2)+";' src=' "+src+"'></img>");

})
window.setInterval(changeBanner, 5000);
}

bannerSetup();
}())

我制作了两个数组,但我不知道如何使用image添加第二个数组。请帮助我......如何解决?

2 个答案:

答案 0 :(得分:1)

为什么不在一个数组文字中合并图像src和a href?它会更容易,你可以这样做:

(function(){
    var datas = [
        {
            'image': "http://i.imgur.com/1fWTOIj.png",
            'url': "http://google.com",
        },
        {
            'image': "http://i.imgur.com/AhWrnN4.gif",
            'url': "http://youtube.com",
        },
    ]

    var currentIndex = datas.length-1;
        changeBanner = function(){
            if(currentIndex == 0){
                $(".banner-fade-img").fadeIn();
                currentIndex = datas.length-1;
            } else {
                $($(".banner-fade-img").get(currentIndex)).fadeOut();
                currentIndex--;
            }
        }
    console.log(currentIndex);

    var bannerSetup = function(){
        $.each(datas, function(index, src) {
            $(".image-container").append(
                '<a href="' + src.url + '">'
            +'<img class="banner-fade-img" width="150" height="150" src="' + src.image + '">'
                +'</a>'
            );
        })
        window.setInterval(changeBanner, 5000);
    }

    bannerSetup();
}())

http://jsfiddle.net/alleks/X6sfv/1/

答案 1 :(得分:0)

我正在疯狂猜测。试试这个

append("<a href='"+links[index]+"' ><img class='banner-fade-img' style='top:0px;position:absolute; z-index:"+(index+2)+";' src=' "+src+"'></img></a>");