感謝Phrogz這部分 - 給圖像容器white-space: nowrap;
和display: inline-block;
。
您可以計算寬度,而不必每次計算兒童的寬度,但您需要計算一次兒童的寬度。
//global variables
var currentWidth = 0;
var slideDistance = 0;
var totalSize = 0;
var dispWidth = (winWidth/2); //this should get you the middle of the page -- see below
var spacing = 6; //padding or margins around the image element
$(Document).Ready(function() {
$("#Gallery li").each(function() {
totalSize = totalSize + parseFloat($(this).children().attr("width"));// my images are wrapped in a list so I parse each li and get it's child
});
totalSpacing = (($("#Gallery li").siblings().length - 1) * spacing); //handles the margins between pictures
currentWidth = (parseFloat($("#Gallery li.pictureSelected").children().attr("width")) + spacing);
maxLeftScroll = (dispWidth - (totalSize + totalSpacing)); //determines how far left you can scroll
});
function NextImage() {
currentWidth = currentWidth + (parseFloat($("#Gallery li.pictureSelected").next().children().attr("width")) + spacing); //gets the current width plus the width of the next image plus spacing.
slideDistance = (dispWidth - currentWidth)
$("#Gallery").animate({ left: slideDistance }, 700);
}
有一種方法可以在javascript中獲取瀏覽器窗口(jQuery示例)。 並且有一種方法可以捕獲調整大小事件。
var winWidth = $(window).width()
if (winWidth == null) {
winWidth = 50;
}
$(window).resize(function() {
var winNewWidth = $(window).width();
if (winWidth != winNewWidth) {
window.clearTimeout(timerID);
timerID = window.setInterval(function() { resizeWindow(false); }, 100);
}
winWidth = winNewWidth;
});
在我的畫廊裏實際上有很多,但這應該讓你指出正確的方向。
對於單個問題,這是很多問號。 – Phrogz