轮播图片库的问题

时间:2013-05-19 04:20:57

标签: javascript jquery carousel image-gallery

我正在制作一个简单的旋转木马图片库。目前我有两个图像,我希望画廊循环回到最后一个后的第一个图像,我无法弄清楚如何使这个功能工作加上我的上一个和下一个按钮由于某种原因停止工作。我的jq / js技能很缺乏,并希望得到任何帮助以实现这一目标。这是我的代码[1]的jsfiddle:http://jsfiddle.net/jsavage/kGAxa/39/

$(document).ready(function () {
    if (jQuery("#gallery").length) {
        // declare variables
        var totalImages = jQuery("#gallery > li").length,
            imageWidth = jQuery("#gallery > li:first").outerWidth(true),
            totalWidth = imageWidth * totalImages,
            visibleImages = Math.round(jQuery("#gallery-wrapper").width() / imageWidth),
            visibleWidth = visibleImages * imageWidth,
            stopPosition = (visibleWidth - totalWidth);

        jQuery("#gallery").width(totalWidth);

        jQuery("#gallery-prev").click(function () {
            if (jQuery("#gallery").position().left < 0 && !jQuery("#gallery").is(":animated")) {
                jQuery("#gallery").animate({
                    left: "+=" + imageWidth + "px"
                });
            }
            return false;
        });

        jQuery("#gallery-next").click(function () {
            if (jQuery("#gallery").position().left > stopPosition && !jQuery("#gallery").is(":animated")) {
                jQuery("#gallery").animate({
                    left: "-=" + imageWidth + "px"
                });
            }
            return false;
        });
    }

});

1 个答案:

答案 0 :(得分:0)

测试你的小提琴,javascript工作正常,只有css缺少以下属性:

#gallery-wrapper {
    position:relative;
}

#gallery {
    position:absolute;
}

代码也需要在

中执行
$(window).load();

而不是$(document).ready();事件处理程序才能正常工作。

http://jsfiddle.net/kGAxa/42/

相关问题