我有一个小提琴: http://jsfiddle.net/maniator/SZpkF/1/
绿色框应该与箭头键一起移动。有人可以解释为什么它不起作用吗?
JS:
var game = {
playArea: null,
player: null,
init: function() {
this.playArea = $('<div>', {
style: 'background-color: #AAF;'
});
this.player = $('<span>');
this.player.css({
width: 20,
height: 20,
'background-color': '#AFA',
display: 'block',
position: 'absolute',
top: 0,
left: 0
})
$(window).resize(game.resize);
$('body').append(this.playArea);
this.resize();
this.playArea.append(this.player);
$(document).keydown(function(event) {
game.keydown(event)
})
return this;
},
resize: function() {
//console.log('resize', game);
game.playArea.css({
width: $(window).width() - 50,
height: $(window).height() - 50,
});
return this;
},
keydown: function(event) {
var direction;
switch (event.keyCode) {
case 38:
direction = {
'top': '-= 15'
};
break;
case 40:
direction = {
'top': '+= 15'
};
break;
case 37:
direction = {
'left': '+= 15'
};
break;
case 39:
direction = {
'left': '-= 15'
};
break;
}
console.log(direction, event, game.player);
game.player.animate(direction, 'slow');
}
};
$(function() {
game.init();
})
答案 0 :(得分:3)
在-=
和+=
之后,jQuery看起来不像空格。我删除了那些并且它有效,尽管方向相反。