??? ??? ????? ?? ??? ????? ???? ?? ?? ??? ???? ??? "?? CSS" ??? ???? ???? ???. ?? ?? ??? ???? ?? ??? ??? ???? ?? ??? scrollIntoView()? ???? ????. ??? ? ??? ?? ?? ??? ??? ???? ??????. ?? ??? ????? ????? ?? ??? ???? ????? ??? ????? ????? ????. ?? Adam Argyle? ?? ?? ???? ?? ??? ?? ????? ?? ??? ??? ?????. ?? ???? ??? ??? ??????.
??? ?? ???? ??????.
https://codepen.io/krjo-the-decoder/pen/dyjPrLy
??? ?? ??? ???? ??? ????? ???? ????? ??? ?? ????? ??????.
????? y? ???? ?? ??? ???? ?? scrollIntoView() ??? scrollTo()? ??? ???? ??? ??? ?????.
<!-- Using tailwind.css --> <div class="py-48 bg-gray-200"> <h1 class="text-center font-bold mb-5 text-2xl">Scroll-snap Carousel</h1> <div class="grid grid-flow-col overflow-x-auto snap-x snap-mandatory overscroll-x-contain auto-cols-[100%] mx-auto w-[500px]"> <img src="https://via.placeholder.com/500" width="500" height="500" class="block snap-center w-auto h-auto" data-slide> <img src="https://via.placeholder.com/500" width="500" height="500" class="block snap-center w-auto h-auto" data-slide> <img src="https://via.placeholder.com/500" width="500" height="500" class="block snap-center w-auto h-auto" data-slide> <img src="https://via.placeholder.com/500" width="500" height="500" class="block snap-center w-auto h-auto" data-slide> </div> <nav class="flex justify-center mt-4"> <button class="p-2 border rounded-full border-solid border-black w-12 h-12 mx-2" type="button" data-index="0">1</button> <button class="p-2 border rounded-full border-solid border-black w-12 h-12 mx-2" type="button" data-index="1">2</button> <button class="p-2 border rounded-full border-solid border-black w-12 h-12 mx-2" type="button" data-index="2">3</button> <button class="p-2 border rounded-full border-solid border-black w-12 h-12 mx-2" type="button" data-index="3">4</button> </nav> </div> <script> const slides = document.querySelectorAll('[data-slide]'); document.querySelectorAll('button').forEach(button => { button.addEventListener('click', event => { console.log('event',event); slides[event.target.dataset.index].scrollIntoView({ behavior: 'smooth', inline: 'center', }); }); }); </script>
??? ?? scrollIntoView ??? block: 'nearest'
? ??? ???.