经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS3 » 查看文章
纯 CSS3实现的霓虹灯特效_css3_CSS
来源:jb51  时间:2021/4/19 8:39:47  对本文有异议

这是要实现的效果:

可以看到,在鼠标移入按钮的时候,会产生类似霓虹灯光的效果;在鼠标移出按钮的时候,会有一束光沿着固定的轨迹(按钮外围)运动。

霓虹灯光的实现

霓虹灯光的实现比较简单,用多重阴影来做即可。我们给按钮加三层阴影,从内到外每层阴影的模糊半径递增,这样的多个阴影叠加在一起,就可以形成一个类似霓虹灯光的效果。这段的代码如下:

HTML:

  1. <div class="light">
  2. Neon Button
  3. </div>

CSS:

  1. body {
  2. background: #050901;
  3. }
  4. .light {
  5. width: fit-content;
  6. padding: 25px 30px;
  7. color: #03e9f4;
  8. font-size: 24px;
  9. text-transform: uppercase;
  10. transition: 0.5s;
  11. letter-spacing: 4px;
  12. cursor: pointer;
  13. }
  14. .light:hover {
  15. background-color: #03e9f4;
  16. color: #050801;
  17. box-shadow: 0 0 5px #03e9f4,
  18. 0 0 25px #03e9f4,
  19. 0 0 50px #03e9f4,
  20. 0 0 200px #03e9f4;
  21. }

最终的效果如下:

运动光束的实现

虽然看起来只有一个光束沿着按钮的边缘运动,但实际上这是四个光束沿着不同方向运动之后叠加的效果。它们运动的方向分别是:从左往右、从上往下、从右往左、从下往上,如下图所示:

在这个过程中,光束和光束之间产生了交集,如果只看按钮的边缘部分,就很像是只有一个光束在做顺时针方向的运动。

下面是具体实现中几个需要注意的点:

  • 四个光束分别对应 div.light 的四个子 div,初始位置分别是在按钮的最左侧、最上方、最右侧和最下方,并按照固定的方向做重复的运动
  • 每个光束的高度或宽度都很小(只有 2px),并且都有一个从透明色到霓虹色的渐变,因此外表会有一个收束的效果(即看上去不是一条完整的线条)
  • 为了确保我们看到的是一个顺时针方向的运动,四个光束的运动实际上是有序的,首先是按钮上方的光束开始运动,在一段时间后,右侧的光束运动,在一段时间后,下方的光束运动,在一段时间后,左侧的光束运动。光束和光束之间的运动有一个延迟,以上方和右侧的光束为例,如果它们同时开始运动,由于右侧的运动距离小于上方的运动距离,就会导致这两个光束错过相交的时机,我们看到的就会是断开的、不连贯的光束。既然右侧光束的运动距离比较短,为了让上方光束可以“追上”它,我们就得让右侧光束“延迟出发”,因此要给它一个动画延迟;同理,剩余两个光束也要有一个动画延迟。多个动画延迟之间大概相差 0.25 秒即可。
  • 只需要显示按钮边缘的光束就够了,因此给 div.light 设置一个溢出隐藏

代码如下:

HTML:

  1. <div class="light">
  2. <div></div>
  3. <div></div>
  4. <div></div>
  5. <div></div>
  6. Neon Button
  7. </div>

CSS:

  1. .light {
  2. position: relative;
  3. padding: 25px 30px;
  4. color: #03e9f4;
  5. font-size: 24px;
  6. text-transform: uppercase;
  7. transition: 0.5s;
  8. letter-spacing: 4px;
  9. cursor: pointer;
  10. overflow: hidden;
  11. }
  12. .light:hover {
  13. background-color: #03e9f4;
  14. color: #050801;
  15. box-shadow: 0 0 5px #03e9f4,
  16. 0 0 25px #03e9f4,
  17. 0 0 50px #03e9f4,
  18. 0 0 200px #03e9f4;
  19. }
  20. .light div {
  21. position: absolute;
  22. }
  23. .light div:nth-child(1){
  24. width: 100%;
  25. height: 2px;
  26. top: 0;
  27. left: -100%;
  28. background: linear-gradient(to right,transparent,#03e9f4);
  29. animation: animate1 1s linear infinite;
  30. }
  31. .light div:nth-child(2){
  32. width: 2px;
  33. height: 100%;
  34. top: -100%;
  35. right: 0;
  36. background: linear-gradient(to bottom,transparent,#03e9f4);
  37. animation: animate2 1s linear infinite;
  38. animation-delay: 0.25s;
  39. }
  40. .light div:nth-child(3){
  41. width: 100%;
  42. height: 2px;
  43. bottom: 0;
  44. right: -100%;
  45. background: linear-gradient(to left,transparent,#03e9f4);
  46. animation: animate3 1s linear infinite;
  47. animation-delay: 0.5s;
  48. }
  49. .light div:nth-child(4){
  50. width: 2px;
  51. height: 100%;
  52. bottom: -100%;
  53. left: 0;
  54. background: linear-gradient(to top,transparent,#03e9f4);
  55. animation: animate4 1s linear infinite;
  56. animation-delay: 0.75s;
  57. }
  58. @keyframes animate1 {
  59. 0% {
  60. left: -100%;
  61. }
  62. 50%,100% {
  63. left: 100%;
  64. }
  65. }
  66. @keyframes animate2 {
  67. 0% {
  68. top: -100%;
  69. }
  70. 50%,100% {
  71. top: 100%;
  72. }
  73. }
  74. @keyframes animate3 {
  75. 0% {
  76. right: -100%;
  77. }
  78. 50%,100% {
  79. right: 100%;
  80. }
  81. }
  82. @keyframes animate4 {
  83. 0% {
  84. bottom: -100%;
  85. }
  86. 50%,100% {
  87. bottom: 100%;
  88. }
  89. }

这样就可以达到文章开头图片的效果了。

不同颜色的霓虹灯

如果想要其它颜色的霓虹灯光效果怎么办呢?是否需要把相关的颜色重新修改一遍?其实我们有更简单的方法,就是使用 filter:hue-rotate(20deg) 一次性修改 div.light 和内部所有元素的色相/色调。

The hue-rotate() CSS function rotates the hue of an element and its contents.

最终效果如下:

以上就是纯 CSS3实现的霓虹灯特效的详细内容,更多关于CSS3实现霓虹灯特效的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号