jquery - Changing variable during setInterval with key -
in animation "raining" dots random colors, want able change colors pressing enter. keypress i'm using following code:
$(document).keypress(function(event){ var keycode = (event.keycode ? event.keycode : event.which); if(keycode == '13'){ // change variables colors } });
as see it, shouldn't wrong code, nothing happens when pressing enter. glad if me out here!
you can find code here: http://jsfiddle.net/gsbcz/
why binding event on each animation call ? [event.which] jquery's crossbrowser way detect mouse , key events.
this should help,
html:
<html> <head> <title>raining dots</title> <link href='http://fonts.googleapis.com/css?family=give+you+glory' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=gloria+hallelujah' rel='stylesheet' type='text/css'> <link rel="stylesheet" type="text/css" media="screen" href="css/style.css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="js/external.js" ></script> </head> <body id="bg"> </body> </html>
javascript:
var capsule = { init: function() { this.c1 = 256, this.c2 = 220, this.c3 = 225; capsule.calldots(); }, calldots: function() { window.setinterval(function() { capsule.animatedots(capsule.c1, capsule.c2, capsule.c3); }, 10); }, animatedots: function() { var thewidth = $(window).width(), theheight = $(window).height(), randomentry = math.ceil(math.random() * thewidth), prerandomdotsize = math.ceil(math.random() * 40), randomdotsize = prerandomdotsize + 50; var dotname = 'dot-' + randomentry, color1 = math.ceil(math.random() * capsule.c1), color2 = math.ceil(math.random() * capsule.c2), color3 = math.ceil(math.random() * capsule.c3), colors = 'rgb(' + color1 + ',' + color2 + ',' + color3 + ')'; $('<div />', { text: '.', id: dotname, }).appendto('body').addclass('thedots').css('left', randomentry).css('font-size', randomdotsize).css('color', colors).animate({ "top": "+=" + theheight }, 5000, function() {}); } }; $(document).ready(function() { capsule.init(); }).keypress(function(event) { if (event.which == '13') { capsule.c1 += 2, capsule.c2 += 4, capsule.c3 += 6; } });
css:
body { font: 14px / 1.5em 'gloria hallelujah', cursive; } .thedots{ position:absolute; top:-50px; left:0px; z-index:10; position:block; }
Comments
Post a Comment