jquery - Move div when you click and hold a button -
basically want player move when click , hold button using jquery's .mousedown function. problem is, player doesn't continously move left, moves once. want him keep moving left until mouseup.
here's code:
$('#arrowright').mousedown(function() { $('#player').animate({'left':'+=20px'}); });
any ideas?
try this:
var isdown = false; $arrowright = $("#arrowright"); $arrowleft = $("#arrowleft"); $arrowright.mousedown(function(){isdown = true;}); $arrowleft.mousedown(function(){isdown = true;}); $(document).mouseup(function(){ if(isdown){ isdown = false; } }); $arrowright.mousedown(function() {moveplayer('+=20');}); $arrowleft.mousedown(function() {moveplayer('-=20');}); function moveplayer(intmovement){ $( "#player" ).animate({ 'left': intmovement +'px' }, 100, function() { if (isdown){ moveplayer(intmovement); } }); }
Comments
Post a Comment