Text blinking jQuery

后端 未结 30 1673
我寻月下人不归
我寻月下人不归 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:48

    Text Blinking start and stop on button click -

    <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-11-27 03:49

    If you'd rather not use jQuery, this can be achieved with CSS3

    @-webkit-keyframes blink {  
      from { opacity: 1.0; }
      to { opacity: 0.0; }
    }
    
    blink {
      -webkit-animation-name: blink;  
      -webkit-animation-iteration-count: infinite;  
      -webkit-animation-timing-function: cubic-bezier(1.0,0,0,1.0);
      -webkit-animation-duration: 1s; 
    }
    

    Seems to work in Chrome, though I thought I heard a slight sobbing noise.

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

    Combine the codes above, I think this is a good solution.

    function blink(selector){
        $(selector).animate({opacity:0}, 50, "linear", function(){
            $(this).delay(800);
            $(this).animate({opacity:1}, 50, function(){
            blink(this);
            });
            $(this).delay(800);
        });
    }
    

    At least it works on my web. http://140.138.168.123/2y78%202782

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

    You can also try these:

    <div>some <span class="blink">text</span> are <span class="blink">blinking</span></div>
    <button onclick="startBlink()">blink</button>
    <button onclick="stopBlink()">no blink</button>
    
    <script>
      function startBlink(){
        window.blinker = setInterval(function(){
            if(window.blink){
               $('.blink').css('color','blue');
               window.blink=false;
             }
            else{
                $('.blink').css('color','white');
                window.blink = true;
            }
        },500);
      }
    
      function stopBlink(){
        if(window.blinker) clearInterval(window.blinker);
      } 
    </script>
    
    0 讨论(0)
  • 2020-11-27 03:51

    Link to author

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
    
    <div id="msg"> <strong><font color="red">Awesome Gallery By Anil Labs</font></strong></p> </div>
    
    <script type="text/javascript" >
    function blink(selector){
        $(selector).fadeOut('slow', function(){
            $(this).fadeIn('slow', function(){
                blink(this);
            });
        });
    }
    
    blink('#msg');
    </script>
    
    0 讨论(0)
  • 2020-11-27 03:51

    I like alex's answer, so this is a bit of an extension of that without an interval (since you would need to clear that interval eventually and know when you want a button to stop blinking. This is a solution where you pass in the jquery element, the ms you want for the blinking offset and the number of times you want the element to blink:

    function blink ($element, ms, times) {
        for (var i = 0; i < times; i++) {
            window.setTimeout(function () {
                if ($element.is(':visible')) {
                    $element.hide();
                } else {
                    $element.show();
                }
            }, ms * (times + 1));
        }
    }
    
    0 讨论(0)
提交回复
热议问题