javascript - 在 jquery 中创建雪花

标签 javascript jquery

我使用 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();

});

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

最佳答案

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

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

jsFiddle

关于javascript - 在 jquery 中创建雪花,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19119420/

相关文章:

javascript - 如何将实际对象(即对象的引用)传递给指令?

javascript - 根据我网站的语言选择图像

javascript - 单击滚动可滚动 div 内的元素不起作用

javascript - jquery 点击播放视频

javascript - 我有十进制数,需要转换为二进制数并有一个复选框链接到该二进制数的每一位

jquery - 成功后ajax调用函数

javascript - 我想从一个对象中提取前 5 个值的名称......在 React 中

javascript - 执行顺序 - 新对象的函数未定义

用于表中除第一列之外的所有列的 jQuery 选择器

jquery - 设置预输入的选定值