2015-09-25 136 views
2

我有两个按钮,它们使用Javascript来调用它们的控制旋转木马和其他一些动作的函数。Javascript按钮,防止鼠标双击

我只想禁用用户双击按钮的能力。

这里是我的代码:

var onRightArrow = function(e) { 
    //alert("Right"); 
    if (unitCtr<=unitTotal) { 
     unitCtr++; 
     TweenLite.to(productTxt, 0.2, {y: "-="+unitHeight }); 
     TweenLite.to(productImg, 0.2, {y: "-="+unitHeight }); 
    } 
    hideArrows(); 
}, onLeftArrow = function(e) { 
    //alert("Left"); 
    if (unitCtr>1) { 
     unitCtr--; 
     TweenLite.to(productTxt, 0.2, {y: "+="+unitHeight }); 
     TweenLite.to(productImg, 0.2, {y: "+="+unitHeight }); 
    } 
    hideArrows(); 
} 

arrowRight.addEventListener('click', onRightArrow, false); 
arrowLeft.addEventListener('click', onLeftArrow, false); 

我知道的代码DBLCLICK行,但不完全知道如何应用到禁止从鼠标的双击动作。

当用户现在doubleclicks,它misplaces在这就是为什么我要删除的DBLCLICK的影响按钮的功能转盘元素的定位。

在此先感谢您的任何建议。请避免在JQuery中提供答案。

更多代码: http://codepen.io/anon/pen/QjGydw

+0

只覆盖双击事件并使其无效? – Steve

+0

@Steve或'返回false'。 – divy3993

+1

@ divy3993我认为返回false只适用于jQuery,纠正我,如果我错了 – Steve

回答

0

我这解决了我自己,而答案是比较复杂的,是任何人都正在寻找应用听众禁用双击的能力,它在这里:

(arrowRight和arrowLeft是由ID定义的变量)

arrowRight.addEventListener('dblclick', function(e){ 
    e.preventDefault(); 
    }); 
    arrowLeft.addEventListener('dblclick', function(e){ 
    e.preventDefault(); 
    }); 

我还创建了在动画发生时禁用箭头以防止出现错误的函数。动画完成后重新启用。函数如下所示:

function disableArrows() { //ADDED NEW FUNCTION TO DISABLE ARROWS 
    arrowRight.removeEventListener('click', onTopArrow, false); 
    arrowLeft.removeEventListener('click', onBottomArrow, false); 
} 

function enableArrows() { //ADDED NEW FUNCTION TO RE-ENABLE ARROWS 
    arrowRight.addEventListener('click', onTopArrow, false); 
    arrowLeft.addEventListener('click', onBottomArrow, false); 
}