Code:
<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>
I want to just move my div to the right on every key down, but for some reason, this won’t work. I’m getting the “hello” alert so there is probably some small mistake in the animate statement that I just can’t figure out.
Can anyone please tell me why this is not working and the correct way to do it?
Thanks in advance. 🙂
That is because left/top/right/bottom properties only apply to positioned elements.
So you either need to use
position:relative/position:absoluteon the div, or animate themargin-leftinstead. (depends on the end result you want..)Quote from the specs