CSS 运动路径 onClick

use*_*568 7 html javascript css

我需要有关已上传到codepen 的CSS 和 javascript 代码的帮助。

我正在尝试使用 CSS 运动路径,我希望能够使用 JavaScript 来控制它,因此当您按下“前进按钮”时,它会运行到最后一个关键帧并且有效,但是当我按下“后退按钮”时没发生什么事。我会喜欢它移回第一个关键帧。

问题是我的代码是否在正确的轨道上?我需要改变什么?

function myForwardFunction() {
  document.getElementById("pathed").style.animationPlayState = "running";
}

function myBackFunction() {
  document.getElementById("pathed").style.animationDirection = "reverse";
}
Run Code Online (Sandbox Code Playgroud)
section {
  width: 244px;
  height: 200px;
  border: 2px dashed lightgrey;
}

body {
  display: flex;
  padding: 10px;
  flex-wrap: wrap;
  gap: 30px;
  margin: 0;
  height: 100vh;
  justify-content: center;
  align-items: center;
}

div {
  width: 50px;
  height: 50px;
  border: 1px solid hsl(343, 100%, 58%, .3);
  border-right: 5px solid hsl(343, 100%, 58%);
  background: hsla(343, 100%, 58%, .3) radial-gradient(circle, hsl(343, 100%, 58%, 1) 3px, hsl(343, 100%, 58%, 0) 3px);
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: monospace, sans-serif;
}

.pathed {
  offset-path: path('M0,0 C40,240 200,240 240,0');
  position: absolute;
  animation: distance 4000ms alternate ease-in-out;
  animation-play-state: paused;
  animation-fill-mode: both;
  animation-direction: normal;
}

@keyframes distance {
  from {
    offset-distance: 0%;
  }
  to {
    offset-distance: 100%;
  }
}

code {
  position: absolute;
  top: 100px;
  right: 10px;
  left: 10px;
  line-height: 1.5;
}

section {
  position: relative;
}

* {
  box-sizing: border-box;
}

svg {
  position: absolute;
  opacity: .333;
  stroke-width: 2px;
}
Run Code Online (Sandbox Code Playgroud)
<button onclick="myForwardFunction()">Forward</button>

<button onclick="myBackFunction()">Back</button>


<section>
  <svg viewBox="0 0 240 200"><path d="M0,0 C40,240 200,240 240,0" fill="none" stroke="lightgrey"/></svg>
  <div id="pathed" class="pathed"></div>
</section>
Run Code Online (Sandbox Code Playgroud)

G-C*_*Cyr 5

您也可以切换 aclass并使用 a transition

function myForwardFunction() {
  document.getElementById("pathed").classList.add('to100');
}
function myBackFunction() {
  document.getElementById("pathed").classList.toggle('to100');
}
Run Code Online (Sandbox Code Playgroud)
section {
  width: 244px;
  height: 200px;
  border: 2px dashed lightgrey;
}

body {
  display: flex;
  padding: 10px;
  flex-wrap: wrap;
  gap: 30px;
  margin: 0;
  height: 100vh;
  justify-content: center;
  align-items: center;
}

div {
  width: 50px;
  height: 50px;
  border: 1px solid hsl(343, 100%, 58%, .3);
  border-right: 5px solid hsl(343, 100%, 58%);
  background: hsla(343, 100%, 58%, .3) radial-gradient(circle, hsl(343, 100%, 58%, 1) 3px, hsl(343, 100%, 58%, 0) 3px);
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: monospace, sans-serif;
}

.pathed {
  offset-path: path('M0,0 C40,240 200,240 240,0');
  position: absolute;
  transition: 4s;
  offset-distance: 0%;
}

.pathed.to100 {
  offset-distance: 100%;
}

code {
  position: absolute;
  top: 100px;
  right: 10px;
  left: 10px;
  line-height: 1.5;
}

section {
  position: relative;
}

* {
  box-sizing: border-box;
}

svg {
  position: absolute;
  opacity: .333;
  stroke-width: 2px;
}
small {font-size:0.6em}
Run Code Online (Sandbox Code Playgroud)
<button onclick="myForwardFunction()">Forward</button>
<button onclick="myBackFunction()">Back<small>/toggle direction</small></button>


<section>
  <svg viewBox="0 0 240 200"><path d="M0,0 C40,240 200,240 240,0" fill="none" stroke="lightgrey"/></svg>
  <div id="pathed" class="pathed"></div>
</section>
Run Code Online (Sandbox Code Playgroud)

按照@wizzwizz4 评论的建议进行编辑,您可以使用 js 在按钮上设置 onclick 功能:

let btn = document.querySelector('#click');
let div = document.querySelector('#pathed');
btn.addEventListener("click", function() {
  this.classList.toggle('to100');
  div.classList.toggle('to100');
});
Run Code Online (Sandbox Code Playgroud)
section {
  width: 244px;
  height: 200px;
  border: 2px dashed lightgrey;
}

body {
  display: flex;
  padding: 10px;
  flex-wrap: wrap;
  gap: 30px;
  margin: 0;
  height: 100vh;
  justify-content: center;
  align-items: center;
}

div {
  width: 50px;
  height: 50px;
  border: 1px solid hsl(343, 100%, 58%, .3);
  border-right: 5px solid hsl(343, 100%, 58%);
  background: hsla(343, 100%, 58%, .3) radial-gradient(circle, hsl(343, 100%, 58%, 1) 3px, hsl(343, 100%, 58%, 0) 3px);
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: monospace, sans-serif;
}

.pathed {
  offset-path: path('M0,0 C40,240 200,240 240,0');
  position: absolute;
  transition: 4s;
  offset-distance: 0%;
}

.pathed.to100 {
  offset-distance: 100%;
}

#click:after {
  content: ' forwards';
}

#click.to100:after {
  content: ' backwards';
}

code {
  position: absolute;
  top: 100px;
  right: 10px;
  left: 10px;
  line-height: 1.5;
}

section {
  position: relative;
}

* {
  box-sizing: border-box;
}

svg {
  position: absolute;
  opacity: .333;
  stroke-width: 2px;
}

small {
  font-size: 0.6em
}
Run Code Online (Sandbox Code Playgroud)
<button id=click>Click to go</button>


<section>
  <svg viewBox="0 0 240 200"><path d="M0,0 C40,240 200,240 240,0" fill="none" stroke="lightgrey"/></svg>
  <div id="pathed" class="pathed"></div>
</section>
Run Code Online (Sandbox Code Playgroud)


Jan*_*nga 3

尝试这个

更新

CSS

.pathed {
  offset-path: path('M0,0 C40,240 200,240 240,0');
  position: absolute;
  animation-play-state: paused;
  animation-direction: normal;

}
@keyframes distance_1 {
  from {
    offset-distance: 0%;
  }
  to{  offset-distance: 100%;}
}

@keyframes distance_2 {
  from {
    offset-distance: 100%;
  }
  to{  offset-distance: 0%;}
}
Run Code Online (Sandbox Code Playgroud)

js

 function myForwardFunction() {
      document.getElementById("pathed").style.animation = "distance_1 4000ms alternate ease-in-out";
      document.getElementById("pathed").style.animationDirection = "running";
      document.getElementById("pathed").style.animationFillMode = "forwards";
    }

    function myBackFunction() {
      document.getElementById("pathed").style.animation = "distance_2 4000ms alternate ease-in-out";
      document.getElementById("pathed").style.animationDirection = "running";
      document.getElementById("pathed").style.animationFillMode = "backwards";
    }
Run Code Online (Sandbox Code Playgroud)

工作示例