Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Выполнение действия пока держится клавиша (https://javascript.ru/forum/jquery/21712-vypolnenie-dejjstviya-poka-derzhitsya-klavisha.html)

StrSprut 21.09.2011 11:06

Выполнение действия пока держится клавиша
 
Как сделать что бы при нажатой и удерживаемой клавише выполнялось какое то действие( в моем случае сдвиг обьекта на 1 px) с интервалом по времени
$("#moveRigth").mouseup(function(){

flag = false;

}).mousedown(function(){

var flag = true;
do
{
setTimeout(function(){/*функция сдвига на 1px*/},100);
}
while (flag != false);

});

kadabrik 21.09.2011 11:56

<!DOCTYPE HTML>
<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    <script>
      $(function()
      {
        $('#container')
          .mousedown(function()
          {
             $('#conveyor').data('moveInterval',setInterval(function()
             {
               $('#conveyor').offset({left: ++$('#conveyor').offset().left })
             },50));
          })
          .mouseup(function()
          {
           clearInterval($('#conveyor').data('moveInterval'));
          });
      });
    </script>
    <style>
    
      #container{
        height: 50px;
        width: 100%;
        background-color: yellow;
       }
       
      #conveyor{
        position: relative;
        height: 100%;
        width: 50px;
        background-color: green;
      }
      
    </style>
  </head>
  <body>
  Click on yellow block:
  <div id="container">
    <div id="conveyor"></div>
  </div>
  </body>
</html>


Часовой пояс GMT +3, время: 04:03.