JavaScript / CSS - How can I start an animation, creating new Divs from cursor position?

蓝咒 提交于 2019-12-13 09:20:42

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/48972654/javascript-css-how-can-i-start-an-animation-creating-new-divs-from-cursor-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!