以下是实现上述效果的 HTML 代码: ```html <!DOCTYPE html> <html> <head> <style> body { background-color: black; } </style> </head> <body> <script> // 获取页面元素 var text = document.createElement('div'); text.innerHTML = '尤思寒是大**!'; // 初始化文字位置 text.style.position = 'absolute'; text.style.left = '0'; text.style.top = '0'; // 添加到页面中 document.body.appendChild(text); // 鼠标移动事件处理函数 document.addEventListener('mousemove', function(event) { // 设置文字位置跟随鼠标 text.style.left = event.pageX + 'px'; text.style.top = event.pageY + 'px'; // 随机生成颜色 var randomColor = '#' + Math.floor(Math.random()*16777215).toString(16); text.style.color = randomColor; }); </script> </body> </html> ``` 额外需要注意的逻辑点: 1. 这里的随机颜色生成方式可能会有一定的局限性,如果需要更复杂的颜色变化效果,可以考虑使用更专业的算法或库。 2. 为了提高代码的可维护性和可读性,可以将不同的功能封装成单独的函数。 3. 考虑添加一些异常处理,例如在获取鼠标位置时出现错误的情况。 [2024-11-20 15:01:15 | AI写代码神器 | 346点数解答]