本文介绍了jQuery项目类的定时更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据某些类型的计时器更改项目类别或ID?每隔1500毫秒,它就会转到hey-there-1,然后在1500ms之后出现hey-there-2 ..等等.我可以控制它经过多少时间间隔.

Is it possible to change an items class or ID based on some type of timer? Every 1500ms it would go to hey-there-1, then 1500ms later hey-there-2.. etc.. something that I could possibly control how many intervals it goes through..

  • 嘿,有
  • hey-there-1
  • hey-there-2
  • hey-there-3
  • (从头开始)嘿

谢谢

推荐答案

像这样的插件函数将允许您执行所需的操作:

A plugin function as such will allow you to do what you want:

jQuery.fn.rotateClasses = function(classes, interval, max) {
    var currentRotation = 0;
    var timer = null;

    var rotateFn = (function() {
            var currentClass = currentRotation % classes.length;

        var previousClass = currentClass - 1;
        if(previousClass == -1) previousClass = classes.length - 1;

        this.addClass(classes[currentClass]).removeClass(classes[previousClass]);

        currentRotation += 1;

            if(max > 0 && currentRotation >= max) clearInterval(timer);
    })();

    timer = setInterval(rotateFn, interval);

    return this;
};

然后您可以使用以下命令简单地调用它:

Then you can simply call it using the following:

$('#mydiv').rotateClasses(["class1", "class2", "class3"], 1000, 20);

只需根据需要修改参数.第一个参数是要旋转的类的数组,第二个参数是将它们旋转到的间隔,第三个参数是最大迭代次数.如果设置为0,则会迭代广告无限.

Simply modify the parameters as you wish. First parameter is an array of classes to rotate, second is the interval to rotate them to, and the third is the maximum number of iterations. If set to 0, it iterates ad-infinitum.

这篇关于jQuery项目类的定时更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:37