【问题标题】:CSS transition not working when moving element移动元素时CSS过渡不起作用
【发布时间】:2019-12-28 16:05:36
【问题描述】:

我有一个 CSS 元素/球,我在点击时移动到新坐标。

这可行,但是我正在应用的转换似乎没有生效。

球会跳到新位置。我希望它慢慢动画/过渡/移动到新坐标。

我做错了什么?

.ball {
    width: 30px;
    height: 30px;
    border-radius: 50%; 
    background-color: #FF5722;
    position: absolute;
    // d.style.transition = "all 1s ease-in";
    transition: all 3s ease-in-out;
    // -webkit-transition: all 1s ease-in-out;
    // -moz-transition: all 1s ease-in-out;
    // -o-transition: all 1s ease-in-out;
    // -ms-transition: all 1s ease-in-out;
  }

 handleClick = (e) => {
      console.log('ball clicked');
      var d = document.getElementById('ball');
      console.log('d', d);
      d.style.x =  12 + "px";
      d.style.top = 341 + "px";
      d.style.transition = "all 1s ease-in";
  }

谢谢

【问题讨论】:

  • 在改变属性之前,过渡应该存在于元素上......这意味着在处理程序中的这段代码甚至运行之前
  • @JaromandaX - 我确实将它添加到 css 类中,但这也不起作用..
  • 如果过渡是最后添加的,那么是的,它会跳转,因为其他值已经更改。通常虽然过渡被添加到 css 类中,所以它一直存在。
  • getElementById('ball') .... 样式表中需要#ball 而不是.ball
  • 顺便说一下,像你一样将转换从样式表中的 3 秒更改为 1 秒内联,转换可能需要 3 秒而不是 1 秒

标签: javascript css animation transition


【解决方案1】:

您必须为xtop 分配一个默认值,否则您将尝试从无到有。

附:看来您的 CSS 正在选择具有 CLASS ball 的元素,而不是 ID 为 ball 的元素。在 CSS 中使用 #ball 而不是 .ball。 (归功于 jaromanda-x)

window.onclick = (e) => {
      console.log('ball clicked');
      var d = document.getElementById('ball');
      console.log('d', d);
      d.style.x =  12 + "px";
      d.style.top = 341 + "px";
      d.style.transition = "all 1s ease-in";
  }
#ball {
    width: 30px;
    height: 30px;
    border-radius: 50%; 
    background-color: #FF5722;
    position: absolute;
    // d.style.transition = "all 1s ease-in";
    transition: all 3s ease-in-out;
    // -webkit-transition: all 1s ease-in-out;
    // -moz-transition: all 1s ease-in-out;
    // -o-transition: all 1s ease-in-out;
    // -ms-transition: all 1s ease-in-out;
    x:0; /* default value */
  top:0; /* default value */
  }
<div id="ball">

【讨论】:

  • 您没有提及 OP 的问题,即 OP 使用的是 .ball 而不是 #ball
【解决方案2】:

好像有几处需要改正;

  • 球由 CSS 中的 .ball 类设置样式,因为球元素是通过 id 访问的,这表明存在潜在问题。 ball 类是否应用于 id 为 ball 的元素?
  • 样式对象上的x 属性应替换为left 属性以确保球元素的水平移动
  • 确保在修改任何 CSS 属性之前将过渡分配给目标元素

以下是演示这些更正的示例:

const handleClick = (e) => {

  console.log('ball clicked');

  const ball = document.getElementById('ball');

  /* Setting random coordinates to demonstrate transition */
  ball.style.left = Number.parseInt(Math.random() * 200) + "px";
  ball.style.top = Number.parseInt(Math.random() * 200) + "px";
}

document.addEventListener("click", handleClick);
#field {
  position: relative;
  width: 100%;
  height: 100%;
}

/* Corrected to id selector with # rather than class selector 
   with . */
#ball {
  position: absolute;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #FF5722;
  position: absolute;

  /* Assigning transition behavior which is applied during 
     property changes */
  transition: all 3s ease-in-out;
}
<div id="field">
  <div id="ball"></div>
</div>

希望有帮助

【讨论】:

  • 太棒了 - 非常感谢你们!我没有意识到我需要设置默认的起始 x 和顶部坐标。所以这现在有效。谢谢!
猜你喜欢
  • 2018-08-29
  • 2013-04-06
  • 2016-09-22
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
  • 2012-09-27
相关资源
最近更新 更多