jQuery “blinking highlight” effect on div?

后端 未结 15 1053
囚心锁ツ
囚心锁ツ 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
        }
    }
    
    0 讨论(0)
  • 2020-12-04 10:11

    I use different predefined colors like so:

    theme = {
        whateverFlashColor: '#ffffaa',
        whateverElseFlashColor: '#bbffaa',
        whateverElseThenFlashColor: '#ffffff',
    };
    

    and use them like this

    $('#element').effect("highlight", {color:theme.whateverFlashColor}, 1000);
    

    easy :)

    0 讨论(0)
  • 2020-12-04 10:12

    If you are not already using the Jquery UI library and you want to mimic the effect what you can do is very simple

    $('#blinkElement').fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100);
    

    you can also play around with the numbers to get a faster or slower one to fit your design better.

    This can also be a global jquery function so you can use the same effect across the site. Also note that if you put this code in a for loop you can have 1 milion pulses so therefore you are not restricted to the default 6 or how much the default is.

    EDIT: Adding this as a global jQuery function

    $.fn.Blink = function (interval = 100, iterate = 1) {
        for (i = 1; i <= iterate; i++)
            $(this).fadeOut(interval).fadeIn(interval);
    }
    

    Blink any element easily from your site using the following

    $('#myElement').Blink(); // Will Blink once
    
    $('#myElement').Blink(500); // Will Blink once, but slowly
    
    $('#myElement').Blink(100, 50); // Will Blink 50 times once
    
    0 讨论(0)
  • 2020-12-04 10:18

    Check it out -

    <input type="button" id="btnclick" value="click" />
    var intervalA;
            var intervalB;
    
            $(document).ready(function () {
    
                $('#btnclick').click(function () {
                    blinkFont();
    
                    setTimeout(function () {
                        clearInterval(intervalA);
                        clearInterval(intervalB);
                    }, 5000);
                });
            });
    
            function blinkFont() {
                document.getElementById("blink").style.color = "red"
                document.getElementById("blink").style.background = "black"
                intervalA = setTimeout("blinkFont()", 500);
            }
    
            function setblinkFont() {
                document.getElementById("blink").style.color = "black"
                document.getElementById("blink").style.background = "red"
                intervalB = setTimeout("blinkFont()", 500);
            }
    
        </script>
    
        <div id="blink" class="live-chat">
            <span>This is blinking text and background</span>
        </div>
    
    0 讨论(0)
  • 2020-12-04 10:19

    This is a custom blink effect I created, which uses setInterval and fadeTo

    HTML -

    <div id="box">Box</div>
    

    JS -

    setInterval(function(){blink()}, 1000);
    
    
        function blink() {
            $("#box").fadeTo(100, 0.1).fadeTo(200, 1.0);
        }
    

    As simple as it gets.

    http://jsfiddle.net/Ajey/25Wfn/

    0 讨论(0)
  • 2020-12-04 10:19

    Try with jquery.blink.js plugin:

    https://github.com/webarthur/jquery-blink

    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="/path/to/jquery.blink.js"></script>
    
    <script>
    jQuery('span').blink({color:'white'}, {color:'black'}, 50);
    </script>
    

    #enjoy!

    0 讨论(0)
提交回复
热议问题