javascript - 通过鼠标移动在屏幕上书写特定文本
问题描述
我想当我移动鼠标时在屏幕上写下特定的文字
例如:“你好世界”
我有代码,但他不是我想要的
脚本--->
window.focus();
const output = document.querySelector('output');
let text = 'A';
window.addEventListener('mousemove', ({
x,
y
}) => {
if (x > 100) text = 'W';
if (x > 400) text = 'X';
if (y > 100) text = 'B';
if (y > 400) text = 'J';
output.textContent += text;
});
请帮我!!
解决方案
请注意,我更改了 for,document.querySelector('#output');
因为我使用了 id 为“输出”的 div。如果您使用并<output>
标记 (?),请将其保留在原始代码中。
这里字母会随着页面滚动而移动,如果您想在滚动时保持在同一位置,请position: static;
改用。
const output = document.querySelector('#output');
let text = 'Hello World!';
window.addEventListener('mousemove', ({
x,
y
}) => {
output.innerHTML += '<div style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;">' + text + '</div>';
});
<div id="output" style="width: 100%; height: 100%;"></div>
推荐阅读
- model-view-controller - pecl:找不到命令
- android-studio - 如何在 Android Studio 中将代码粘贴到光标位置?
- node.js - TypeError:无法读取未定义的属性“发送”-discord.js
- javascript - 如何将按钮 ID 的值传递给 GET 请求?
- python - 比较 2 个 DataFrame 的半匹配行
- java - 将双向链表中的元素复制到单链表
- git - 将git目录放在驱动器同步工具中是否危险
- python - 在python中计算y的循环函数
- java - Confirm subscriptions error in Google purchase library
- python - 更改特定标签文本