运行 ❯
获取您
自己
的网站
×
更改方向
保存代码
更改主题,暗/亮
前往 Spaces
<!DOCTYPE html> <html> <head> <style> #frameDiv { width: 300px; height: 300px; margin: 20px; position: relative; border: solid black 1px; overflow: hidden; background-color: rgb(205, 242, 205); } svg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } path { opacity: 0.3; } img { position: absolute; z-index: 1; width: 70px; offset-path: path('M 50,150 C 50,275 250,275 250,150 M 250,150 C 250,25 50,25 50,150'); animation: moveDiv linear 7s infinite; } @keyframes moveDiv { 100% { offset-distance: 100%; } } </style> </head> <body> <h1>Change the offset property value with JavaScript</h1> <p>Initially, the fish follows a circular path and is rotated in the direction it is moving.</p> <p>Click the "Try it" button to change the offset property of the fish element so that:</p> <ol> <li>the path changes (offset-path)</li> <li>the fish starts the animation at a 200 pixels distance from the start of the path (offset-distance)</li> <li>the fish is rotated to be perpendicular (90 degrees) to the direction it is moving (offset-rotate)</li> </ol> <button onclick="myFunction()">Try it</button> <div id="frameDiv"> <img src="img_fish.svg" alt="fish"> <svg fill="none" stroke="gray" stroke-width="2" stroke-dasharray="5,5"> <path id="path1" d="M 50,150 C 50,275 250,275 250,150 M 250,150 C 250,25 50,25 50,150" /> <path id="path2" d="M 50,250 C 700,-50 -400,-50 250,250" /> </svg> </div> <script> function myFunction() { document.querySelector("img").style.offset = "path('M 50,250 C 700,-50 -400,-50 250,250') 200px auto 90deg"; document.querySelector("#path2").style.opacity = "1"; } </script> </body> </html>