JavaScript / CSS - How can I start an animation, c

2019-09-21 04:32发布

I'd need to create Divs from the position where I click on the page that move from left to right. Is there a way to change constantly the position where I can start the animation? instead of having a fixed left and a fixed right? I was using keyframes with CSS.

2条回答
ら.Afraid
2楼-- · 2019-09-21 04:40

PLease try this:

HTML

<div class="container">
  <div class="startleft">
    <div class="box">
    </div>
    <div class="box">
    </div>
</div>
</div>

CSS

.container {
  width:100%;
  height:100vh;
}
.box {
  margin: 5px;
  width:30px;
  height:30px;
  background-color:blue;
}

JS

var container = document.getElementsByClassName("container")[0];
container.onclick = function(e) {
  console.log("startleft");

  var offset = e.offsetX; 
  var startleft = document.getElementsByClassName("startleft")[0];
  startleft.setAttribute("style", "padding-left:"+ offset + "px");
};

codepen: https://codepen.io/YasirKamdar/pen/aqaMpj

This will set the offset to start, You can use animation as you want using the offset value received

查看更多
来,给爷笑一个
3楼-- · 2019-09-21 04:44

html code

<div class="move-me move-me-1">steps(4, end)</div>
<br>
<div class="move-me move-me-2">steps(4, start)</div>
<br>
<div class="move-me move-me-3">no steps</div>

scss code :

.move-me {
  display: inline-block;
  padding: 20px;
  color: white;
  position: relative;
  margin: 0 0 10px 0;
}
.move-me-1 {
  animation: move-in-steps 8s steps(4) infinite;
}
.move-me-2 {
  animation: move-in-steps 8s steps(4, start) infinite;
}
.move-me-3 {
  animation: move-in-steps 8s infinite;
}

body {
  padding: 20px;
}

@keyframes move-in-steps {
  0% {
    left: 0;
    background: blue;
  }
  100% {
    left: 100%;
    background: red;
  }
}

and this link help This link try it

Thanks

查看更多
登录 后发表回答