0

I want to disable vertical scrolling on webpage while swiping the carousel horizontally on mobile devices. I'm using the Owl carousel.

I have tried to use css overflow: hidden to html, body but doesn't work. Tried the other solutions out there but they don't work. The code I have tried is down below.

// Tried this but doesn't work
var owl = $j(".owl-carousel");
owl.on('drag.owl.carousel', function(event) {
document.ontouchmove = function (e) {
    e.preventDefault()
}
});
owl.on('dragged.owl.carousel', function(event) {
document.ontouchmove = function (e) {
    return true
}
});

// Tried this but doesn't work
owl.on('drag.owl.carousel', function(event) {
    $('body').css('overflow', 'hidden');
});

owl.on('dragged.owl.carousel', function(event) {
    $('body').css('overflow', 'auto');
});
designbyme
  • 55
  • 1
  • 8
  • How would it scroll vertically if your swiping left or right? Will you attach the link to your site? Thanks. – Jerdine Sabio Aug 09 '19 at 08:29
  • When swiping left or right on the carousel it also moves the page up and down. It should be fixed as soon you touch the carousel. – designbyme Aug 09 '19 at 08:48
  • Example as this carousel [https://nickpiscitelli.github.io/Glider.js](https://nickpiscitelli.github.io/Glider.js/) Thanks. – designbyme Aug 09 '19 at 09:18

1 Answers1

3

Did you try this where you initialize the carousel?

$j(".owl-carousel").owlCarousel({
    onDragged: function() {
       $j('body').css('overflow', 'auto');
    },
    onDrag: function() {
       $j('body').css('overflow', 'hidden');
    }
});

Also, I'd recommend just adding/removing a class to the body/html tag which adds the overflow, rather than using JS to manipulate the CSS itself.

UPDATE: Based on the comments, it appears there is a known bug. Someone else asked this same question and got an answer here: disable scrolling when trigger owl carousel on mobile device

For data removal purposes, I'll copy the answer that someone claimed works on IOS. Please test and tell us if it worked for your application:

var owl = $('.owl-carousel');
    owl.owlCarousel({
    //  your options
})

// disable scroll
owl.on('drag.owl.carousel', function(event) {
    document.ontouchmove = function (e) {
        e.preventDefault()
    }
})

// enable scroll
owl.on('dragged.owl.carousel', function(event) {
    document.ontouchmove = function (e) {
        return true
    }
})

I would still recommend leaving your current code as is, including the onDrag and onDragged functions, and just update it. Replace your code inside your script tag with this:

var $j = jQuery.noConflict();
$j( document ).ready( function() {
    var owl = $j( ".owl-carousel" ).owlCarousel( {
        stagePadding: 90,
        loop: true,
        lazyLoad: true,
        dots: false,
        margin: 10,
        nav: false,
        onDragged: function() {
            $j( 'body' ).css( 'overflow', 'auto' );
        },
        onDrag: function() {
            $j( 'body' ).css( 'overflow', 'hidden' );
        },
        responsive: {
            0: {
                items: 2
            },
            500: {
                items: 4
            },
            1000: {
                items: 5
            }
        }
    } );

    // disable scroll
    owl.on( 'drag.owl.carousel', function( event ) {
        document.ontouchmove = function( e ) {
            e.preventDefault();
        }
    } );

    // enable scroll
    owl.on( 'dragged.owl.carousel', function( event ) {
        document.ontouchmove = function( e ) {
            return true;
        }
    } );

} );
Daniel C
  • 2,105
  • 12
  • 18
  • Thank you, I have tried it and it doesn't work. Here is the full code below – designbyme Aug 09 '19 at 17:32
  • I also tried css `body.home {` `overflow: hidden!important;` `}` – designbyme Aug 09 '19 at 17:40
  • Getting any JS errors? Can you share a link to your website with this on it? – Daniel C Aug 09 '19 at 17:44
  • The error is because in MY code I used $, but you initialized it with $j... so I updated my code. Try again? – Daniel C Aug 09 '19 at 18:32
  • Thanks change that now and the errors are gone but the page is still going up and down while swiping carousel. – designbyme Aug 09 '19 at 18:38
  • You have a caching issue with your browser then... it works for me, and I tested multiple browsers! What browser and OS are you using? Try opening your site in a Private browser to assure a clear cache. – Daniel C Aug 09 '19 at 18:41
  • Did you test on mobile device? Or in the dev chrome tool? I'm testing on iOS Safari and Chrome and it is not working because it will be only used on mobile devices. – designbyme Aug 09 '19 at 18:56
  • Yes... for real mobile I tested on Android only, as that is the phone I have. I also used Dev tools in multiple browsers. So it appears to be an IOS issue. At least you know it works on non-IOS devices! I'll throw in an option for IOS to see if we can capture that. – Daniel C Aug 09 '19 at 19:10
  • @designbyme Also note, there is a known bug with this carousel and IOS. Is this what you are experiencing? https://github.com/OwlCarousel2/OwlCarousel2/issues/2299. If so, the code provided may very well be working... you're just experiencing the bug. Let me know! – Daniel C Aug 09 '19 at 19:13
  • It is, same as the bug, tested the css `touch-action: none` on class .owl-item without success. – designbyme Aug 09 '19 at 19:22
  • But the bug seems only to go up not down, the experience I have is more that it is sliding (jiggle around when using carousel) It seems it doesn't take the effect of the overflow at all. – designbyme Aug 09 '19 at 19:31
  • I updated the answer with the only possible solution someone claims worked... hopefully it helps your cause. Otherwise, know that it is only affecting IOS... and only certain versions at that! Most other browsers and OSs won't experience this effect. – Daniel C Aug 09 '19 at 19:35
  • Thanks I understand did try that code before as written in my post. Added it once more but getting error. Not sure where to place the code? – designbyme Aug 09 '19 at 19:50
  • @designbyme I updated the last block of code to use exactly what you have on your website. Just copy and paste it, and replace what's inside your ` – Daniel C Aug 09 '19 at 19:57
  • This is awesome! It works now. Thanks a lot for the help. – designbyme Aug 09 '19 at 20:04
  • Thanks for your solution.I used this code but when I reach to last slide in owl-carousel and I want to scroll to the bottom of the page it is crashed and I can not move the page and I have to go back to the prev slide and then it work correctly and I can move on the page. how can solve this issue?thanks. my website is https://www.khaaspo.com/Product/ProductDeialsInMob?Code=93 – Elham Bagheri Jul 18 '20 at 06:12