运行 ❯
获取你
自己的
网站
×
更改方向
保存代码
更改主题,暗色/亮色
前往 Spaces
<!DOCTYPE html> <html> <head> <style> #container { width: 80%; aspect-ratio: 2/1; margin: 30px auto; border: solid black 2px; overflow-x: hidden; overflow-y: scroll; scroll-snap-type: y mandatory; } .blue { background-color: lightblue; width: 90%; } .green { background-color: lightgreen; width: 80%; } .pink { background-color: lightpink; width: 70%; } #container > div{ margin: 5px; scroll-snap-align: center; scroll-snap-stop: normal; aspect-ratio: 4/1; } </style> </head> <body> <h1>Change scroll-snap-stop property with JavaScript</h1> <p>Try first to scroll past several elements with one fast swipe.</p> <p>Now, click button, click inside container, and see how scrolling behavior has changed. Scroll stops and snaps in place on the next element.</p> <button onclick="myFunction()">Try it</button> <div id="container"> <div class="blue"></div> <div class="green"></div> <div class="pink"></div> <div class="green"></div> <div class="blue"></div> <div class="pink"></div> <div class="blue"></div> <div class="green"></div> <div class="pink"></div> <div class="blue"></div> <div class="green"></div> <div class="pink"></div> <div class="blue"></div> <div class="green"></div> </div> <script> function myFunction() { let childDiv = document.querySelectorAll("#container>div"); for(let i=0; i<childDiv.length; i++){ childDiv[i].style.scrollSnapStop = "always"; } } </script> </body> </html>