仅在向左或向右滑动时禁用垂直滚动-Carousel Bootstrap 4

问题描述

我在Stack Overflow上在线找到了以下代码,但对于将其用于Carousel Bootstrap 4感到不满意。

.carousel {
touch-action: none;
} 

由于用户可能不知道如何滚动,因此轮播几乎占据了整个移动屏幕。

有一种方法是,如果i向左或向右滑动,则只能禁用垂直滚动。如果我只是触摸并向下滑动,请再次激活垂直滚动。

是否可以在手指移动的方式上定位代码?如果我从左到右移动,则阻止垂直滚动,而如果我向上移动,则启用垂直滚动。

$(document).ready(function() {
        (function ($) {

            var touchStartX = null;

            $('.carousel').each(function () {
                var $carousel = $(this);
                $(this).on('touchstart',function (event) {
                    var e = event.originalEvent;
                    if (e.touches.length == 1) {
                        var touch = e.touches[0];
                        touchStartX = touch.pageX;
                    }
                }).on('touchmove',function (event) {
                    var e = event.originalEvent;
                    if (touchStartX != null) {
                        var touchCurrentX = e.changedTouches[0].pageX;
                        if ((touchCurrentX - touchStartX) > 60) {
                            touchStartX = null;
                            $carousel.carousel('prev');
                        } else if ((touchStartX - touchCurrentX) > 60) {
                            touchStartX = null;
                            $carousel.carousel('next');
                        }
                    }
                }).on('touchend',function () {
                    touchStartX = null;
                });
            });

        })(jQuery);
      });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)