gpt4 book ai didi

javascript - 在 javascript 中使用 requestAnimationFrame 时没有动画

转载 作者:行者123 更新时间:2023-11-30 00:19:06 26 4
gpt4 key购买 nike

我想旋转图像,但没有得到任何图像旋转。如何解决这个问题?

HTML 文件:

<html>
<head>
<title>javascript</title>
</head>
<body>

<p style="text-align: center" >
<img src="kalam.jpg" style="position: relative">
</p>

<script type="text/javascript" src="code.js">
</script>
</body>
</html>

代码.js:

var kalam = document.querySelector ("img") ;
var angle = 0 , lastTime = null ;
function animate ( time ) {
if ( lastTime != null )
angle += ( time - lastTime ) * 0.001;
lastTime = time ;
kalam.style.top = ( Math.sin ( angle ) * 20) + " px ";
kalam.style.left = ( Math.cos ( angle ) * 200) + " px ";
requestAnimationFrame ( animate ) ;
}
requestAnimationFrame ( animate );

最佳答案

唯一的问题是你的 css 说的是 "px" 而不是 "px" 因为你在声明 css 的度量单位之前不能有空格属性正在使用。

Demo on JSBin

var kalam = document.querySelector("img") ;
var angle = 0 , lastTime = null ;
function animate ( time ) {
if ( lastTime != null )
angle += ( time - lastTime ) * 0.001;
lastTime = time ;
kalam.style.top = ( Math.sin ( angle ) * 40) + "px ";
kalam.style.left = ( Math.cos ( angle ) * 90) + "px ";
requestAnimationFrame ( animate ) ;
}
requestAnimationFrame ( animate );
<img src="http://static.jsbin.com/images/dave.min.svg" style="position:absolute;margin:40px 0 0 100px;max-height:90px">

关于javascript - 在 javascript 中使用 requestAnimationFrame 时没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com