javascript javascript双击问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5489338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript double click problem
提问by Tom
I have a slideshow which has quick links to the specific slides. The slideshow then continues from that point. The trouble is if I double click on a quick link - when the slideshow continues it skips a slide (if I treble click it skips 2).
我有一个幻灯片,其中包含指向特定幻灯片的快速链接。然后幻灯片从该点继续。问题是如果我双击一个快速链接 - 当幻灯片继续播放时,它会跳过一张幻灯片(如果我三次单击它会跳过 2)。
I think it's actually a problem with clicking again while the functions are running that's causing the problem as you can achieve the same problem by quickly clicking on other links while the functions are running.
我认为这实际上是在功能运行时再次单击导致问题的一个问题,因为您可以通过在功能运行时快速单击其他链接来解决同样的问题。
Here's the code I have...
这是我的代码...
var images=new Array();
var locationToRevealCount=6;
var nextimage=2;
var t;
var doubleclick;
addIcons();
function addIcons() {
while (locationToRevealCount>0) {
$("#extraImageButtons").append('<img class="galleryButtons" src="http://www.steveszone.co.uk/images/button_sets/pink_square_button1n.png" alt="'+locationToRevealCount+'" />');
images[locationToRevealCount]='http://www.tombrennand.net/'+locationToRevealCount+'a.jpg';
locationToRevealCount--;
};
$('.homeLeadContent').prepend('<img class="backgroundImage" src="http://www.tombrennand.net/1a.jpg" />');
$("#extraImageButtons img.galleryButtons[alt*='1']").attr("src","http://www.steveszone.co.uk/images/button_sets/black_square_button1n.png");
runSlides();
}
function runSlides() {
clearTimeout(t);
t = setTimeout(doSlideshow,3000);
}
function doSlideshow() {
if($('.backgroundImage').length!=0)
$('.backgroundImage').fadeOut(500,function() {
$('.backgroundImage').remove();
slideshowFadeIn();
});
else
slideshowFadeIn();
}
function slideshowFadeIn() {
if(nextimage>=images.length)
nextimage=1;
$("#extraImageButtons img.galleryButtons").attr("src","http://www.steveszone.co.uk/images/button_sets/pink_square_button1n.png");
$("#extraImageButtons img.galleryButtons[alt*='"+nextimage+"']").attr("src","http://www.steveszone.co.uk/images/button_sets/black_square_button1n.png");
$('.homeLeadContent').prepend($('<img class="backgroundImage" src="'+images[nextimage]+'" style="display:none;">').fadeIn(500,function() {
nextimage++;
runSlides();
}));
}
$("#extraImageButtons img.galleryButtons").live('click', function() {
nextimage=$(this).attr("alt");
$("#extraImageButtons img.galleryButtons").attr("src","http://www.steveszone.co.uk/images/button_sets/pink_square_button1n.png");
$(this).attr("src","http://www.steveszone.co.uk/images/button_sets/black_square_button1n.png");
clearTimeout(t);
doSlideshow();
});
...
...
<div class="homeLeadContent" style="width:965px;"></div>
<div id="extraImageButtons"></div>
回答by Rudu
Double click still fires two single click events... well almost all the time, depending on the browser (see: Javascript Madness: Mouse Events). This is why most recommend implement either single click or double click events but not both.
双击仍然会触发两个单击事件......几乎所有时间,这取决于浏览器(请参阅:Javascript Madness:鼠标事件)。这就是为什么大多数人推荐实现单击或双击事件而不是两者。
If you want to detect double clicks at the same time you'll need to get into timing click events proximity to each other and preventing the second, reverting the first, and calling a double click event instead.
如果你想同时检测双击,你需要进入定时点击事件彼此接近并防止第二次,恢复第一个,并改为调用双击事件。
It sounds like you don't actually need double clicks though? Just the ability to -when clicking on a slide link- switch to that slide instead of forwarding to the next slide?
听起来您实际上并不需要双击?只是能够在单击幻灯片链接时切换到该幻灯片而不是转发到下一张幻灯片?
回答by Marcus Olsson
If you want to complete disable doubleclick:
如果要完成禁用双击:
jQuery('.dblClickDisabled').bind('dblclick',function(e){
e.preventDefault();
})
Where .dblClickDisabled
is the DOM-element that you want to disable double-click on.
.dblClickDisabled
您要禁用双击的 DOM 元素在哪里。
If you aren't going to use a double click event, just put a timer to prevent a double click. This answer might be what you are looking for: How can jQuery be used to handle timer in click, dblclick separation
如果您不打算使用双击事件,只需放置一个计时器来防止双击。这个答案可能就是您要找的:如何使用 jQuery 处理单击中的计时器,dblclick 分离