は、簡単なスライダコードであり、私は、変数、関数クロージャが動作しているかを理解したかった...この部分で機能変数閉鎖
(function($) {
var sliderUL = $('div.slider').css('overflow', 'hidden').children('ul'),
imgs = sliderUL.find('img'),
imgWidth = imgs[0].width, // 600
imgsLen = imgs.length, // 4
current = 1,
totalImgsWidth = imgsLen * imgWidth; // 2400
$('#slider-nav').show().find('button').on('click', function() {
var direction = $(this).data('dir'),
loc = imgWidth; // 600
// update current value
**(direction === 'next') ? ++current : --current;
// if first image
if (current === 0) {
current = imgsLen;
loc = totalImgsWidth - imgWidth; // 2400 - 600 = 1800
direction = 'next';
} else if (current - 1 === imgsLen) { // Are we at end? Should we reset?
current = 1;
loc = 0;
}
transition(sliderUL, loc, direction);**
});
function transition(container, loc, direction) {
var unit; // -= +=
if (direction && loc !== 0) {
unit = (direction === 'next') ? '-=' : '+=';
}
container.animate({
'margin-left': unit ? (unit + loc) : loc
});
}
})(jQuery);
:もし(現在で
$('#slider-nav').show().find('button').on('click', function() {
var direction = $(this).data('dir'),
loc = imgWidth; // 600
// update current value
(direction === 'next') ? ++current : --current;
// if first image
if (current === 0) {
current = imgsLen;
loc = totalImgsWidth - imgWidth; // 2400 - 600 = 1800
direction = 'next';
} else if (current - 1 === imgsLen) { // Are we at end? Should we reset?
current = 1;
loc = 0;
}
transition(sliderUL, loc, direction);
});
=== 0)block、最初のブロックで変更された後の変数current、loc、directionは更新されていますか?それから、それらは以下の遷移関数に渡されますか?
else(current-1 === imgsLen)がtrueの場合、currentとlocはそれらに割り当てられた前の値を上書きしてから、transition関数に渡されますか?
ありがとうございました。非常に良い説明! – nick