我正在編輯我的Shopify主題的theme.js文件,以改變圖片輪播的默認屬性。我想有按鈕控制旋轉木馬循環開始和結束時,向后/向前。下面是旋轉木馬的代碼,我很難理解它的意思。任何幫助添加一個語句,檢查它是否在cc- start或cc-end,以將其偏移到正確的位置,將不勝感激!
// Initialises Featured Collection(s) carousels
theme.carousels = {
init: () => {
var _this = this;
_this.updateArrows = (carousel) => {
var section = $(carousel).closest('.section, .cc-carousel-section')[0];
if (carousel.scrollLeft < 15) {
section.classList.add('cc-start');
} else {
section.classList.remove('cc-start');
}
if (carousel.scrollLeft + carousel.clientWidth > carousel.scrollWidth - 15) {
section.classList.add('cc-end');
} else {
section.classList.remove('cc-end');
section.classList.add('cc-carousel-scrolling');
}
};
_this.initArrows = () => {
var carousels = document.querySelectorAll(".cc-carousel");
carousels.forEach((carousel) => {
var $sectionContainer = $(carousel).closest('.section, .cc-carousel-section');
var $carouselNext = $sectionContainer.find('.cc-carousel--next');
var $carouselPrevious = $sectionContainer.find('.cc-carousel--previous');
$carouselNext.addClass('cc-initialized');
$carouselPrevious.addClass('cc-initialized');
if ($carouselNext.length && $carouselPrevious.length) {
$carouselNext.on('click.cc-carousel', function (evt) {
evt.preventDefault();
//Get the left hand edge of the next card out of view
var nextLeftOffset = -1;
var carouselOffsetLeft = $(this).closest('.section, .cc-carousel-section').find('.cc-carousel').offset().left;
$(carousel).find('.column').each(function () {
var offsetLeft = parseInt($(this).offset().left - carouselOffsetLeft);
if (offsetLeft > carousel.scrollLeft + carousel.clientWidth - 20 && nextLeftOffset === -1) {
nextLeftOffset = offsetLeft;
}
});
if (nextLeftOffset === -1) {
nextLeftOffset = carousel.scrollLeft + carousel.clientWidth;
}
_this.scrollLeft(carousel, nextLeftOffset);
});
$carouselPrevious.on('click.cc-carousel', function (evt) {
evt.preventDefault();
//Get the left hand edge of the next card out of view
var nextLeftOffset = -1;
if (theme.viewport.isXs()) {
var carouselOffsetLeft = $(this).closest('.section, .cc-carousel-section').find('.cc-carousel').offset().left;
var prevLeftOffsets = {};
$(carousel).find('.column').each(function (index) {
var offsetLeft = parseInt($(this).offset().left - carouselOffsetLeft);
if (offsetLeft >= 0 && nextLeftOffset === -1) {
if ($(this).hasClass('min-cols-2') && prevLeftOffsets[index - 2]) {
nextLeftOffset = carousel.scrollLeft + prevLeftOffsets[index - 2];
} else {
nextLeftOffset = carousel.scrollLeft + prevLeftOffsets[index - 1];
}
}
prevLeftOffsets[index] = offsetLeft;
});
}
if (nextLeftOffset === -1) {
nextLeftOffset = carousel.scrollLeft - carousel.clientWidth;
}
_this.scrollLeft(carousel, nextLeftOffset);
});
}
_this.updateArrows(carousel);
});
};
_this.scrollLeft = (carousel, left) => {
carousel.classList.add('cc-carousel--smooth-scroll');
carousel.scrollLeft = left;
setTimeout(() => {
_this.updateArrows(carousel);
carousel.classList.remove('cc-carousel--smooth-scroll');
}, 1000); // unsure about this number
};
_this.initMobileCarousels = () => {
if (theme.viewport.isXs()) {
var uninitializedMobCarousels = document.querySelectorAll(".cc-carousel-mobile:not(.cc-carousel)");
uninitializedMobCarousels.forEach((carousel) => {
carousel.classList.add('cc-carousel');
var $sectionContainer = $(carousel).closest('.section, .cc-carousel-section');
$sectionContainer.find('.cc-carousel--next').addClass('cc-initialized');
$sectionContainer.find('.cc-carousel--previous').addClass('cc-initialized');
$sectionContainer.addClass('cc-is-carousel');
});
} else {
var initializedMobCarousels = document.querySelectorAll(".cc-carousel-mobile.cc-carousel");
initializedMobCarousels.forEach((carousel) => {
carousel.classList.remove('cc-carousel');
var $sectionContainer = $(carousel).closest('.section, .cc-carousel-section');
$sectionContainer.find('.cc-carousel--next').removeClass('cc-initialized');
$sectionContainer.find('.cc-carousel--previous').removeClass('cc-initialized');
$sectionContainer.removeClass('cc-is-carousel');
});
}
};
_this.initCarousels = () => {
var carousels = document.querySelectorAll(".cc-carousel");
if (carousels.length) {
//Init click to drag on desktop browsers (except for Safari)
if (theme.viewport.isSm() && !theme.browser.isSafari()) {
carousels.forEach((carousel) => {
var isDown = false,startX,scrollLeft;
if (!carousel.classList.contains('cc-initialized')) {
carousel.classList.add('cc-initialized');
carousel.addEventListener("mousedown", (e) => {
isDown = true;
startX = e.pageX - carousel.offsetLeft;
scrollLeft = carousel.scrollLeft;
});
carousel.addEventListener("mouseleave", () => {
isDown = false;
});
carousel.addEventListener("mouseup", () => {
isDown = false;
_this.updateArrows(carousel);
});
carousel.addEventListener("mousemove", (e) => {
if (!isDown) return;
e.preventDefault();
var x = e.pageX - carousel.offsetLeft;
var walk = x - startX;
carousel.scrollLeft = scrollLeft - walk;
_this.updateArrows(carousel);
});
}
});
} else {
carousels.forEach((carousel) => {
//Update arrows on touch end
carousel.addEventListener("touchend", () => {
_this.updateArrows(carousel);
});
//Update arrows on scroll end
var timer;
carousel.addEventListener('scroll', () => {
clearTimeout(timer);
timer = setTimeout(() => {
_this.updateArrows(carousel);
}, 300);
}, { passive: true });
});
}
//Init left/right buttons
_this.initArrows();
}
};
if (document.querySelectorAll(".cc-carousel-mobile").length) {
$(window).off('debouncedresizewidth.cc-carousel').on('debouncedresizewidth.cc-carousel', _this.initMobileCarousels);
_this.initMobileCarousels();
}
_this.initCarousels();
},
destroy: () => {
var carousels = document.querySelector(".cc-carousel");
if (carousels && carousels.length) {
carousels.forEach((carousel) => {
carousel.removeEventListener('mousedown').
removeEventListener('mouseleave').
removeEventListener('mouseup').
removeEventListener('mousemove').
classList.remove('cc-initialized');
var $sectionContainer = $(carousel).closest('.section, .cc-carousel-section');
$sectionContainer.find('.cc-carousel--previous').removeClass('cc-initialized');
$sectionContainer.find('.cc-carousel--next').removeClass('cc-initialized');
});
}
$(window).off('.cc-carousel');
} };
;
曾嘗試使用偏移甚至滑離頁面,但無濟于事。