[英]issue with 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;
});
}
});
测试了您的小提琴,javascript很好用,只有CSS缺少以下属性:
#gallery-wrapper {
position:relative;
}
#gallery {
position:absolute;
}
而且代码也需要在内部执行
$(window).load();
而不是$(document).ready(); 事件处理程序才能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.