Owlcarousel2: Reset autoplay timeout when scrolled by user

Created on 26 Sep 2018  路  4Comments  路  Source: OwlCarousel2/OwlCarousel2

How to reset autoplay timeout when scrolled by user?
Now, when user scroll the carousel manually, it can be scrolled twice: by user + by autoplay.

Most helpful comment

This happens with drag on mobile also (autoplay transition can happen just after a drag). I used this to reset autoplay both on drag and navigating with the buttons:

$('.owl-carousel').on('touchstart', 'img', function(e) {
    $(this).closest('.owl-carousel').trigger('stop.owl.autoplay');
});
$('.owl-carousel').on('touchend', 'img', function(e) {
    $(this).closest('.owl-carousel').trigger('play.owl.autoplay');
});
$('.owl-carousel').on('click', '.owl-dots, .owl-nav', function(e) {
    $(this).closest('.owl-carousel').trigger('stop.owl.autoplay');
    $(this).closest('.owl-carousel').trigger('play.owl.autoplay');
});

This should be the default behavior I think.

All 4 comments

+1

Probably there is better solution, but this is good enough for me now - though it doesn't handle mobiles (I hope they will rather drag than use the nav functions).

$('.owl-carousel').on('mouseenter', '.owl-dots, .owl-nav', function(e) {
    $(this).closest('.owl-carousel').trigger('stop.owl.autoplay');
});

$('.owl-carousel').on('mouseleave', '.owl-dots, .owl-nav', function(e) {
    $(this).closest('.owl-carousel').trigger('play.owl.autoplay');
});

This happens with drag on mobile also (autoplay transition can happen just after a drag). I used this to reset autoplay both on drag and navigating with the buttons:

$('.owl-carousel').on('touchstart', 'img', function(e) {
    $(this).closest('.owl-carousel').trigger('stop.owl.autoplay');
});
$('.owl-carousel').on('touchend', 'img', function(e) {
    $(this).closest('.owl-carousel').trigger('play.owl.autoplay');
});
$('.owl-carousel').on('click', '.owl-dots, .owl-nav', function(e) {
    $(this).closest('.owl-carousel').trigger('stop.owl.autoplay');
    $(this).closest('.owl-carousel').trigger('play.owl.autoplay');
});

This should be the default behavior I think.

@zcorpan that looks good, but it might need a check if autoplay was enabled for the carousel, and only trigger play.owl.autoplay if it was enabled before.

I couldn't get the touchstart or end to work using the above:- so I opted to use the onDragged Callback :-

```
var owl = $('#myCarousel').owlCarousel({
// other options removed for brevity
onDragged: resetTimeOut
});

function resetTimeOut(e) {
owl.trigger('stop.owl.autoplay');
owl.trigger('play.owl.autoplay');
}
owl.on('click', '.owl-dots, .owl-nav', function(e) {
resetTimeOut();
});

Note: I'm targeting one particular carousel here, but it could easily be updated for a more general deployment.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unite4 picture unite4  路  4Comments

leecollings picture leecollings  路  3Comments

SoufianeAbid picture SoufianeAbid  路  3Comments

SimonHarte picture SimonHarte  路  3Comments

JezCheese picture JezCheese  路  3Comments