2016-08-24 76 views
0

我有一个全屏图像滑块,它没有自动播放功能,所以我必须编写自定义脚本来单击下一个按钮。鼠标单击或悬停时的清除间隔

var interval = setInterval(function() { 

document.querySelector('.fp-controlArrow.fp-next').click(); 


}, 7000); 

setTimeout(function() { clearInterval(interval); }, 44000); 

但现在,每当用户点击同一个类(.fp-controlArrow.fp-NEXT)的按钮,我很想给clearInterval。 JS能否以某种方式区分模拟点击和真实鼠标点击之间的区别?如果是这样,那么代码是什么?

如果没有,也许有可能清除悬停按钮与.fp-controlArrow.fp-next类间隔?

谢谢!

+0

我不明白“模拟点击和真正的鼠标点击”是什么意思?什么是模拟点击? –

回答

0

是的,您可以通过使用isTrusted事件侦听器中的事件对象的属性来区分用户生成的事件与代码生成的事件。

var elem = document.querySelector('.fp-controlArrow.fp-next'); 

elem.addEventListener("click", function(event) { 
    if(event.isTrusted) 
     clearInterval(interval); 

    }, false); 

https://developer.mozilla.org/en/docs/Web/API/Event/isTrusted

0

可以使用mousedown事件为真正的点击。

var el = document.querySelector('.fp-controlArrow.fp-next') 
el.addEventListener('mousedown', function(){ 
    clearInterval(interval); 
}); 

工作例如:https://jsfiddle.net/fov47eny/

您也可以使用isTrusted但对浏览器的支持有限。

if (e.isTrusted) { 
    /* The event is trusted. */ 
} else { 
    /* The event is not trusted. */ 
} 
+0

嗨,穆罕默德。感谢你的回答。所以在我的情况下,我应该用我的按钮类替换“targer”是吗?所以我的代码如下...我错在哪里? 'var interval = setInterval(function(){ document.querySelector('.fp-controlArrow.fp-next')。click();},7000); ('。fp-controlArrow.fp-next')。on('mousedown',function(){ clearInterval(interval);});' –

+0

@DmitryRa我更新了我的代码。你可以在这里看到这个例子https://jsfiddle.net/fov47eny/ –

+0

谢谢。我已经添加了你的代码,但是当我点击“下一步”按钮时id没有清除间隔。也许它与页面上代码的复杂性有关......它是http://rogercastillo.org/responsive3/home ..该网站还没有优化,等待大约7秒的页面加载。任何想法呢? –