我使用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();
});
现在我想提醒特定的点击。点击雪花片如何防止多重警报。 感谢。
答案 0 :(得分:4)
如果您只想在单击雪时发出警报,请将处理程序移动到创建雪花的位置,而不是像我们当前更新时那样。
$('<div />')
.addClass('snow')
.css('top', snowTop)
.css('left', snowLeft)
.css('position','absolute')
.html('*')
.click(function() {
alert('You Clicked');
})
);
答案 1 :(得分:1)
你在每个雪花元素上每200毫秒绑定一个点击处理程序。
看起来generateSnow()
是绑定点击处理程序的好地方。
你也可以在DOM ready上使用事件委托和绑定:
$(document).on('click', '.snow', function() {
alert('You clicked');
});
答案 2 :(得分:1)
生成雪花后,只需将click
处理程序移到snowFalling
函数之外:
// 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');
});