-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
120 lines (92 loc) · 3.13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$(document).ready(function() {
let position = 0;
// const slidesToShow = 2;
const windWidth = window.screen.width,
slidesToShow = windWidth > 1200 ? 2 : windWidth < 700 ? 1 : 1;
// slidesToShow = windWidth < 700 ? 1 : 2;
const slidesToScroll = 1;
const container = $('.slider-container');
const track = $('.slider-track');
const item = $('.slider-item');
const btnPrev = $('.btn-prev');
const btnNext = $('.btn-next');
const itemsCount = item.length;
const itemWidth = container.width() / slidesToShow;
const movePosition = slidesToScroll * itemWidth;
item.each(function (index, item) {
$(item).css({
minWidth: itemWidth,
});
});
btnNext.click(function(){
const itemsLeft = itemsCount - (Math.abs(position) + slidesToShow * itemWidth) / itemWidth;
position -= itemsLeft >= slidesToScroll ? movePosition : itemsLeft * itemWidth;
setPosition();
checkBtns();
});
btnPrev.click(function() {
const itemsLeft = Math.abs(position) / itemWidth;
position += itemsLeft >= slidesToScroll ? movePosition : itemsLeft * itemWidth;
setPosition();
checkBtns();
});
const setPosition = () => {
track.css({
transform: `translate(${position}px)`
});
};
const checkBtns = () => {
btnPrev.prop('disabled', position === 0);
btnNext.prop(
'disabled',
position <= -(itemsCount - slidesToShow) * itemWidth
);
};
checkBtns();
});
$(document).ready(function() {
let position = 0;
// const slidesToShow = 1;
const windWidth = window.screen.width,
slidesToShow = windWidth > 1500 ? 2 : windWidth < 700 ? 1 : 1;
// slidesToShow = windWidth < 700 ? 1 : 2;
const slidesToScroll = 1;
const container = $('.slider-container1');
const track = $('.slider-track1');
const item = $('.slider-item1');
const btnPrev = $('.btn-prev1');
const btnNext = $('.btn-next1');
const itemsCount = item.length;
const itemWidth = container.width() / slidesToShow;
const movePosition = slidesToScroll * itemWidth;
item.each(function (index, item) {
$(item).css({
minWidth: itemWidth,
});
});
btnNext.click(function(){
const itemsLeft = itemsCount - (Math.abs(position) + slidesToShow * itemWidth) / itemWidth;
position -= itemsLeft >= slidesToScroll ? movePosition : itemsLeft * itemWidth;
setPosition();
checkBtns();
});
btnPrev.click(function() {
const itemsLeft = Math.abs(position) / itemWidth;
position += itemsLeft >= slidesToScroll ? movePosition : itemsLeft * itemWidth;
setPosition();
checkBtns();
});
const setPosition = () => {
track.css({
transform: `translate(${position}px)`
});
};
const checkBtns = () => {
btnPrev.prop('disabled', position === 0);
btnNext.prop(
'disabled',
position <= -(itemsCount - slidesToShow) * itemWidth
);
};
checkBtns();
});