尝试每隔5秒按类别更改div jQuery [英] Trying to change div by class every 5 seconds jquery

查看:78
本文介绍了尝试每隔5秒按类别更改div jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我正在尝试创建一个类数组,间隔为5秒,我想添加和删除替换当前div的类.这是针对轮播,此时我当前正在更改背景图像.我现在想以相同的方式替换我的描述性文字.有人可以帮忙吗?

I am trying to create an array of classes and with a 5 second interval, I would like to add and remove class replacing the current div. This is for a carousel where I currently have the background image changing at this time. I would now like to make my descriptive text alternate in the same way. Can anyone help?

我当时正在考虑检查当前背景的if语句是否可行.我如何收到此错误:意外令牌{"

I was thinking an if statement checking the current background could work. Howver I get this error: 'unexpected token {'

$(document).ready(function(){

/* H E A D E R   C A R O S U E L */

    $(function() {
        var headCarosuel = $(".headCarosuel");
        var backgrounds = new Array(
        "url('./img/backgroundVinny1.jpg')","url('./img/backgroundVinny2.jpg')"
        );
        var current = 0;
        function nextBackground() {
        $(".headCarosuel").css("background", backgrounds[current = ++current % backgrounds.length]);
        setTimeout(nextBackground, 5000);
        }
        setTimeout(nextBackground, 5000);
        $(".headCarosuel").css("background", backgrounds[0]);

    if(backgrounds = $("url('./img/backgroundVinny1.jpg')").css() {
        $('.headCarosuelText').removeClass('description2').addClass('description1');
    }
    else {
        $('.headCarosuelText').removeClass('description1').addClass('description2');
    }

    });
});

推荐答案

也许是这样的:

var classes = [
  'class1',
  'class2',
  'class3',
  'class4',
  'class5'
]

var classIndex = 0;

function loopClasses() {
  var currentClass = classes[classIndex];

  // loop through classes and remove from element
  for (var i = 0; i < classes.length; i++) {
    $('.element').removeClass(classes[i]);
  }

  // add current class to element
  $('.element').addClass(currentClass);

  // advance or reset loop counter
  classIndex = (classIndex + 1) % classes.length

}

setInterval(loopClasses, 5000);

这篇关于尝试每隔5秒按类别更改div jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆