过渡在第一时间不起作用

时间:2018-05-30 09:44:26

标签: javascript css css-transitions

我遇到过渡属性问题。即,它仅在第一次之后起作用。这是第一次没有过渡效应。

JS代码:

document.addEventListener("DOMContentLoaded", function() {

  var second = document.querySelector('.square');

  if(second) {
      second.addEventListener("mouseenter", function() {
          var maxX = Math.floor(Math.random() * (window.innerWidth - 200));
          var maxY = Math.floor(Math.random() * (window.innerHeight - 200));
          this.style.left = maxX + 'px';
          this.style.top = maxY + 'px';
          this.style.transition = 'left 2s, top 2s';
      });
  }

 });

CSS代码:

* {
margin: 0;
padding: 0;
}

main {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}

.second {
height: 200px;
width: 200px;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
}

编辑:

JSfiddle

更新:

将过渡转移到CSS不会改变任何东西。广场必须居中,不能离开窗户。

1 个答案:

答案 0 :(得分:0)

.innerHTMLtransitiontop移至css - 浏览器应知道要转换的内容如何转换之前你设置了新的X和Y。

left
document.addEventListener("DOMContentLoaded", function() {

  var second = document.querySelector('.square');

  if(second) {
      second.addEventListener("mouseenter", function() {
          var maxX = Math.floor(Math.random() * (window.innerWidth - 200));
          var maxY = Math.floor(Math.random() * (window.innerHeight - 200));
          this.style.left = maxX + 'px';
          this.style.top = maxY + 'px';
      });
  }

 });
.square {
  background:red;
  height: 200px;
  width: 200px;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: left 2s, top 2s;
  top: 0;
  left: 0;
}

编辑1

如果您想最初居中,请添加此

<div class="square">
</div> 

top: 50%;
left: 50%;
transform: translate(-50%, -50%);
document.addEventListener("DOMContentLoaded", function() {

  var second = document.querySelector('.square');

  if(second) {
      second.addEventListener("mouseenter", function() {
          var maxX = Math.floor(Math.random() * (window.innerWidth - 200));
          var maxY = Math.floor(Math.random() * (window.innerHeight - 200));
          this.style.left = maxX + 'px';
          this.style.top = maxY + 'px';
      });
  }

 });
.square {
  background:red;
  height: 200px;
  width: 200px;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: left 2s, top 2s;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

编辑2

考虑到你的小提琴 - 你应该修改你的<div class="square"> </div> maxX计算因为maxY属性将你的方块的中心从“左上角”移动到“中心”:

transform
document.addEventListener("DOMContentLoaded", function() {

      var second = document.querySelector('.square');

      if(second) {
          second.addEventListener("mouseenter", function() {
              var maxX = 100 + Math.floor(Math.random() * (window.innerWidth - 200));
              var maxY = 100 + Math.floor(Math.random() * (window.innerHeight - 200));
              this.style.left = maxX + 'px';
              this.style.top = maxY + 'px';
          });
      }

 });
* {
    margin: 0;
    padding: 0;
}

main {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

.square {
    height: 200px;
    width: 200px;
    position: absolute;
    -webkit-box-shadow: 0px 0px 70px 5px rgba(0,0,0,0.5);
    -moz-box-shadow: 0px 0px 70px 5px rgba(0,0,0,0.5);
    box-shadow: 0px 0px 70px 5px rgba(0,0,0,0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    transition: left 0.8s, top 0.8s;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%); 
}