gpt4 book ai didi

javascript - 使用 jQuery 将其他对象移动到位后淡出对象

转载 作者:行者123 更新时间:2023-11-28 10:57:51 25 4
gpt4 key购买 nike

我刚刚开始了解 jQuery,我有了制作简单的 super 马里奥模仿的想法。我可以用箭头键移动马里奥,我想一旦马里奥移动到位我就可以淡出蘑菇,但这似乎不像我预期的那样有效,如果有人能给我指出正确的方向,我会非常快乐!

这是 HTML:

<!DOCTYPE html>
<html>
<head>
<title>Super Mario!</title>
<link rel='stylesheet' type='text/css' href='stylesheet.css'/>
<script type='text/javascript' src='script.js'></script>
</head>
<body>
<img class="mushroom" src="http://ih2.redbubble.net/image.6378228.5104/sticker,375x360.u2.png"/>
<img class="mario" src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg"/>
</body>
</html>

这是 CSS:

img {
position: relative;
left: 0;
top: 0;
}

.mushroom {
position: relative;
left: 300;
top: 200;
width: 48px;
height: 48px;
}

这是 jQuery:

$(document).ready(function() {
// Keydown function to move mario
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
// Left arrow key pressed
case 37:
$('.mario').animate({left: "-=10px"}, 'fast');
break;
// Up Arrow Pressed
case 38:
$('.mario').animate({top: "-=10px"}, 'fast');
break;
// Right Arrow Pressed
case 39:
$('.mario').animate({left: "+=10px"}, 'fast');
break;
// Down Array Pressed
case 40:
$('.mario').animate({top: "+=10px"}, 'fast');
break;
}
// On Mario's arrival remove Mushroom
$(document).on('keydown', '.mario', function(e){
var x = e.clientX - this.offsetLeft,
y = e.clientY - this.offsetTop;

if (x > 300 && x < 400 && y > 200 && y < 300) {
$('.mushroom').fadeOut('slow');
}
});
});
});

首先,我尝试使用 IF 语句检查 Mario 的 css 的位置 - 它不起作用 - 但我意识到,css 可能不会在 keydown 上重写。所以我卡住了:/

IF 语句的思路是这样的:

// On Mario's arrival remove Mashroom
$(document).on('keydown', '.mushroom', function() {
if($('.mario').css("left")=="300" && ("top")=="200") {
$('.mashroom').animate({left: "+=10px"}, 'fast');
}
});

感谢大家的帮助!

最佳答案

失踪

$('.mario').css("top")

if($('.mario').css("left")=="300" && $('.mario').css("top")=="200") {
// ^ ^

关于javascript - 使用 jQuery 将其他对象移动到位后淡出对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563565/

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