I create the snowflake using jquery. Everything seems ok but i want to do alert option when you click particular flake then it should alert the message. But i implemented the click function that alert option is continuously triggering. I am not sure where i did mistake. I tried the preventDefault still nothing happens. http://jsfiddle.net/vicky081/4cZdu/12/
function snowFalling(){
// move the snow
$('.snow').each(function(key, value){
// check if the snow has reached the bottom of the screen
if( parseInt($(this).css('top')) > windowHeight - 80 ) {
// remove the snow from the HTML DOM structure
$(this).remove();
}
// set up a random speed
var fallingSpeed = Math.floor(Math.random() * 5 + 1);
// set up a random direction for the snow to move
var movingDirection = Math.floor(Math.random()*2);
// get the snow's current top
var currentTop = parseInt($(this).css('top'));
// get the snow's current top
var currentLeft = parseInt($(this).css('left'));
// set the snow's new top
$(this).css('top', currentTop + fallingSpeed );
// check if the snow should move to left or move to right
if( movingDirection === 0){
// set the snow move to right
$(this).css('left', currentLeft + fallingSpeed );
}else {
// set the snow move to left
$(this).css('left', currentLeft + -(fallingSpeed) );
}
});
jQuery(this).click(function() {
alert('You Clicked');
});
// repeat the rollIt() function for each 200 microseconds
window.setTimeout(snowFalling, 200);
}
// call the function when the document is loaded completely
generateSnow();
snowFalling();
});
Now i want to alert particular click. How to prevent multi alert on click the snow flakes. Thanks.
If you only want the alert to occur when snow is clicked, move the handler to the point where the snowflake is created, not when it us updated, as you currently have it.
$('<div />')
.addClass('snow')
.css('top', snowTop)
.css('left', snowLeft)
.css('position','absolute')
.html('*')
.click(function() {
alert('You Clicked');
})
);
You're binding a click handler every 200 ms on each snowflake element.
It looks like generateSnow()
would be a good place to bind your click handler instead.
You could also use event delegation and bind on DOM ready:
$(document).on('click', '.snow', function() {
alert('You clicked');
});
Just move the click
handler to outside your snowFalling
function, after you generate your snowflakes:
JSFiddle
// this function is to alter the top of each snow, using the handy .each() jQuery method
function snowFalling(){
// move the snow
$('.snow').each(function(key, value){
// check if the snow has reached the bottom of the screen
if( parseInt($(this).css('top')) > windowHeight - 80 ) {
// remove the snow from the HTML DOM structure
$(this).remove();
}
// set up a random speed
var fallingSpeed = Math.floor(Math.random() * 5 + 1);
// set up a random direction for the snow to move
var movingDirection = Math.floor(Math.random()*2);
// get the snow's current top
var currentTop = parseInt($(this).css('top'));
// get the snow's current top
var currentLeft = parseInt($(this).css('left'));
// set the snow's new top
$(this).css('top', currentTop + fallingSpeed );
// check if the snow should move to left or move to right
if( movingDirection === 0){
// set the snow move to right
$(this).css('left', currentLeft + fallingSpeed );
}else {
// set the snow move to left
$(this).css('left', currentLeft + -(fallingSpeed) );
}
});
// repeat the rollIt() function for each 200 microseconds
window.setTimeout(snowFalling, 200);
}
// call the function when the document is loaded completely
generateSnow();
snowFalling();
$('.snow').click(function() {
alert('You Clicked');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With