代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jQuery/jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(document).keydown(function () {
alert("hello");
$('div').animate({ left: '+=10px' }, 500);
});
});
</script>
<style type="text/css">
div
{
height: 50px; width: 50px; background-color: Red;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
我想在每次按下键时将我的 div 向右移动,但由于某种原因,这不起作用。我收到“你好”警报,因此动画语句中可能存在一些我无法弄清楚的小错误。
谁能告诉我为什么这不起作用以及正确的方法吗?
提前致谢。 :)
最佳答案
这是因为 left/top/right/bottom 属性仅适用于定位元素。
因此,您需要在 div 上使用 position:relative
/position:absolute
,或者对 margin-left
进行动画处理。 (取决于您想要的最终结果..)
9.3.2 Box offsets: '
top
', 'right
', 'bottom
', 'left
'An element is said to be positioned if its 'position' property has a value other than 'static'. Positioned elements generate positioned boxes, laid out according to four properties:
关于jQuery 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700477/