开发者

jQuery add class for certain time

开发者 https://www.devze.com 2023-02-12 23:04 出处:网络
How I can add a css class to an el开发者_Python百科ement for only 10 seconds ?A nicely reusable way would be this little jQuery plugin:

How I can add a css class to an el开发者_Python百科ement for only 10 seconds ?


A nicely reusable way would be this little jQuery plugin:

(function($){

    $.fn.extend({ 

        addTemporaryClass: function(className, duration) {
            var elements = this;
            setTimeout(function() {
                elements.removeClass(className);
            }, duration);

            return this.each(function() {
                $(this).addClass(className);
            });
        }
    });

})(jQuery);

Use like so:

$("#myElement").addTemporaryClass("myClass", 10000);


You can add the class, then call setTimeout(function() { ... }, 10000) to remove it 10,000 milliseconds later.


Use setTimeout(function,10000); directly after you set the class on the element to remove the class from that element.


You can add class , and remove it without setTimeout() like this.

$(".count-box", element).addClass("red");
$(".count-box", element).removeClass("red", 10000);
0

精彩评论

暂无评论...
验证码 换一张
取 消