【问题标题】:Transition doesn't work at first time过渡在第一次不起作用
【发布时间】:2022-02-08 16:24:13
【问题描述】:

我的转换属性有问题。即,它仅在第一次之后才起作用。第一次没有过渡效果。

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 不会改变任何东西。 Square必须居中,不能离开窗口。

【问题讨论】:

  • 你能提供一个jsfiddle吗?这样就很容易弄明白了
  • 我猜是因为你是用 JS 添加过渡,把它移到 CSS
  • 我添加了 jsfiddle。我把它移到 CSS 上,效果是一样的。

标签: javascript css css-transitions


【解决方案1】:

transitiontopleft 移动到 css - 在设置新的 X 和 Y 之前,浏览器应该知道如何过渡到什么.

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;
}
<div class="square">
</div> 

编辑 1

如果您想最初将您的正方形居中 - 添加此

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%);
}
<div class="square"> 
</div> 

编辑 2

考虑到您的小提琴 - 您应该修改您的 maxXmaxY 计算,因为 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%); 
}
<main>
    <div class="square">
        <p>catch me!</p>
    </div>
</main>

【讨论】:

  • 没关系,但是正方形不居中,当顶部:0 和左侧:0 时。我需要让这个正方形居中。
  • @Ben 您可以在我编辑的答案中看到如何最初将您的正方形居中
  • 是的,我现在很清楚,但这样一来,广场就跑出了窗外。
  • @Ben 编辑答案
猜你喜欢
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多