ganlanyuan / tiny-slider

Vanilla javascript slider for all purposes.
MIT License
5.24k stars 784 forks source link

Start autoplay instantly #498

Open cgpro opened 4 years ago

cgpro commented 4 years ago

Is it possible to start the slider instantly on page load? My use case is very slow moving logo bar.

JS:

tns({
    items: 7,
    slideBy: 1,
    controls: false,
    autoplay: true,
    speed: 10000,
    autoplayTimeout: 10000,
    autoplayButtonOutput: false,
});

CSS:

.tns-slider{
    transition-timing-function: linear;
}

My problem is: the slider starts after 10 seconds and not instantly ;-) Any ideas?

nicmare commented 4 years ago

some kind of infinite and constant autoplay would be nice.

dseeker commented 4 years ago

I don't know how to fix properly, but the issue lies in this function https://github.com/ganlanyuan/tiny-slider/blob/master/src/tiny-slider.js#L2384

it's invoking a setTimeout with the autoplayTimeout value. however setTimeout does not execute straightaway, it waits for the autoPlayTimeout value until it's triggered. a dirty fix would be to add something like this after the setInterval function

setTimeout(function(){
onControlsClick(null, autoplayDirection);
},10)

this would change behaviour to, instead of waiting the first 10 seconds to starts, it trigger the autoplay once at the beginning before the 10 seconds

a proper fix would be nice to have, I don't know what other consequences this would have