[英]Owl carousel autoplay when scrolling to element not working
我想在滚动到包含它的元素时自动播放我的猫头鹰旋转木马(v2)。 出于某种原因,当我用鼠标进入时,它会滑动一次然后停止。
这是我想要触发自动播放的html元素:
<div class="owl-carousel r-latest-news-list" id="r-latest-news-slider">
所有都正确加载,因为如果我设置自动播放开始在页面加载像往常一样,它的工作原理。
这是我的代码,当在该元素上输入鼠标时触发自动播放:
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]);
})
}
这是我正在关注的文档: https : //owlcarousel2.github.io/OwlCarousel2/docs/api-events.html
嘿这个问题在这里得到了解答: 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.