So, I have this code:
$(document).ready(function() { $(document).keypress(function(e) { switch(e.which) { // user presses the "a" case 97: $('#character').css('left','+=40'); } } }
The problem is that I can only press "a" once, and #character only moves once ...
I also have jQuery draggable ( http://jqueryui.com/demos/draggable/ ) enabled with a limited environment around it.
Why can I only move the div once by pressing a key?
, jQuery "+ = 40" .css(). , , , "" "+ = 40", . .
.css()
, , jQuery, , , .css() , .animate() , .
.animate()
.animate() :
case 97: $('#character').animate({'left': '+=40'}, 1);
.css(), .
:
// The 'left' parameter in the function references the current position. case 97:$('#character').css('left',function(i,left){ return parseInt(left) + 40; });
: @Gaby, .replace('px','') , .parseInt() .
.replace('px','')
.parseInt()
I think you will need to get the character’s position, add 40 to the left, and then apply a css change, for example
var pos = $("#character").offset(); $("#character").animate({"left": pos.left + 40}, 100);
If you save the position of the symbol in another place, you can also simply update this value and update its position in the game loop.
I am surprised that he does anything at all!
your missing pair); and I also changed it a bit. it works!
$(document).ready(function () { $(document).keypress(function (e) { switch (e.which) { // user presses the "a" case 97: var left = parseInt($('#character').css('left')) + 40; $('#character').css('left', left + 'px'); } }); });
Source: https://habr.com/ru/post/1754809/More articles:Should I use 301 to redirect in place? - redirectHow to create an object design in a specific place in memory? - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1754806/converting-relative-paths-to-absolute-paths-c&usg=ALkJrhiIpV_WdjNmOC5dF49D0ESTkH_RMQКак бороться с истекающим предметом (из-за TTL) в memcached на веб-сайте высокой загрузки? - cachingJavascript operator overload? (def.js) - javascriptThe grep command works in testdir, but not in the "real" directory - linuxhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1754811/can-we-use-constants-in-place-of-property-name-in-assigning-values-to-class-properties&usg=ALkJrhjhXAo6O3sxJmiq4oPkpN9zD0fk7Amethod declared in struct in C ++ (STL) - c ++How to convert bytes to string to integers? python - pythonAjax readystate 3 (Chrome / IE) - javascriptAll Articles