1
1
const setCarousel = ( scroller ) => {
2
- const mainSection = document . querySelector ( '.body-content__main-section' ) ;
3
- const items = scroller . querySelectorAll ( '[data-scroll="item"]' ) ;
4
- const mobileStep = screen . width <= 768 ? scroller . children [ 0 ] . offsetWidth / 2 : 0 ;
2
+ if ( scroller . children . length <= 4 ) scroller . innerHTML += scroller . innerHTML ;
3
+
4
+ const items = scroller . querySelectorAll ( '.animate-visibility' ) ;
5
+ const mobileStep = screen . width <= 768 ? scroller . children [ 0 ] . offsetWidth / 2 : 0 ;
5
6
6
7
let clones = [ ] ;
7
8
let disableScroll = false ;
8
9
let scrollWidth = 0 ;
9
10
let scrollPos = 0 ;
10
11
let clonesWidth = 0 ;
11
- let isScrolling = false ;
12
12
let isDragging = false ;
13
13
14
14
scroller . innerHTML += scroller . innerHTML ;
@@ -24,10 +24,9 @@ const setCarousel = (scroller) => {
24
24
25
25
clones = scroller . querySelectorAll ( '.js-clone' ) ;
26
26
27
- const getClonesWidth = ( ) => {
27
+ const getClonesWidth = ( ) => {
28
28
clonesWidth = 0 ;
29
29
clones . forEach ( clone => { clonesWidth += clone . offsetWidth ; } ) ;
30
-
31
30
return clonesWidth ;
32
31
}
33
32
@@ -46,12 +45,12 @@ const setCarousel = (scroller) => {
46
45
if ( ! disableScroll ) {
47
46
scrollPos = Math . round ( getScrollPos ( ) / 100 ) * 100 ;
48
47
if ( clonesWidth + scrollPos >= scrollWidth ) {
49
- setScrollPos ( clonesWidth + mobileStep ) ;
48
+ setScrollPos ( clonesWidth ) ;
50
49
disableScroll = true ;
51
- } else if ( scrollPos == 0 ) {
50
+ } else if ( scrollPos == 0 ) {
52
51
setScrollPos ( scrollWidth - clonesWidth * 2 ) ;
53
52
disableScroll = true ;
54
- } else if ( scrollPos <= mobileStep ) {
53
+ } else if ( scrollPos <= mobileStep ) {
55
54
setScrollPos ( scrollWidth - clonesWidth * 2 + mobileStep ) ;
56
55
disableScroll = true ;
57
56
}
@@ -63,111 +62,109 @@ const setCarousel = (scroller) => {
63
62
}
64
63
}
65
64
66
- const easeInOutQuad = ( t , b , c , d ) => {
67
- t /= d / 2 ;
68
- if ( t < 1 ) return c / 2 * t * t + b ;
69
- t -- ;
70
- return - c / 2 * ( t * ( t - 2 ) - 1 ) + b ;
71
- }
72
-
73
- const smoothScroll = ( element , to ) => {
74
- if ( isScrolling ) return ;
75
- isScrolling = true ;
76
-
77
- const start = element . scrollLeft ;
78
- const change = to - start ;
79
- const duration = 500 ;
80
- let currentTime = 0 ;
81
-
82
- const animateScroll = ( ) => {
83
- currentTime += 20 ;
84
- const val = easeInOutQuad ( currentTime , start , change , duration ) ;
85
- element . scrollLeft = val ;
86
-
87
- if ( currentTime < duration ) {
88
- requestAnimationFrame ( animateScroll ) ;
89
- } else {
90
- isScrolling = false ;
91
- requestAnimationFrame ( scrollUpdate ) ;
92
- }
93
- }
94
-
95
- animateScroll ( ) ;
96
- }
97
-
98
- const handleWheelEvent = ( e ) => {
99
- e . preventDefault ( ) ;
100
- const direction = e . shiftKey ? Math . sign ( e . deltaY ) : Math . sign ( e . deltaX ) ;
101
-
102
- if ( Math . abs ( e . deltaX ) > Math . abs ( e . deltaY ) || e . shiftKey ) {
103
- const delta = direction * scroller . children [ 0 ] . offsetWidth ;
104
- smoothScroll ( scroller , scroller . scrollLeft + delta ) ;
105
- } else {
106
- const mainBlock = screen . width <= 768 ? mainSection : window ;
107
- mainBlock . scrollBy ( 0 , e . deltaY ) ;
108
- }
109
- }
110
-
111
- scroller . addEventListener ( "click" , ( event ) => {
65
+ scroller . addEventListener ( 'click' , ( event ) => {
112
66
const rect = event . currentTarget . getBoundingClientRect ( ) ;
113
67
const clickX = event . clientX - rect . left ;
114
68
const middle = rect . width / 2 ;
115
69
116
70
if ( clickX > middle ) {
117
- smoothScroll ( scroller , scroller . scrollLeft + scroller . children [ 1 ] . offsetWidth ) ;
71
+ scroller . scrollTo ( {
72
+ left : scroller . scrollLeft + scroller . children [ 1 ] . offsetWidth ,
73
+ behavior : 'smooth'
74
+ } ) ;
118
75
} else {
119
- smoothScroll ( scroller , scroller . scrollLeft - scroller . children [ 1 ] . offsetWidth ) ;
76
+ scroller . scrollTo ( {
77
+ left : scroller . scrollLeft - scroller . children [ 1 ] . offsetWidth ,
78
+ behavior : 'smooth'
79
+ } ) ;
120
80
}
121
81
isDragging = false ;
122
82
} ) ;
123
83
124
- let startX , deltaX , currentX , delta ;
84
+ let isDesktopScrolling ;
85
+ scroller . addEventListener ( 'scroll' , ( event ) => {
86
+ const target = event . target ;
87
+ const offsetWidth = target . offsetWidth ;
88
+
89
+ const checkPos = ( ) => {
90
+ [ ...target . children ] . map ( e => {
91
+ const toCenter = Math . abs ( window . outerWidth / 2 - e . getBoundingClientRect ( ) . left - e . getBoundingClientRect ( ) . width / 2 ) ;
92
+ const toCenter2 = window . outerWidth / 2 - e . getBoundingClientRect ( ) . left - e . getBoundingClientRect ( ) . width / 2 ;
93
+ const viewport = toCenter / offsetWidth * 100 ;
94
+ const viewport2 = toCenter2 / offsetWidth * 100 ;
95
+ e . style . setProperty ( '--viewport' , viewport ) ;
96
+ e . style . setProperty ( '--viewport2' , viewport2 ) ;
97
+ } ) ;
98
+ } ;
99
+ checkPos ( ) ;
100
+ scrollUpdate ( ) ;
101
+
102
+ if ( screen . width >= 768 ) {
103
+ clearTimeout ( isDesktopScrolling ) ;
104
+ isDesktopScrolling = setTimeout ( ( ) => {
105
+ onScrollStop ( ) ;
106
+ } , 100 ) ;
107
+
108
+ function onScrollStop ( ) {
109
+ scroller . scrollTo ( {
110
+ left : scroller . children [ 0 ] . offsetWidth * Math . round ( scroller . scrollLeft / scroller . children [ 0 ] . offsetWidth ) ,
111
+ behavior : 'smooth'
112
+ } ) ;
113
+ } ;
114
+ }
115
+ } ) ;
116
+
117
+ let startX , deltaX , currentX , halfOfCard , startY , deltaY , currentY , isHorizontalScroll ;
125
118
126
119
const onTouchStart = ( event ) => {
127
120
startX = event . touches [ 0 ] . pageX ;
121
+ startY = event . touches [ 0 ] . pageY ;
128
122
deltaX = 0 ;
129
123
currentX = 0 ;
130
- delta = 0 ;
124
+ currentY = 0 ;
125
+ halfOfCard = 0 ;
126
+ deltaY = 0 ;
127
+ isHorizontalScroll = undefined ;
131
128
}
132
129
133
130
const onTouchMove = ( event ) => {
131
+ if ( event . touches . length > 1 ) return ;
132
+
134
133
isDragging = true ;
135
134
currentX = event . touches [ 0 ] . pageX ;
135
+ currentY = event . touches [ 0 ] . pageY ;
136
136
deltaX = currentX - startX ;
137
- delta = currentX - startX > 0 ? - 1 : 1 ;
138
- scroller . scrollLeft -= deltaX ;
137
+ deltaY = currentY - startY ;
138
+ const scrollDelta = currentX - startX > 0 ? - 1 : 1 ;
139
+ halfOfCard = screen . width <= 768 ? ( scroller . children [ 0 ] . offsetWidth / 2 ) * scrollDelta : 0 ;
140
+ if ( typeof isHorizontalScroll === 'undefined' ) isHorizontalScroll = Math . abs ( deltaY ) > Math . abs ( deltaX ) ;
141
+
142
+ if ( ! isHorizontalScroll ) {
143
+ event . preventDefault ( ) ;
144
+ scroller . scrollLeft -= deltaX ;
145
+ }
146
+
139
147
startX = currentX ;
148
+ startY = currentY ;
140
149
}
141
150
142
151
const onTouchEnd = ( ) => {
143
- if ( isDragging ) {
144
- const scrollToPos = scroller . children [ 0 ] . offsetWidth * Math . round ( scroller . scrollLeft / scroller . children [ 0 ] . offsetWidth ) + mobileStep * delta ;
145
- smoothScroll ( scroller , scrollToPos ) ;
146
- isDragging = false ;
152
+ if ( ! isHorizontalScroll && isDragging ) {
153
+ setTimeout ( ( ) => {
154
+ scroller . scrollTo ( {
155
+ left : scroller . children [ 0 ] . offsetWidth * Math . round ( scroller . scrollLeft / scroller . children [ 0 ] . offsetWidth ) + halfOfCard ,
156
+ behavior : 'smooth'
157
+ } ) ;
158
+ } , 0 ) ;
147
159
}
160
+ isDragging = false ;
148
161
}
149
162
150
- scroller . addEventListener ( 'wheel' , handleWheelEvent , { passive : false } ) ;
151
163
scroller . addEventListener ( 'touchstart' , onTouchStart , { passive : false } ) ;
152
164
scroller . addEventListener ( 'touchmove' , onTouchMove , { passive : false } ) ;
153
165
scroller . addEventListener ( 'touchend' , onTouchEnd , { passive : false } ) ;
154
166
}
155
167
156
- function loop ( { target, target : { offsetWidth } } ) {
157
- const checkPos = ( ) => {
158
- [ ...target . children ] . map ( e => {
159
- const toCenter = Math . abs ( window . outerWidth / 2 - e . getBoundingClientRect ( ) . left - e . getBoundingClientRect ( ) . width / 2 )
160
- const toCenter2 = window . outerWidth / 2 - e . getBoundingClientRect ( ) . left - e . getBoundingClientRect ( ) . width / 2
161
- const viewport = toCenter / offsetWidth * 100
162
- const viewport2 = toCenter2 / offsetWidth * 100
163
- e . style . setProperty ( '--viewport' , viewport )
164
- e . style . setProperty ( '--viewport2' , viewport2 )
165
- } )
166
- }
167
- requestAnimationFrame ( checkPos ) ;
168
- }
169
- window . loop = loop ;
170
-
171
168
document . addEventListener ( "DOMContentLoaded" , ( ) => {
172
169
const carousels = document . querySelectorAll ( '[data-scroll="scroller"]' ) ;
173
170
carousels . forEach ( ( element ) => setCarousel ( element ) ) ;
0 commit comments