How can I pause a Bootstrap Carousel on hover and resume it on mouse-out?

前端 未结 3 1735
半阙折子戏
半阙折子戏 2021-01-20 20:58

I have a Bootstrap Carousel on my website.

When the user hovers over an element #formcontainer, I\'d like to pause the carousel. And when I hover off, I

3条回答
  •  星月不相逢
    2021-01-20 21:37

    jQuery's hover function has an implementation that takes two arguments: a hover in handler and a hover out handler:

    $('#foo').hover(function() {
        // handler in
    }, function() {
        // handler out
    });
    

    When you pass it just one argument, the function you give it handles both the in and out events, so you're pausing it on both mouse enter and mouse out.

    You need to pass it separate handlers:

    $('#myCarousel4').hover(function() {
        $(this).carousel('pause');
    }, function() {
        $(this).carousel('cycle');
    });
    

    Note that we can use this to refer to the carousel rather than rewriting its ID. Inside jQuery event handlers, this always refers to the object you bound the event to when possible.

提交回复
热议问题