运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 Spaces
<!DOCTYPE html> <html> <head> <style> #container { width: 80%; aspect-ratio: 2/1; margin: auto; border: solid black 2px; overflow-x: scroll; overflow-y: hidden; white-space: nowrap; scroll-snap-type: x mandatory; scroll-padding-right: 0; } .blue { background-color: lightblue; height: 95%; aspect-ratio: 3/2; } .green { background-color: lightgreen; height: 80%; aspect-ratio: 4/3; } .blue, .green { display: inline-block; margin: 2px; scroll-snap-align: end; } </style> </head> <body> <h1>Change scroll-padding-right property with JavaScript</h1> <p>Try first to scroll to see how the scroll snaps on end of elements on x-axis.</p> <p>Now, click button, click inside the container, and see how scrolling behavior has changed. The elements now snap on end of elements with a 20px distance from the container.</p> <button onclick="myFunction()">Try it</button> <div id="container"> <div class="blue"></div> <div class="green"></div> <div class="blue"></div> <div class="green"></div> <div class="blue"></div> <div class="green"></div> <div class="blue"></div> </div> <script> function myFunction() { let containerDiv = document.querySelector("#container"); containerDiv.style.scrollPaddingRight = "20px"; } </script> </body> </html>