禁止jQuery在触控设备上运行鼠标输入

时间:2021-03-31 07:11:46

I have written some fancy hover scrolling effects in jQuery. They work great on a desktop computer. My problem is that on a mobile device, because the user taps on the screen, my code still believes the user is hovering on my .scrollright div and keeps scrolling.

我用jQuery编写了一些悬停滚动效果。它们在台式电脑上工作得很好。我的问题是,在移动设备上,由于用户点击屏幕,我的代码仍然认为用户在我的.scrollright div上徘徊,并继续滚动。

How can I disable this, or otherwise just prevent this problem, on mobile/tablet devices?

如何在移动/平板设备上禁用这个功能,或者以其他方式防止这个问题?

$('.thumbnails .scrollright').on('mouseenter', function() {
    this.iid = setInterval(function() {
        var CurrentScrollLeft = $( ".thumbnails .thumbnailphotos" ).scrollLeft();
        $( ".thumbnails .thumbnailphotos" ).scrollLeft( CurrentScrollLeft+10 );         
    }, 50);
    }).on('mouseleave', function(){
        this.iid && clearInterval(this.iid);
    });

1 个解决方案

#1


8  

A quick check for touch maybe?

快速检查一下触觉?

var tap = ("ontouchstart" in document.documentElement);

Then wrap your code in the condition:

然后在以下条件下包装代码:

if(!tap){
    $('.thumbnails .scrollright').on('mouseenter', function() {
        this.iid = setInterval(function() {
            var CurrentScrollLeft = $( ".thumbnails .thumbnailphotos" ).scrollLeft();
            $( ".thumbnails .thumbnailphotos" ).scrollLeft( CurrentScrollLeft+10 );         
        }, 50);
    }).on('mouseleave', function(){
        this.iid && clearInterval(this.iid);
    });
}

Something like that anyways.

类似的东西。

#1


8  

A quick check for touch maybe?

快速检查一下触觉?

var tap = ("ontouchstart" in document.documentElement);

Then wrap your code in the condition:

然后在以下条件下包装代码:

if(!tap){
    $('.thumbnails .scrollright').on('mouseenter', function() {
        this.iid = setInterval(function() {
            var CurrentScrollLeft = $( ".thumbnails .thumbnailphotos" ).scrollLeft();
            $( ".thumbnails .thumbnailphotos" ).scrollLeft( CurrentScrollLeft+10 );         
        }, 50);
    }).on('mouseleave', function(){
        this.iid && clearInterval(this.iid);
    });
}

Something like that anyways.

类似的东西。