5

我使用 jquery 创建雪花。一切似乎都很好,但是当您单击特定薄片时我想做警报选项,那么它应该会提醒消息。但是我实现了警报选项不断触发的点击功能。我不确定我在哪里做错了。我尝试了 preventDefault 仍然没有任何反应。 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();

});

现在我想提醒特定的点击。如何防止点击雪花时出现多重警报。谢谢。

4

3 回答 3

4

如果您只想在单击雪花时发出警报,请将处理程序移动到创建雪花的位置,而不是在我们更新时,就像您当前拥有的那样。

$('<div />')
    .addClass('snow')
    .css('top', snowTop)
    .css('left', snowLeft)
    .css('position','absolute')
    .html('*')
    .click(function() {
        alert('You Clicked');
    })
);

jsFiddle

于 2013-10-01T14:57:42.433 回答
1

您每 200 毫秒在每个雪花元素上绑定一个单击处理程序。

它看起来generateSnow()是一个绑定你的点击处理程序的好地方。

您还可以使用事件委托并在 DOM 上绑定:

$(document).on('click', '.snow', function() {
    alert('You clicked');
});
于 2013-10-01T14:57:34.517 回答
1

生成雪花后,只需将click处理程序移到函数之外:snowFalling

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');
});
于 2013-10-01T14:59:42.837 回答