为什么我能';不能通过JQuery移动图像吗?
我试着用JQuery移动图像,当我按下键盘上的一个键时,但它不起作用。谁能告诉我有什么问题吗 代码如下:为什么我能';不能通过JQuery移动图像吗?,jquery,html,Jquery,Html,我试着用JQuery移动图像,当我按下键盘上的一个键时,但它不起作用。谁能告诉我有什么问题吗 代码如下: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> $(document).ready(function(){ $(document).k
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).keydown(function(){
$(document).find("img").animate({
top:"+=100px",
});
});
});
</script>
</head>
<body>
<img src="player.jpg">
</body>
</html>
$(文档).ready(函数(){
$(文档).keydown(函数(){
$(文档).find(“img”).animate({
顶部:“+=100px”,
});
});
});
非常感谢
$(document).ready(function(){
$(document).keydown(function(){
$('img').css('margin-top', '+=100px');
});
});
希望这能奏效。这是你唯一应该改变的事情:
<img style="position: relative" src="player.jpg">
您可能需要更改css
<style>
img {
width:20%;
display:block;
position:absolute;
left:0;
right:0;
bottom:0;
margin:auto;
}
</style>
img{
宽度:20%;
显示:块;
位置:绝对位置;
左:0;
右:0;
底部:0;
保证金:自动;
}
尝试添加“位置:相对;”相对于图像的CSS。不是相对的,位置必须是absolute@luizcarlosfx位置不能是静态的,但可以是相对的,这也是有效的:)您的就绪处理程序在这里是无用的,您的代码应该是:$(document).keydown(函数(){$((“img”).animate({top:+=100px',});})代码>相对位置看起来更相关,停留在页面流中