You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用keyframes无法使block元素移动的问题求助

解决@keyframes无法让元素移动的问题

你的代码里有个明显的拼写错误,导致动画不生效:

  • animation: block is infinite linear; 里的 is 是笔误,应该改成动画时长,比如 1s(代表1秒完成一次动画),浏览器无法识别is这个无效值,所以元素不会动。

修正后的完整代码:

#block{
    width: 20px;
    height: 20px;
    background-color: blue;
    position: relative;
    top: 130px;
    left: 480px;
    animation: block 1s infinite linear;
}

@keyframes block{
    0%{left:480px;}
    100%{left: -40px;}
}

其他部分的写法是正确的:设置position: relative让元素可以通过left属性移动,@keyframes的关键帧定义也没问题,修正后元素就能正常从左向右移动并循环了。

内容的提问来源于stack exchange,提问作者Cheems Bread

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 08:55:25