@@ -4,25 +4,85 @@ document.addEventListener('DOMContentLoaded', function() {
44 const scrollRightBtn = document . getElementById ( 'meetups-scroll-right' ) ;
55
66 if ( scrollLeftBtn && scrollRightBtn && meetupContainer ) {
7- scrollLeftBtn . addEventListener ( 'click' , ( ) => {
8- const containerWidth = meetupContainer . clientWidth ;
9- const scrollAmount = containerWidth * 1.0 ;
10- meetupContainer . scrollBy ( {
11- left : - scrollAmount ,
12- behavior : 'smooth'
13- } ) ;
7+ const leftmostElemIsShown = ( ) => {
8+ let { clientWidth, scrollLeft, scrollWidth } = meetupContainer
9+ scrollLeft = Math . abs ( scrollLeft )
10+ errorMargin = scrollWidth * 0.1
11+
12+ res = ( scrollLeft > scrollWidth - clientWidth - errorMargin )
13+ return res
14+ }
15+ const rightmostElemIsShown = ( ) => {
16+ let { scrollLeft, scrollWidth } = meetupContainer
17+ scrollLeft = Math . abs ( scrollLeft )
18+ errorMargin = scrollWidth * 0.1
19+
20+ res = ( scrollLeft < errorMargin )
21+ return res
22+ }
23+
24+ const deactivate = ( button ) => {
25+ button . classList . add ( 'opacity-0' ) ;
26+ button . classList . remove ( 'opacity-100' , 'hover:cursor-pointer' , 'hover:scale-125' ) ;
27+ button . disabled = true ;
28+ }
29+
30+ const activate = ( button ) => {
31+ button . classList . add ( 'opacity-100' , 'hover:cursor-pointer' , 'hover:scale-125' ) ;
32+ button . classList . remove ( 'opacity-0' ) ;
33+ button . disabled = false ;
34+ }
35+
36+ // Function to update button visibility
37+ const updateButtonVisibility = ( ) => {
38+ // Check if the container is scrolled to the left
39+ if ( leftmostElemIsShown ( ) ) {
40+ deactivate ( scrollLeftBtn )
41+ } else {
42+ activate ( scrollLeftBtn )
43+ }
44+
45+ // Check if the container is scrolled to the right
46+ if ( rightmostElemIsShown ( ) ) {
47+ deactivate ( scrollRightBtn )
48+ } else {
49+ activate ( scrollRightBtn )
50+ }
51+ } ;
52+
53+ // Initial check on page load
54+ updateButtonVisibility ( ) ;
55+
56+ // Scroll Left Button
57+ scrollLeftBtn . addEventListener ( 'click' , ( ) => {
58+ const containerWidth = meetupContainer . clientWidth ;
59+ meetupContainer . scrollBy ( {
60+ left : - containerWidth ,
61+ behavior : 'smooth'
1462 } ) ;
63+ } ) ;
1564
16- scrollRightBtn . addEventListener ( 'click' , ( ) => {
17- const containerWidth = meetupContainer . clientWidth ;
18- const scrollAmount = containerWidth * 1.0 ;
19- meetupContainer . scrollBy ( {
20- left : scrollAmount ,
21- behavior : 'smooth'
22- } ) ;
65+ // Scroll Right Button
66+ scrollRightBtn . addEventListener ( 'click' , ( ) => {
67+ const containerWidth = meetupContainer . clientWidth ;
68+ meetupContainer . scrollBy ( {
69+ left : containerWidth ,
70+ behavior : 'smooth'
2371 } ) ;
72+ } ) ;
73+
74+ // Update buttons after scrolling
75+ meetupContainer . addEventListener ( 'scroll' , ( ) => {
76+ // Throttle the updates to improve performance
77+ clearTimeout ( meetupContainer . updateTimer ) ;
78+ meetupContainer . updateTimer = setTimeout ( updateButtonVisibility , 100 ) ;
79+ } ) ;
80+
81+ // Also update on window resize in case the container size changes
82+ window . addEventListener ( 'resize' , updateButtonVisibility ) ;
83+
2484 } else {
25- console . error ( 'One or more required elements are missing from the DOM' ) ;
85+ console . error ( 'One or more required elements are missing from the DOM' ) ;
2686 }
2787} ) ;
2888
0 commit comments