下面相当简单的代码不起作用。不知道为什么,JS函数可以平滑移动图像。帮助将是伟大的家伙,几乎尝试了一切。
代码准备复制粘贴到 php 脚本并测试它。提前谢谢了
<?php
echo "
<script type='text/javascript'>
var img = document.getElementById( 'test' );
function translate( elem, x, y ) {
var left = 120,
top = 120,
dx = left - x,
dy = top - y,
i = 1,
count = 20,
delay = 20;
function loop() {
if ( i >= count ) { return; }
i += 1;
elem.style.left = ( left - ( dx * i / count ) ).toFixed( 0 ) + 'px';
elem.style.top = ( top - ( dy * i / count ) ).toFixed( 0 ) + 'px';
setTimeout( loop, delay );
}
loop();
}
</script>
";
echo '
</head>
<body>
<img id="test" src="http://placekitten.com/100/100" style="position:absolute; left:120px; top:120px;">
<a href="#" onclick="translate(\'test\', 30 , 30)">Translate to (0, 200)</a>
</body>
';
?>