Owl Carousel limit number of loops with autoPlayTimeout doesn't stop looping - javascript

Is autoPlayTimeout not intended to stop autoplay:true? Because no matter what I try Owl Carousel never stops.
$('.owl-carousel').owlCarousel({
items: 1,
autoplay:true,
autoPlaySpeed:500,
autoPlayTimeout:5000,
autoPlayHoverPause:true,
loop:true,
Surely someone else wanted something other than loop forever, since that is annoying.

You can combine the 'stop.owl.autoplay' trigger with a timer and stop the carousel after going through each slide once, or twice or as much as you want.
In order to loop it twice and stop, you can use the following configurations:
loop: true
autoplayTimeout : 1000 (1 second just for the example)
items: 1
Now with these options, your slider will loop forever, showing one slide for 1 second and move to the next one.
In order to stop it after going through each slide twice, you can use the 'onInitialized' event provided by owl and call a function when the slider is initialized. Let's say you have 3 slides in total, so if you want to stop it after the second loop, you have to wait 6000ms (6 seconds).
So in the callback function, you can set a timer to stop the slider at the 6th second.
Example carousel :
let $carousel = $('.carousel');
let itemsCount = Number($('.carousel').attr("data-items"));
let autoplayTimeout = 1000;
let loops = 2;
$carousel.owlCarousel({
loop: true,
autoplay: true,
nav: true,
dots: true,
items: 1,
autoplayTimeout: autoplayTimeout,
// Stops after the second cycle
onInitialized : stopTimer({
itemsCount,
autoplayTimeout,
loops
}),
});
function stopTimer(params) {
setTimeout(
function() {
$carousel.trigger('stop.owl.autoplay');
},
params.itemsCount * Number(params.autoplayTimeout) * params.loops
);
}
You can find more events, provided by owl HERE.
Hope this will help! :)

To stop the owl carousel, you can trigger:
$('.owl-carousel').trigger('stop.owl.autoplay')
JSFiddle

Related

Animation disappears on end LottieJS

I have an animation build with LottieJS which loops like:
var lottieLogo = lottie.loadAnimation({
container: document.getElementById('icon_logo'), // the dom element that will contain the animation
renderer: 'svg',
loop: true,
autoplay: false,
path: 'path-to-json'
});
lottieLogo.play();
Then after animation is done I want to pause for 10 seconds like:
function logoAnimation() {
lottieLogo.pause();
setTimeout(
function()
{
lottieLogo.play();
}, 10000);
}
lottieLogo.addEventListener('loopComplete', logoAnimation);
It works fine but the only problem is that the animation disappears for about 0.2 seconds every time it loads again. Anyone familiar with this problem?
If you set loop to false it should give you the desired effect.

Slick Slider - slick-current class is not moving when navigating if all slides are shown

I have a simple slider with Slick.js (https://kenwheeler.github.io/slick/)
The slider arrows are hidden by default when all slide-items are shown because its not necessary to slide. So i use custom arrows with a click function that call .slick('slickNext') or .slick('slickPrev') and its working fine. But my issue is when all slide-items are shown and I call the functions to slide next or previous the .slick-current class on the active slide-item is not switching anymore.
My goal is that the .slick-current class still moves when I click the buttons.
An example: https://jsfiddle.net/0nprbj95/2/ Appreciate your help!
One solution to ensure that Slick is moving the .slick-current class is to hold the state of the current slide index and then use the slick('goTo') method to force Slick to move the class to this slide.
This may not give exactly the behavior that you want because Slick will still shift the slides in its "viewport", but you might be able to tweak the behavior from there.
$(document).ready(function(){
var currentSlide = 0;
var numSlides = document.getElementsByClassName('slide-item').length;
$('.slider-items-wrapper').slick({
slidesToShow: 4,
slidesToScroll: 1,
mobileFirst: true,
infinite: true,
arrows: false,
});
$('.button-prev').click(function(){
// wrap to last slide when clicked on first slide
currentSlide = (currentSlide - 1 + numSlides) % numSlides;
$('.slider-items-wrapper').slick('goTo', currentSlide);
});
$('.button-next').click(function(){
// wrap to first slide when clicked on last slide
currentSlide = (currentSlide + 1) % numSlides;
$('.slider-items-wrapper').slick('goTo', currentSlide);
});
});
Full JSFiddle: https://jsfiddle.net/c3amxzk1/1/

How do I deal with cloned slides to create a seamless html5 video slider in SlickJS?

I'm using slickJS to create a slider with HTML5 video backgrounds for each slide. Here's my SlickJS init code:
$homeSlider.slick({
adaptiveHeight: true,
arrows: false,
autoplay: true,
autoplaySpeed: 4000,
dots: true,
fade: false,
initialSlide: startSlide,
lazyLoad : 'progressive',
onBeforeChange: beforeSlickChange
});
Everything's working fine except that when I reach the end of the list of slides and try to go to the next slide (this loops back to the first slide), Slick shows a cloned slide for a moment and then the real first slide. Because of this, I'm unable to get a seamless transition between the last and first slide going in either direction. When I click forward to the first slide from the last slide or to the last slide from the first slide, the dummy slide appears (with no video playing) and after about 500ms or so, the real slide takes its place with the video playing.
What I'd like to happen is for the video to be playing seamlessly when transitioning two slides. I tried playing the video on the cloned slide and then playing the real slide's video from a slightly later point so that when the cloned slide disappeared, the real slide's video would be at the same point and there would be no noticeable change. However, the amount of time it takes for the cloned slide to disappear seems inconsistent.
Does anyone have a solution for this problem? Changing the slider to fade instead of slide fixes this problem, but I'd like to keep the sliding behavior. Thank you!
I managed to solve this problem. What I did was to play the video on the cloned slide and the real slide and then set the currentTime property of the real slide to that of the cloned slide on the afterChange event. The code looks like this:
Init code:
$homeSlider.slick({
adaptiveHeight: true,
arrows: false,
autoplay: true,
autoplaySpeed: 4000,
dots: true,
fade: false,
initialSlide: startSlide,
lazyLoad : 'progressive',
onBeforeChange: beforeSlickChange,
onAfterChange: afterSlickChange
});
onBeforeChange handler:
function beforeSlickChange(slick, currentSlide, nextSlide) {
if(hasVideoBG(currentSlide) === true && isMobile(mobileQuery) === false) {
getSlideVideoByIndex(currentSlide).currentTime = 0;
getSlideVideoByIndex(nextSlide).currentTime = 0;
}
if(hasVideoBG(nextSlide) === true && isMobile(mobileQuery)=== false) {
if(nextSlide === 0) {
$homeSlider.find(".slick-cloned:eq(1) video").get(0).currentTime = 0;
$homeSlider.find(".slick-cloned:eq(1) video").get(0).play();
}
if(nextSlide === (getSliderCount() - 1)) {
$homeSlider.find(".slick-cloned:eq(0) video").get(0).currentTime = 0;
$homeSlider.find(".slick-cloned:eq(0) video").get(0).play();
}
getSlideVideoByIndex(nextSlide).play();
}
}
onAfterChange handler:
function afterSlickChange(slick, currentSlide) {
if(currentSlide === 0) {
var $slideVideo = $homeSlider.find(".slick-cloned:eq(1) video").get(0);
getSlideByIndex(currentSlide).find("video").get(0).currentTime = $slideVideo.currentTime
} else if(currentSlide === (getSliderCount - 1)) {
var $slideVideo = $homeSlider.find(".slick-cloned:eq(0) video").get(0);
getSlideByIndex(currentSlide).find("video").get(0).currentTime = $slideVideo.currentTime
}
}
Basically I am resetting the currentTime of both the real and cloned slide videos to 0 and playing them before the slide changes. After the slide changes, I'm setting the currentTime property of the real slide to that of the cloned slide. This makes it so that when the cloned slide disappears, the real slide is playing the same video at the same time and the transition is seamless.

bxSlider - Play/Pause all sliders on page

I have two sliders on a page. Called with the same set of times and settings. One slider is a image slider, the other a sort of caption for it. Both in drastically different areas of the html.
bxslider is creating a play/pause for each slideshow, but I want some way of having one button that pauses all slideshows on the current page. This is because the slideshows are relative to each other and must keep a:
Slider A - Slide 1 / Slider B - Slide 1
Slider A - Slide 2 / Slider B - Slide 2
Slider A - Slide 3 / Slider B - Slide 3
etc.
Right now, when I pause one slideshow, the other continues and when the play is pressed, they are no longer in sync.
.bxslider & .bxslider-content are both my bxslider classes.
Thanks!
<script type="text/javascript">
$(document).ready(function () {
$('.bxslider, .bxslider-content').bxSlider({
mode: 'fade',
auto: true,
ease: 'cubic-bezier(0.42,0,0.58,1)',
pager: false,
controls: false,
autoControlsCombine: true,
autoControls: true,
pause: 8000,
speed: 800
});
});
</script>
Try
var slider=$('.bxslider').bxSlider({
//code here
});
var slider1=$('.bxslider-content').bxSlider({
//code here
});
$(document).on('click','.pause',function(){
slider.stopAuto();
slider1.stopAuto();
});
$(document).on('click','.play',function(){
slider.startAuto();
slider1.startAuto();
});
Fiddle http://jsfiddle.net/CDvmk/

Nivo Slider - Stop animation when only one image

I am using the jQuery plugin for Nivo Slider and need to find a way to stop it from transitioning when only one image exists.
Can you set the option:
manualAdvance: true
Will that help? This is the documentation for the latest NivoSlider update.
If this wont help, can you post the code you are using to enable the slider?
This will be the full code:
$(window).load(function() {
$('#slider').nivoSlider({
slices: 1, // For slice animations
startSlide: 0, // Set starting Slide (0 index)
manualAdvance: true, // Force manual transitions
captionOpacity: 0.8, // Universal caption opacity
randomStart: false, // Start on a random slide
beforeChange: function(){}, // Triggers before a slide transition
afterChange: function(){}, // Triggers after a slide transition
slideshowEnd: function(){}, // Triggers after all slides have been shown
lastSlide: function(){}, // Triggers when last slide is shown
afterLoad: function(){} // Triggers when slider has loaded
});
});
There is probably a better way to do it but it works for me:
if($('#slider img').length == 1) {
$('#slider').nivoSlider({});
$('.nivo-controlNav').css('display', 'none');
$('.nivo-directionNav').css('display', 'none');
$('#slider').data('nivo:vars').stop = true;
} else {
$('#slider').nivoSlider({
effect: 'slideInLeft'
});
}
PS. It's important to check the number of images before initializing Nivoslider because it seems to duplicate image tags...
None of the answers quite worked for me, because I still wanted a single image to display with a caption. I ended up using slightly different options to initialise the nivoSlider depending on the number of images (my images were in a containing div with the id 'hero-images'):
var numImages = $('#hero-images img').length;
if (numImages === 0) {
//No images - hide the block
$('#hero-images').hide();
} else if (numImages === 1) {
// 1 image - disable controls and set to manual advance to prevent animation
$('#hero-images').nivoSlider({
directionNav: false,
manualAdvance: true,
controlNav: false
});
} else {
// Multiple images, set up as normal
$('#hero-images').nivoSlider({
effect: 'fade',
directionNav: false
});
}

Categories