【问题标题】:How do I animate a glowing effect on text?如何为文本设置发光效果的动画?
【发布时间】:2021-12-02 08:20:59
【问题描述】:

我真的找不到任何好的简单教程来制作发光效果的动画。如何在文本上设置发光动画?

【问题讨论】:

  • 我们需要的不仅仅是“动画发光效果”。你想让这个动画看起来像什么?改变颜色?改变发光半径?
  • 任何东西,一旦我开始,我可以从那里调整它。因为它只是 CSS 属性。
  • 好吧,我想我应该指定我的意思是 CSS3 属性,更具体地说是 text/box-shadow。我只是假设当人们想到发光时,他们会想到有光环的东西......就像辐射它的一部分能量。哎呀。

标签: css


【解决方案1】:

如果您只想使用 CSS3,您甚至不必使用任何 jQuery/JavaScript。只需在您的 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 */
}

这是小提琴:http://jsfiddle.net/zenjJ/

如果您希望元素自行运行(不悬停),请执行以下操作:

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);

您可以使用 CSS/JavaScript 中的时间来获得您正在寻找的确切效果。

最后,这是小提琴:http://jsfiddle.net/dH6LS/


2013 年 10 月更新:现在所有主流浏览器都支持 CSS 动画,您只需要这样:

.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;
}
<span class="confirm_selection">
[ Confirm Selection ]
</span>

这是小提琴:http://jsfiddle.net/dH6LS/689/

不要忘记在生产中包含所有不同的供应商前缀。

【讨论】:

  • 尽管 13 年 10 月更新可能是现代浏览器的首选方法,但跨浏览器方法规则。 setInterval 循环获胜!
  • 伟大的更新。非常适合我的“脉冲”文本效果。我使用了 ease-in-out 和轻微的延迟,但基于此。
  • 即使您确实使用了transition,您也不必为其添加供应商前缀:shouldiprefix.com/#transitions
【解决方案2】:

您可以使用.animate 循环到选定的颜色以创建“高光”效果,这大概就是“发光”的意思。

$(".confirm_selection").hover(function() {
    $(this).animate({
        color: "red"
    }, 2000);
}, function() {
    $(this).animate({
        color: "black"
    }, 2000);
});

You can try it here.

注意:彩色动画需要使用彩色动画插件或 jQuery UI(我假设您已经在使用它,因为它已包含在您的小提琴中)。

【讨论】:

  • 别忘了jQuery本身并不支持彩色动画。您必须使用 jQuery UI 或彩色动画插件。
  • 您实际上可以使用 CSS3 过渡来制作动画。
  • css3 属性呢?我可以为文本阴影设置动画吗?
  • 发光的定义:thefreedictionary.com/glowing发光。我以为这是众所周知的。 =D
  • @TheLindyHop - 我对“发光元素”的理解恰好是一种会变亮,然后变暗的元素。 :D
【解决方案3】:

您可以使用纯 CSS3 过渡来做到这一点:

div.transition{
    text-decoration: none;  
    color: blue;  
    text-shadow: none;  
    -webkit-transition: 500ms linear 0s;  
    -moz-transition: 500ms linear 0s;  
    -o-transition: 500ms linear 0s;  
    transition: 500ms linear 0s;  
    outline: 0 none; 
    background-color:#000; 
    font-size:2em;
    width:300px;     
    height:100px; 
    padding:1em;
}

div.transition:hover {
    color: #fff;  
    text-shadow: -1px 1px 8px #ffc, 1px -1px 8px #fff; 
}

示例:http://jsfiddle.net/jasongennaro/z5jCB/1/

注意:发光文本在深色背景上效果最佳。

灵感(以及大部分代码)来自这里:http://www.sitepoint.com/css3-glowing-link-effect/

【讨论】:

  • 这很性感。有没有办法让它脉搏?喜欢...做没有 :hover 的动画?
【解决方案4】:

试试这个代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function ()
{
    var glow = $('h1');
    setInterval(function()
    {
        glow.toggleClass('glow');
    }, 1000);
});
</script>
<style type="text/css">
    h1 {
        -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;
        width: 725px;
    }
    h1:hover,
    h1.glow
    {
        text-shadow: 0 0 10px Green;
    }
</style>
</head>

<body>
    <h1>This text has Glow Animation Effect</h1>
    <div class="buttons">
        <input type="text" name="txt"> <input type="button" class="btnAdd" value="Add"><br>
    </div>
    <h3>J Query</h3>
</body>

</html>

【讨论】:

    【解决方案5】:

    这是一个纯 CSS 动画示例:

    @keyframes shadow {
      0% {
        text-shadow: 5px 5px 10px gray;
      }
      25% {
        text-shadow: -5px 5px 10px gray;
      }
      50% {
        text-shadow: -5px -5px 10px gray;
      }
      75% {
        text-shadow: 5px -5px 10px gray;
      }
    }
    
    h1 {
      text-shadow: 5px 5px 10px gray;
      animation: shadow 2s infinite ease;
    }
    

    https://codepen.io/juanbrujo/pen/abyoXNY

    【讨论】:

    • 不是我的一杯茶,但值得一票,包括 .gif 这可以节省人们将代码放入自己查看效果的时间。
    【解决方案6】:

    这是我过去发现的一个动画效果,它通过向 jQuery fx 对象添加一个新的步进函数很有用。

    $.fx.step.myBlurRedEffect = function (fx) {
        $(fx.elem).css({
            textShadow: '0 0 ' + Math.floor(fx.now) + 'px #FF0000'
        });
    }
    

    以通常的方式调用(比如在半秒内模糊 6px):

    $('#myID').animate({
        myBlurRedEffect: 6
    },{
        duration: 500
    });
    

    http://jsfiddle.net/whLMZ/1/

    这应该让你开始,你应该找到许多有趣的方法来扩展它,添加其他 CSS 参数等...:)

    【讨论】:

    • @TheLindyHop 我添加了一个工作版本。您将 CSS 属性设置为无效值“text-shadow:4 4 [x]px #FFFFFF”(4 什么?)。
    • @TheLindyHop 又放出来了http://jsfiddle.net/whLMZ/1/
    【解决方案7】:

    没有人提到可以动画发光效果摆脱 javascript 的 css3 关键帧(这来自 javascript 开发人员)。

        <!DOCTYPE html>
        <html>
        <head>
        <style type="text/css">
            :root { --color: blue; }
            span.vjs { --color: #F3E5AB; }
            span.cv { --color: skyblue; }
            body {background:gray;}
            .vjs, .cv, h1 {
              text-align: center;
              font-size: 30pt;
              font-weight: 100;
              font-family: cursive2;
            }
            .vjs {
              text-shadow: 0 0 2px #333333, 0 0 4px #333333;
              color: var(--color);
            }
            .cv {
              text-shadow: 0 0 2px #fff, 0 0 4px #fff;
              color: #2965f1;
            }
            h1 {
              text-shadow: 0 0 2px #fff, 0 0 4px #fff;
              color:black;
            }
            .cv:hover, .vjs:hover, h1:hover {
                animation: glow 1s linear infinite;
                color: var(--color);
            }
            @keyframes glow {
                0% {text-shadow: 0 0 0px var(--color);}
                50% {text-shadow: 0 0 10px var(--color);}
                100% {text-shadow: 0 0 0px var(--color);}
            }
        </style>
        <script src="vanilla-js.com/lib/vanilla.4.1.min.js"></script>
        </head>
    
        <body>
        <h1>This text has Real Glow Animation Effect with 
    <span class="vjs">Vanilla JS</span>And <span class="cv">CSS Variables</span>. Hover over all the texts!</h1>
        </body>
    
        </html>
    

    一个jsfiddle

    【讨论】:

      【解决方案8】:

      我发现了这个on-line,并且只花了几分钟就完成了设置:

      // Neon glowing Text: https://www.w3schools.com/howto/howto_css_glowing_text.asp
      .glow {
        // font-size: 80px; // Way TOO BIG
        color: #fff;
        text-align: center;
        -webkit-animation: glow 1s ease-in-out infinite alternate;
        -moz-animation: glow 1s ease-in-out infinite alternate;
        animation: glow 1s ease-in-out infinite alternate;
      }
      
      @-webkit-keyframes glow {
        from {
          text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #e60073, 0 0 40px #e60073, 0 0 50px #e60073, 0 0 60px #e60073, 0 0 70px #e60073;
        }
        to {
          text-shadow: 0 0 20px #fff, 0 0 30px #ff4da6, 0 0 40px #ff4da6, 0 0 50px #ff4da6, 0 0 60px #ff4da6, 0 0 70px #ff4da6, 0 0 80px #ff4da6;
        }
      }
      

      我不喜欢这些颜色,会把它们改成火红色(如果我能弄清楚代码的话):

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 2018-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多