简体   繁体   English

当滚动到元素不工作时,猫头鹰轮播自动播放

[英]Owl carousel autoplay when scrolling to element not working

I want to autoplay my owl carousel (v2) when scrolling to the element that contains it. 我想在滚动到包含它的元素时自动播放我的猫头鹰旋转木马(v2)。 For some reason it slides once and then stops when I enter with my mouse. 出于某种原因,当我用鼠标进入时,它会滑动一次然后停止。

This is my html element for which I want to trigger the autoplay: 这是我想要触发自动播放的html元素:

<div class="owl-carousel r-latest-news-list" id="r-latest-news-slider">

All is correctly loaded because if I set autoplay to start on pageload like how it is usually done, it works. 所有都正确加载,因为如果我设置自动播放开始在页面加载像往常一样,它的工作原理。

This is my code to trigger autoplay when entering the mouse over that element: 这是我的代码,当在该元素上输入鼠标时触发自动播放:

if($("#r-latest-news-slider").length > 0){
    var owl = $('#r-latest-news-slider').owlCarousel({
        loop:true,
        margin: 30,
        items: 4,
        nav: false,
        dots: true,
        responsive:{
            0:{
                items:2
            },
            600:
                items:2
            },
            1000:{
                items:4
            }
        }
    })
    $('#r-latest-news-slider').on("mouseenter", function(e) {
        console.log('mouse enter');
        owl.trigger('play.owl.autoplay', [2000]);
    })
}

This is the documentation I am following: https://owlcarousel2.github.io/OwlCarousel2/docs/api-events.html 这是我正在关注的文档: https : //owlcarousel2.github.io/OwlCarousel2/docs/api-events.html

Hey the question was answered here: https://lazyfox.io/task/qP6/owl-carousel-autoplay-when-scrolling-to-element-not-working 嘿这个问题在这里得到了解答: https//lazyfox.io/task/qP6/owl-carousel-autoplay-when-scrolling-to-element-not-working

if($("#r-latest-news-slider").length > 0) {
    var owl = $('#r-latest-news-slider').owlCarousel({
        loop: true,
        margin: 30,
        items: 4,
        nav: false,
        dots: true,
        responsive: {
            0: {
                items:2
            },
            600: {
                items:2
            },
            1000: {
                items:4
            }
        }
    });
}

// Function for checking if the element is in view
function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

var animate = false;

// Function activated when scrolling
$(window).scroll(function() {
    // Check if the element is visible
    if(isScrolledIntoView("#r-latest-news-slider") && !animate) {
        owl.trigger('play.owl.autoplay', [1000]);
        animate = true;

        console.log('Starting animation');
    } else if(!isScrolledIntoView("#r-latest-news-slider") && animate) {
        owl.trigger('stop.owl.autoplay', [1000]);
        animate = false;

        console.log('Stopping animation');
    }
});

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

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