-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
74 lines (61 loc) · 2.5 KB
/
slider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function initializeSlider(config) {
console.log("Init slider for config" + config.slider_id)
const sliderWrapper = $(config.slider_id + ' .product-slider');
const slider = $(config.slider_id + ' .slider-content');
const sliderControls = $(config.slider_id + ' .slider-controls');
let currentIndex = 0;
const getWindowSize = () => {
const screenWidth = window.innerWidth;
const windowSize = config.slider_width_ranges.find(range => screenWidth >= range.range[0] && screenWidth <= range.range[1]);
return windowSize
};
const getSliderWidth = () => {
const windowRange = getWindowSize();
return windowRange.slider_width;
};
const updateVisibleItems = () => {
updateSliderControls()
slider.css('transform', `translateX(${config.slider_offset * -currentIndex}px)`);
};
const updateSliderControls = () => {
const sliderWidth = getSliderWidth();
const maxItemWidth = config.item_width;
const maxVisibleItems = Math.floor(sliderWidth / (maxItemWidth + 20));
const itemsCount = $(config.slider_id + ' .product-item').length / $(config.slider_id).length;
if (currentIndex === itemsCount - maxVisibleItems) {
$(config.slider_id + ' .slider-control.right').css('display', 'none');
} else {
$(config.slider_id + ' .slider-control.right').css('display', 'block');
}
if (currentIndex === 0) {
$(config.slider_id + ' .slider-control.left').css('display', 'none');
} else {
$(config.slider_id + ' .slider-control.left').css('display', 'block');
}
}
const updateSliderWidth = () => {
const sliderWidth = getSliderWidth();
currentIndex = 0;
slider.css('transform', `translateX(0)`);
sliderWrapper.css('max-width', `${sliderWidth}px`);
}
const handleArrowClick = (direction) => {
const itemsCount = $(config.slider_id + ' .product-item').length / $(config.slider_id).length;
const maxVisibleItems = Math.floor(getSliderWidth() / (config.item_width + 20));
if (direction === 'left' && currentIndex > 0) {
currentIndex--;
} else if (direction === 'right' && currentIndex < itemsCount - maxVisibleItems) {
currentIndex++;
}
updateVisibleItems();
};
$(config.slider_id + ' .slider-control.left').click(() => handleArrowClick('left'));
$(config.slider_id + ' .slider-control.right').click(() => handleArrowClick('right'));
updateVisibleItems();
updateSliderWidth();
updateSliderControls();
$(window).resize(() => {
updateSliderWidth();
updateSliderControls();
});
}