简体   繁体   English

禁用 Materialise Carousel 上的 Touch

[英]Disable Touch on Materialize Carousel

It looks like no one has asked this question before since I've pretty much scoured the internet looking for a very simple answer.看起来以前没有人问过这个问题,因为我已经在互联网上搜索了很多寻找一个非常简单的答案。

How would one go about disabling the ability to swipe left/right on the materialize carousel?如何禁用在实体化轮播上向左/向右滑动的能力?

in Materialize.js add/edit:在 Materialize.js 添加/编辑:

var allowCarouselDrag = true;
value: function _handleCarouselDrag(e) {
    if(allowCarouselDrag){
       ....
    }
}

You can set the allowCarouselDrag variable per application.您可以为每个应用程序设置 allowCarouselDrag 变量。

I solved it like this我是这样解决的

// Create carouser
$('#carousel').carousel({
            fullWidth: true,
            indicators: false,
            duration: 100,
        });

// Get instance of carousel
carouselInstance = M.Carousel.getInstance(sliderDOM);

// Remove event listeners added by Materialize for corousel
document.getElementById("carousel").removeEventListener('mousedown', carouselInstance._handleCarouselTapBound);
document.getElementById("carousel").removeEventListener('mousemove', carouselInstance._handleCarouselDragBound);
document.getElementById("carousel").removeEventListener('mouseup', carouselInstance._handleCarouselReleaseBound);
document.getElementById("carousel").removeEventListener('mouseleave', carouselInstance._handleCarouselReleaseBound);
document.getElementById("carousel").removeEventListener('click', carouselInstance._handleCarouselClickBound);

After that drag/swipe is disabled and you can still change page/item via在禁用拖动/滑动之后,您仍然可以通过以下方式更改页面/项目

carouselInstance.set(0);

and

carouselInstance.next(0);

This is far from a perfect solution, and it might disable too much of the functionality in your case, I'm not sure.这远不是一个完美的解决方案,它可能会禁用您的情况下的太多功能,我不确定。 An option to turn this on/off would be much appreciated.非常感谢打开/关闭此功能的选项。

But for my needs, turning off the events on the carousel did the job:但对于我的需要,关闭旋转木马上的事件完成了这项工作:

var carousel = $('.carousel.carousel-slider').carousel();
// Disable all swipping on carousel
if (typeof window.ontouchstart !== 'undefined') {
    carousel.off('touchstart.carousel');
}
carousel.off('mousedown.carousel');
function tap(e) {

    pressed = true;
    dragged = false;
    vertical_dragged = true;
    reference = xpos(e);
    referenceY = ypos(e);

    velocity = amplitude = 0;
    frame = offset;
    timestamp = Date.now();
    clearInterval(ticker);
    ticker = setInterval(track, 100);
}

I have attempted to solve this problem for the past ~three days and have come to the conclusion that there is no clean solution other than directly editing the materialize.js file as in Lester's answer.在过去的三天里,我一直试图解决这个问题,并得出的结论是,除了像 Lester 的回答那样直接编辑 materialize.js 文件之外,没有其他干净的解决方案。 Unfortunately, this is not an ideal solution as it causes issues when updating Materialize etc.不幸的是,这不是一个理想的解决方案,因为它会在更新 Materialise 等时导致问题。
The simplest solution that I have come up with after this time is the following piece of javascript:在此之后,我想出的最简单的解决方案是以下 javascript:

window.onload = function() {    
    window.mouseDownNow = false;
    // The selector below must be more specific than .carousel.carousel-slider in
    // order for the event to be cancelled properly.
    $('.class-added-to-block-swiping-functionality')
    .mousedown(function() {
        window.mouseDownNow = true;
    })
    .mousemove(function(event) {
        if(window.mouseDownNow) {
            event.stopPropagation();
        }
    })
    .mouseup(function() {
        window.mouseDownNow = false;
    });
};

This will simply stop the event from bubbling to the Materialize swiping functionality.这将简单地阻止事件冒泡到 Materialise 滑动功能。
Note: I am not sure how specific the selector must be, mine were classes that were specific to text areas.注意:我不确定选择器必须有多具体,我的是特定于文本区域的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM