jQuery “blinking highlight” effect on div?

后端 未结 15 1122
囚心锁ツ
囚心锁ツ 2020-12-04 09:24

I\'m looking for a way to do the following.

I add a

to a page, and an ajax callback returns some value. The
is fill
15条回答
  •  盖世英雄少女心
    2020-12-04 10:08

    I couldn't find exactly what I was looking for so wrote something as basic as I could make it. The highlighted class could just be a background color.

    var blinking = false; // global scope
    
    function start_blinking() {
        blinking = true;
        blink();
    }
    
    function stop_blinking() {
        blinking = false;
    }
    
    function blink(x=0) {
        $("#element").removeClass("highlighted"); // default to not highlighted to account for the extra iteration after stopping
    
        if (blinking) {
            if (x%2 == 0) $("#element").addClass("highlighted"); // highlight on even numbers of x
            setTimeout(function(){blink(++x)},500); // increment x and recurse
        }
    }
    

提交回复
热议问题