javascript 可以禁用幻灯片的触摸模拟,但不能禁用滚动条(危险的 swiper)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23780805/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:33:18  来源:igfitidea点击:

Possible to disable touch simulation for slides but not scrollbar (idangerous swiper)?

javascriptjqueryswiper

提问by Tolgahan ?zgür

I have an idangerous swiper on my page which successfully simulates touch events on both the slides and accompanying scrollbar (allowing a mouse click and movement to slide slides left or right).

我的页面上有一个危险的 swiper,它成功地模拟了幻灯片和随附滚动条上的触摸事件(允许单击鼠标和移动向左或向右滑动幻灯片)。

This is fine, but I've now called draggable on the slides within the swiper, which means I need to stop this touch simulation (dragging the slides and moving them at the same time is causing confusion) - but only on the slides, not the scrollbar (I still need the scrollbar to function as it did subsequent to a mouse click and movement).

这很好,但我现在在 swiper 中的幻灯片上调用了 draggable,这意味着我需要停止这个触摸模拟(拖动幻灯片并同时移动它们会引起混乱) - 但仅限于幻灯片,而不是滚动条(我仍然需要滚动条在单击和移动鼠标后才能发挥作用)。

What I've Tried

我试过的

According to the API I can disable touch simulation:

根据API,我可以禁用触摸模拟:

var swiper = new Swiper('.swiper', {
    slidesPerView: 3,
    simulateTouch: false,
    scrollbar: {
        container: '.swiper-scrollbar',
        hide: false,
        draggable: true,
        snapOnRelease: true
    }
});

This works as expected, but on both the scrollbar and slides, which is no good.

这按预期工作,但在滚动条和幻灯片上都没有好处。

I've also tried returning false from a number of the events the swiper api exposes:

我还尝试从 swiper api 公开的许多事件中返回 false:

var swiper = new Swiper('.swiper', { slidesPerView: 3, onTouchStart: function() { return false; }, scrollbar: { container: '.swiper-scrollbar', hide: false, draggable: true, snapOnRelease: true } });

var swiper = new Swiper('.swiper', { slidesPerView: 3, onTouchStart: function() { return false; }, scrollbar: { container: '.swiper-scrollbar', hide: false, draggable: true, snapOnRelease: true } });

This doesn't have any effect at all.

这根本没有任何影响。

回答by Tolgahan ?zgür

Here is solution for Swiper-3.3.1

这是 Swiper-3.3.1 的解决方案

simulateTouch:false

回答by Tolgahan ?zgür

I found a way that, for now, I'm happy with.

我找到了一种方法,目前我很满意。

In the idangerous swiper source (idangerous.swiper-2.1.js) I return false from the onTouchStartfunction (line 1120) so my code now resembles the following:

在 idangerous swiper 源代码 (idangerous.swiper-2.1.js) 中,我从onTouchStart函数(第 1120 行)返回 false,因此我的代码现在类似于以下内容:

    function onTouchStart(event) {
    if (params.preventLinks) _this.allowLinks = true;
    //Exit if slider is already was touched

    return false;

    if (_this.isTouched || params.onlyExternal) {
        return false;
    }

This is non-invasive to the way the scrollbar prototype works too, so the scrollbar's touch events are left in-tact.

这对滚动条原型的工作方式也是非侵入性的,因此滚动条的触摸事件保持原样。