jQuery “blinking highlight” effect on div?

后端 未结 15 1121
囚心锁ツ
囚心锁ツ 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:00

    If you don't want the overhead of jQuery UI, I recently wrote a recursive solution using .animate(). You can customize the delays and colors as you need.

    function doBlink(id, count) {
        $(id).animate({ backgroundColor: "#fee3ea" }, {
            duration: 100, 
            complete: function() {
    
                // reset
                $(id).delay(100).animate({ backgroundColor: "#ffffff" }, {
                    duration: 100,
                    complete: function() {
    
                        // maybe call next round
                        if(count > 1) {
                            doBlink(id, --count);
                        }
                    }
                });
    
            }
        });
    }
    

    Of course you'll need the color plugin to get backgroundColor to work with .animate(). https://github.com/jquery/jquery-color

    And to provide a bit of context this is how I called it. I needed to scroll the page to my target div and then blink it.

    $(window).load(function(){
        $('html,body').animate({
            scrollTop: $(scrollToId).offset().top - 50
        }, {
            duration: 400,
            complete: function() { doBlink(scrollToId, 5); }
        });
    });
    

提交回复
热议问题