javascript - Show gif while clicking in a key -


hello want make gif appear while m clicking in ctrl key when stop want show image.

my code make gif appear when click ctrl if stop gif keeps.

code:

document.addeventlistener('keydown', function(event) {  if(event.keycode == 17) {  document.getelementbyid("key").innerhtml = "<img src=\"stick.gif\">";  shoot -= 1;  document.getelementbyid("shoot").innerhtml = shoot;   } } 

try this.i have made key-up event replaces gif when key not pressed.you can make react ctrl key well.i have added 2 id tags elements, since had not attached html elements.

as comment above mentioned, you need keyup function handle event when key not pressed.

<script>document.addeventlistener('keydown', function(event) { console.log("event"); if(event.keycode == 17) { console.log("key pressed"); document.getelementbyid("key").innerhtml = "<img src=\"stick.gif\">";  shoot -= 1;  document.getelementbyid("shoot").innerhtml = shoot;   }  }); document.addeventlistener('keyup', function(event) { document.getelementbyid("key").innerhtml = "this key up"  });  </script> <p id="key">hello</p> <p id="shoot">shot</p> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -