Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create snowflakes in jquery

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.

like image 463
Vignesh Pichamani Avatar asked Oct 01 '13 14:10

Vignesh Pichamani


3 Answers

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

jsFiddle

like image 73
Daniel Gimenez Avatar answered Nov 03 '22 19:11

Daniel Gimenez


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');
});
like image 30
Jason P Avatar answered Nov 03 '22 19:11

Jason P


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');
});
like image 1
Bucket Avatar answered Nov 03 '22 20:11

Bucket