How do I animate a glowing effect on text?

前端 未结 6 1066
闹比i
闹比i 2021-01-31 10:48

I haven\'t really been able to find any good simple tutorials an animating a glow effect. How do I animate glowing on text?

6条回答
  •  盖世英雄少女心
    2021-01-31 10:48

    If you want to just use CSS3, you don't even have to use any jQuery/JavaScript. Just do this in your CSS:

    .confirm_selection {
        -webkit-transition: text-shadow 0.2s linear;
        -moz-transition: text-shadow 0.2s linear;
        -ms-transition: text-shadow 0.2s linear;
        -o-transition: text-shadow 0.2s linear;
        transition: text-shadow 0.2s linear;
    }
    .confirm_selection:hover {
        text-shadow: 0 0 10px red; /* replace with whatever color you want */
    }
    

    Here's the fiddle: http://jsfiddle.net/zenjJ/

    If you want the element to run on its own (without hovering), do this:

    CSS:

    .confirm_selection {
        -webkit-transition: text-shadow 1s linear;
        -moz-transition: text-shadow 1s linear;
        -ms-transition: text-shadow 1s linear;
        -o-transition: text-shadow 1s linear;
        transition: text-shadow 1s linear;
    }
    .confirm_selection:hover,
    .confirm_selection.glow {
        text-shadow: 0 0 10px red;
    }
    

    JavaScript:

    var glow = $('.confirm_selection');
    setInterval(function(){
        glow.toggleClass('glow');
    }, 1000);
    

    You can play around with the timing in the CSS/JavaScript to get the exact effect you're looking for.

    And finally, here's the fiddle: http://jsfiddle.net/dH6LS/


    Update Oct. 2013: being that all major browsers now support CSS animations, all you need is this:

    .confirm_selection {
        animation: glow .5s infinite alternate;
    }
    
    @keyframes glow {
        to {
            text-shadow: 0 0 10px red;
        }
    }
    
    .confirm_selection {
        font-family: sans-serif;
        font-size: 36px;
        font-weight: bold;
    }
    
    [ Confirm Selection ]
    

    Here's the fiddle: http://jsfiddle.net/dH6LS/689/

    Don't forget to include all the different vendor prefixes in production.

提交回复
热议问题