• 助力高考,一组彩色的文字


    1、获取文本内容

    首先,获取每个

    元素的文本内容,并清空其内部HTML(innerHTML = "")。

    2、创建元素

    然后,它遍历文本的每个字符,为每个字符创建一个新的元素,并将该字符设置为元素的文本内容。

    3、设置样式

    为每个元素设置外边距(marginRight),这样字符之间会有一定的间隔。

    4、打乱顺序

    使用shuffle函数随机打乱元素的顺序。这个函数通过Fisher-Yates算法实现随机置换。

    5、应用随机变换

    applyRandomTransform函数为每个元素随机生成位移(xOffset和yOffset)和旋转角度(rotation),并应用CSS的transform属性来改变每个字符的位置和方向。

    6、随机颜色变化

    changeColorSequentially函数逐个改变元素的颜色,每次只改变一个字符的颜色,并在颜色变化时保持顺序感。它使用getRandomColor函数生成随机颜色代码。

    7、定时器

    通过setInterval函数,changeColorSequentially和applyRandomTransform函数被定期调用,使得颜色和变换效果持续发生,创建动态变化的效果。

    8、文本描边和阴影

    在CSS中,使用-webkit-text-stroke-width和-webkit-text-stroke-color为文本添加描边效果,使用text-shadow添加阴影效果,增强文本的视觉效果。

    1. <template>
    2. <div>
    3. <!-- <img alt="Vue logo" src="./assets/logo.png"> -->
    4. <HelloWorld msg="Welcome to Your Vue.js App" />
    5. <!-- <router-view /> -->
    6. <div class="bodys">
    7. <div class="rugrats">高考加油!</div>
    8. <div class="rugrats">榜上有名!</div>
    9. <div class="rugrats">繁花似锦!</div>
    10. </div>
    11. </div>
    12. </template>
    13. <script>
    14. import HelloWorld from "./components/HelloWorld.vue";
    15. export default {
    16. name: "App",
    17. components: {
    18. HelloWorld,
    19. },
    20. mounted() {
    21. this.init();
    22. },
    23. methods: {
    24. init() {
    25. const divs = document.querySelectorAll(".rugrats");
    26. divs.forEach(function (div) {
    27. const text = div.textContent;
    28. div.innerHTML = "";
    29. for (let i = 0; i < text.length; i++) {
    30. const span = document.createElement("span");
    31. span.textContent = text[i];
    32. span.style.marginRight = "1vw";
    33. div.appendChild(span);
    34. }
    35. let spans = div.querySelectorAll("span");
    36. function shuffle(array) {
    37. for (let i = array.length - 1; i > 0; i--) {
    38. const j = Math.floor(Math.random() * (i + 1));
    39. const temp = array[i];
    40. array[i] = array[j];
    41. array[j] = temp;
    42. }
    43. return array;
    44. }
    45. spans = shuffle(Array.from(spans));
    46. function getRandomValue() {
    47. return Math.random() * 0.4 - 0.24;
    48. }
    49. function applyRandomTransform() {
    50. spans.forEach(function (span) {
    51. const xOffset = getRandomValue() * 10;
    52. const yOffset = getRandomValue() * 15;
    53. const rotation = getRandomValue() * 6;
    54. span.style.transform =
    55. "translate(" +
    56. xOffset +
    57. "px, " +
    58. yOffset +
    59. "px) rotate(" +
    60. rotation +
    61. "deg)";
    62. span.style.textIndent = xOffset + "px";
    63. });
    64. }
    65. function getRandomColor() {
    66. const letters = "0123456789ABCDEF";
    67. let color = "#";
    68. for (var i = 0; i < 6; i++) {
    69. color += letters[Math.floor(Math.random() * 16)];
    70. }
    71. return color;
    72. }
    73. let currentIndex = 0;
    74. function changeColorSequentially() {
    75. spans.forEach(function (span, index) {
    76. let colorIndex = (index + currentIndex) % spans.length;
    77. span.style.color =
    78. colorIndex === 0
    79. ? getRandomColor()
    80. : spans[colorIndex - 1].style.color;
    81. });
    82. currentIndex = (currentIndex + 1) % spans.length;
    83. }
    84. setInterval(changeColorSequentially, 250);
    85. setInterval(applyRandomTransform, 100);
    86. });
    87. },
    88. },
    89. };
    90. </script>
    91. <style>
    92. #app {
    93. font-family: Avenir, Helvetica, Arial, sans-serif;
    94. -webkit-font-smoothing: antialiased;
    95. -moz-osx-font-smoothing: grayscale;
    96. text-align: center;
    97. color: #2c3e50;
    98. margin-top: 60px;
    99. }
    100. .bodys {
    101. margin: 10vh;
    102. text-align: center;
    103. font-size: calc(5vw + 4vh);
    104. background: #314d79;
    105. color: #fff;
    106. font-family: sans-serif;
    107. letter-spacing: -0.3vw;
    108. overflow: hidden;
    109. font-family: "Lacquer", system-ui;
    110. }
    111. .rugrats {
    112. margin: 0 auto;
    113. text-align: center;
    114. }
    115. .rugrats span {
    116. display: inline-block;
    117. transition: color linear 0.5s forwards;
    118. -webkit-text-stroke-width: 0.32vw;
    119. -webkit-text-stroke-color: black;
    120. text-shadow: 0.4vw 0.5vw #000;
    121. }
    122. .rugrats span {
    123. text-transform: capitalize;
    124. text-transform: lowercase;
    125. }
    126. </style>

  • 相关阅读:
    eBPF理解 (一)
    HTTP 协议请求头 If-Match、If-None-Match 和 ETag
    C++——string
    CS144 计算机网络 Lab0:Networking Warmup
    数据库主键一定要自增的吗?有哪些场景下不建议自增?
    奇淫巧技,CompletableFuture 异步多线程是真的优雅
    YOLOX 学习笔记
    【跟小嘉学习区块链】二、Hyperledger Fabric 架构详解
    网址导航收藏引导页面H5源码(自适应引导页HTML源码)-自动检测域名延迟
    华为电量分段图表实现过程
  • 原文地址:https://blog.csdn.net/nss33678/article/details/139580980