jQuery和CSS 逼真交互式文本阴影

  • 源码大小:7.94KB
  • 所需积分:1积分
  • 源码编号:19JP-3418
  • 浏览次数:626次
  • 最后更新:2023年06月11日
  • 所属栏目:文本
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

使用CSS文本阴影属性和一点jQuery创建一个逼真的交互式阴影,该阴影看起来会落在你的鼠标指针所在的位置,并创建一个文本落在另一个文本后面的错觉。

如何使用它:

1.将文本添加到页面中。

  1. <!-- Shadow Text -->
  2. <div class="shadow-text lighter">
  3. jQuery
  4. </div>
  5. <div class="shadow-text bolder">
  6. jQuery
  7. </div>
  8.  
  9. <!-- Foreground Text -->
  10. <div class="shadow-text text"></div>

2.文本阴影所需的CSS。

  1. .shadow-text {
  2. font-size: 270px;
  3. letter-spacing: 0.2em;
  4. width: 100vw;
  5. position: absolute;
  6. top: 50%;
  7. left: calc(50% + 15px);
  8. transform: translate(-50%, -50%);
  9. display: flex;
  10. align-items: center;
  11. justify-content: center;
  12. }
  13.  
  14. .shadow-text.lighter {
  15. mix-blend-mode: multiply;
  16. z-index: 2;
  17. color: transparent;
  18. opacity: 0.9;
  19. text-shadow: 0px 0px 10px #1e1e1e;
  20. }
  21.  
  22. .shadow-text.bolder {
  23. transform: translate(-50%, -50%) scale(1);
  24. filter: blur(10px);
  25. color: #fff;
  26. mix-blend-mode: multiply;
  27. z-index: 1;
  28. color: transparent;
  29. opacity: 0.35;
  30. }
  31.  
  32. .shadow-text.text {
  33. background-image: url("/path/to/text.png");
  34. background-repeat: no-repeat;
  35. background-position: stretch;
  36. background-size: contain;
  37. z-index: 10;
  38. position: absolute;
  39. top: 50%;
  40. left: 50%;
  41. height: 200px;
  42. width: 864px;
  43. transform: translate(-50%, -50%);
  44. text-shadow: none;
  45. color: #fff;
  46. }

3.在文档末尾加载所需的jQuery库。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>

4.检测鼠标移动事件并根据光标位置将阴影应用于文本的主jQuery脚本。

  1. $(function () {
  2. let $shadow = $(".text");
  3. let $shadowLighter = $(".lighter");
  4. let $shadowBolder = $(".bolder");
  5. //let shadowMax = $(window).innerHeight();
  6. let shadowMax = 450;
  7. let shadowMin = shadowMax * -1;
  8. let shadowMidPoints = [
  9. $shadow.offset().left + $shadow.width() / 2,
  10. $shadow.offset().top + $shadow.height() / 2
  11. ];
  12.  
  13. let shadowSteps = 20;
  14. var shadowBlur = 10;
  15.  
  16. $(document).on("mousemove", (e) => {
  17. let shadowX = Math.min(
  18. shadowMax,
  19. Math.max(shadowMin, shadowMidPoints[0] - e.pageX)
  20. );
  21. let shadowY = Math.min(
  22. shadowMax,
  23. Math.max(shadowMin, shadowMidPoints[1] - e.pageY)
  24. );
  25. var shadowValue;
  26. var shadowValueBolder;
  27. var shadowOpacity;
  28. var shadowOpacityBolder;
  29.  
  30. for (var i = 0; i < shadowSteps; i++) {
  31. var newOffsetX = Math.floor(((shadowX / 2) * i) / 50);
  32. var newOffsetY = Math.floor(((shadowY / 2) * i) / 50);
  33. shadowOpacity = 1 - shadowSteps * i * 0.002;
  34. shadowOpacityBolder = 1 - shadowSteps * i * 0.005;
  35. shadowBlur = i * 1.5;
  36. if (shadowValue === undefined) {
  37. shadowValue = "0px 0px " + 10 + "px rgba(20, 17, 15, 0.3)";
  38. shadowValueBolder = "0px 0px " + 10 + "px rgba(20, 17, 15, 1)";
  39. } else {
  40. shadowValue +=
  41. ", " +
  42. newOffsetX * 2 +
  43. "px " +
  44. newOffsetY * 2 +
  45. "px " +
  46. shadowBlur * 1.25 +
  47. "px rgba(20, 17, 15, " +
  48. shadowOpacity / 5 +
  49. ")";
  50. shadowValueBolder +=
  51. ", " +
  52. newOffsetX * 0.25 +
  53. "px " +
  54. newOffsetY * 0.25 +
  55. "px " +
  56. shadowBlur * 1.5 +
  57. "px rgba(20, 17, 15, " +
  58. shadowOpacityBolder / 5 +
  59. ")";
  60. }
  61. }
  62. $shadowLighter.css("text-shadow", shadowValue);
  63. $shadowBolder.css("text-shadow", shadowValueBolder);
  64. });
  65. });

预览截图