简体   繁体   English

如何设置此“滑块”每5秒更改一次

[英]How to set this “slider” to change every 5 seconds

I developed this "slider" in jQuery 我在jQuery中开发了这种“滑块”

HTML: HTML:

<div class="col-md-6 pos-rel text-center">
   <div class="slider-meio slider1">    
      <img class="img-responsive" src="http://www.novahidraulica.com.br/imgcategoria/1/2014-09-24-11-09-0420.jpg">
   </div>
</div>
<div class="col-md-6 pos-rel text-center">
   <div class="slider-meio active slider2"> 
      <img class="img-responsive" src="http://www.novahidraulica.com.br/imgcategoria/1/2014-09-24-11-09-0420.jpg">
   </div>
</div>
<div class="col-md-3">
   <ul class="controle-slider">
      <li class="active-slider"><a data-target=".slider1" >LINHA FACE PLANA</a></li>
      <li class=""><a data-target=".slider2" >LINHA COLHEDORA</a></li>
   </ul>
</div>

JS: JS:

 function montaSlider() {
            $(".slider-meio").each(function () {
                if($(this).hasClass("active")){
                   $(this).fadeIn();
                }else {
                    $(this).fadeOut();
                }
            });
        }
        montaSlider();
        $(".controle-slider li a").click(function () {
            $(".controle-slider li a").parent().removeClass("active-slider");
            $(this).parent().addClass("active-slider")
            $(".slider-meio").removeClass("active");
            $($(this).attr("data-target")).addClass("active")
            montaSlider();
        });

i want to change the slide every 5 seconds, but i cant think of how to do it 我想每5秒钟更换一次幻灯片,但我想不出该怎么做

can anyone help me? 谁能帮我?

Use the setInterval() function: 使用setInterval()函数:

var intrvl = setInterval(function(){montaSlider()},5000);

I you need to stop it use: 我需要停止使用:

clearInterval(intrvl);

You can use the window.setInterval() javascript method to call your montarSlider() every 5 seconds. 您可以使用window.setInterval() javascript方法每5秒调用一次montarSlider() Example: 例:

var timer = window.seInterval(montarSlider, 5000);

I recommend you to store the variable returned by the setInterval() method so you can later stop it if necessary. 我建议您存储由setInterval()方法返回的变量,以便以后可以根据需要停止它。

EDIT: Since you also need to rotate the elements with the active class, you can first make a function called slide() to activate the next element before calling the montarSlider() function. 编辑:由于您还需要旋转带有active类的元素,因此您可以在调用montarSlider()函数之前先创建一个名为slide()的函数来激活下一个元素。 Then, instead of set the interval to the montarSlider() function, you set it to the slide() function. 然后,不要将间隔设置为montarSlider()函数,而是将其设置为slide()函数。 Example: 例:

function slide() {
    var currentActive = $(".slider-meio.active");
    var nextActive;
    currentActive.removeClass("active");

    if(currentActive.parent().next().children(".slider-meio").length > 0) {
        nextActive = currentActive.parent().next().children(".slider-meio");
    } else {
        nextActive = $(currentActive.parent().siblings()[0]).children(".slider-meio");
    }
    nextActive.addClass("active");
    montaSlider();
}
var timer = window.seInterval(slide, 5000);

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

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