Create yt-like scrollable chips without scrollbar, also with button
Sat Aug 19 2023 17:00:18 GMT+0000 (Coordinated Universal Time)
Saved by @sadik
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Smooth Horizontal Scroll with Buttons and Mouse Wheel</title> <style> /* Add your CSS styles here */ .scrollable-box { width: 300px; height: 200px; overflow: hidden; border: 1px solid #ddd; position: relative; } .content { white-space: nowrap; overflow-x: auto; padding: 10px; /* Hide the scrollbar */ scrollbar-width: none; /* Firefox */ -ms-overflow-style: none; /* Internet Explorer and Edge */ } .content::-webkit-scrollbar { display: none; /* Webkit-based browsers (Chrome, Safari) */ } .content p { display: inline-block; margin: 0; padding: 10px; } .scroll-button { position: absolute; top: 50%; transform: translateY(-50%); width: 30px; height: 30px; background-color: #333; color: #fff; text-align: center; line-height: 30px; cursor: pointer; } .scroll-left { left: 0; } .scroll-right { right: 0; } </style> </head> <body> <div class="scrollable-box"> <div class="content" id="scrollableContent"> <p>This</p> <p>is</p> <p>horizontal</p> <p>scrolling</p> <p>content.</p> </div> <div class="scroll-button scroll-left" id="scrollLeftButton"><</div> <div class="scroll-button scroll-right" id="scrollRightButton">></div> </div> <script> const contentElement = document.getElementById("scrollableContent"); const scrollLeftButton = document.getElementById("scrollLeftButton"); const scrollRightButton = document.getElementById("scrollRightButton"); contentElement.addEventListener("wheel", (event) => { event.preventDefault(); const currentScrollPosition = contentElement.scrollLeft; const newScrollPosition = currentScrollPosition + event.deltaY; contentElement.scroll({ left: newScrollPosition, behavior: 'smooth' }); }); scrollLeftButton.addEventListener("click", () => { contentElement.scroll({ left: contentElement.scrollLeft - 100, // Adjust the value as needed behavior: 'smooth' }); }); scrollRightButton.addEventListener("click", () => { contentElement.scroll({ left: contentElement.scrollLeft + 100, // Adjust the value as needed behavior: 'smooth' }); }); </script> </body> </html>
Comments