我正在试图弄清楚如何解决滚动时分配给元素的tapped类,但它生效太快了我需要在实际触摸时稍微延迟它而不是在滚动时触摸,这是我的代码这个怎么运作:
$('div, a, span').filter('[tappable][data-tappable-role]').bind('touchstart', function()
{
var self = $(this);
self.addClass(self.data('tappable-role'));
}).bind('touchend', function()
{
var self = $(this);
self.removeClass(self.data('tappable-role'));
}).bind('click', function()
{
var self = $(this),
goTo = self.data('goto');
if(typeof goTo !== 'undefined')
{
window.location = goTo;
}
});
Run Code Online (Sandbox Code Playgroud)
当滚动时,它会在我几乎没有碰到它时将类分配给元素,我想防止这种情况发生,除非它被正确触摸(没有点击).虽然我尝试使用setTimeout,但是因为它延迟但效果不好但它稍后仍将分配该类.
这是我用setTimeout做的方式:
var currentTapped;
$('div, a, span').filter('[tappable][data-tappable-role]').bind('touchstart', function()
{
clearTimeout(currentTapped);
var self = $(this);
var currentTapped = setTimeout(function()
{
self.addClass(self.data('tappable-role'));
}, 60);
}).bind('touchend', function()
{
clearTimeout(currentTapped);
var self = $(this);
self.removeClass(self.data('tappable-role'));
}).bind('click', function()
{
clearTimeout(currentTapped);
var self = $(this),
goTo = …Run Code Online (Sandbox Code Playgroud)