Text blinking jQuery

后端 未结 30 1672
我寻月下人不归
我寻月下人不归 2020-11-27 03:23

What is an easy way to make text blinking in jQuery and a way to stop it? Must work for IE, FF and Chrome. Thanks

相关标签:
30条回答
  • 2020-11-27 03:52
    $.fn.blink = function(times, duration) {
        times = times || 2;
        while (times--) {
            this.fadeTo(duration, 0).fadeTo(duration, 1);
        }
        return this;
    };
    
    0 讨论(0)
  • 2020-11-27 03:52

    Blink functionality can be implemented by plain javascript, no requirement for jquery plugin or even jquery.

    This will work in all the browsers, as it is using the basic functionality

    Here is the code

    HTML:

    <p id="blinkThis">This will blink</p>
    

    JS Code:

    var ele = document.getElementById('blinkThis');
    setInterval(function () {
        ele.style.display = (ele.style.display == 'block' ? 'none' : 'block');
    }, 500);
    

    and a working fiddle

    0 讨论(0)
  • 2020-11-27 03:53

    This stand-alone solution will blink the text a specified number of times and then stop.

    The blinking uses opacity, rather than show/hide, fade or toggle so that the DIV remains clickable, in case that's ever an issue (allows you to make buttons with blinking text).

    jsFiddle here (contains additional comments)

    <html>
        <head>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
            <script type="text/javascript">
                $(document).ready(function() {
    
                    var init = 0;
    
                    $('#clignotant').click(function() {
                        if (init==0) {
                            init++;
                            blink(this, 800, 4);
                        }else{
                            alert('Not document.load, so process the click event');
                        }
                    });
    
                    function blink(selector, blink_speed, iterations, counter){
                        counter = counter | 0;
                        $(selector).animate({opacity:0}, 50, "linear", function(){
                            $(this).delay(blink_speed);
                            $(this).animate({opacity:1}, 50, function(){
    
                                counter++;
    
                                if (iterations == -1) {
                                    blink(this, blink_speed, iterations, counter);
                                }else if (counter >= iterations) {
                                    return false;
                                }else{
                                    blink(this, blink_speed, iterations, counter);
                                }
                            });
                            $(this).delay(blink_speed);
                        });
                    }
    
                    //This line must come *AFTER* the $('#clignotant').click() function !!
                    window.load($('#clignotant').trigger('click'));
    
    
                }); //END $(document).ready()
    
            </script>
        </head>
    <body>
    
        <div id="clignotant" style="background-color:#FF6666;width:500px;
        height:100px;text-align:center;">
            <br>
            Usage: blink(selector, blink_speed, iterations) <br />
            <span style="font-weight:bold;color:blue;">if iterations == -1 blink forever</span><br />
            Note: fn call intentionally missing 4th param
        </div>
    
    
    </body>
    </html>
    

    Sources:
    Danny Gimenez
    Moses Christian

    0 讨论(0)
  • 2020-11-27 03:55

    this code is work for me

       $(document).ready(function () {
            setInterval(function(){
                $(".blink").fadeOut(function () {
                    $(this).fadeIn();
                });
            } ,100)
        });
    
    0 讨论(0)
  • 2020-11-27 03:56

    This code will effectively make the element(s) blink without touching the layout (like fadeIn().fadeOut() will do) by just acting on the opacity ; There you go, blinking text ; usable for both good and evil :)

    setInterval(function() {
      $('.blink').animate({ opacity: 1 }, 400).animate({ opacity: 0 }, 600);
    }, 800);
    
    0 讨论(0)
  • 2020-11-27 03:57

    I was going to post the steps-timed polyfill, but then I remembered that I really don’t want to ever see this effect, so…

    function blink(element, interval) {
        var visible = true;
    
        setInterval(function() {
            visible = !visible;
            element.style.visibility = visible ? "visible" : "hidden";
        }, interval || 1000);
    }
    
    0 讨论(0)
提交回复
热议问题