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);       }   }); } 

check jsfiddle demo


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -