我希望点击空白区域创建<div>;判断<div>区域,当按住<div>移动鼠标时可以拖动<div>,放开鼠标后不允许查看剪贴板
时间: 2023-11-02 17:44:07 浏览: 94
拖拽 拖动div 可拖拽div框 div+css html静态资源
明白了你的需求,以下是更新后的代码,可以实现在空白区域点击创建 `<div>`,同时按住 `<div>` 移动鼠标可以拖动它,放开鼠标后不会查看剪贴板的内容。
```html
<!DOCTYPE html>
<html>
<head>
<title>MathJax Example</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<style>
.mathjax-formula {
font-size: 2.8em;
background-color: green;
position: absolute;
left: 0;
top: 0;
cursor: move;
padding: 5px;
border-radius: 5px;
color: white;
}
</style>
</head>
<body>
<div id="mathjax-formulas-container"></div>
<script>
var formulas = [];
var selectedFormula = null;
var offsetX = 0;
var offsetY = 0;
document.addEventListener("mousedown", function(event) {
if (event.button === 0) {
var target = event.target;
var mathjaxFormulasContainer = document.getElementById("mathjax-formulas-container");
if (target === mathjaxFormulasContainer) {
navigator.clipboard.readText().then(function(text) {
if (text) {
var latexCode = text.trim();
if (latexCode && !selectedFormula) {
var newFormula = document.createElement("div");
newFormula.className = "mathjax-formula";
newFormula.style.left = event.clientX + "px";
newFormula.style.top = event.clientY + "px";
newFormula.innerText = latexCode;
mathjaxFormulasContainer.appendChild(newFormula);
formulas.push({
latexCode: latexCode,
position: {
x: event.clientX,
y: event.clientY
}
});
MathJax.Hub.Queue(["Typeset", MathJax.Hub, newFormula]);
}
}
});
} else if (target.classList.contains("mathjax-formula")) {
selectedFormula = target;
offsetX = event.clientX - selectedFormula.offsetLeft;
offsetY = event.clientY - selectedFormula.offsetTop;
document.addEventListener("mousemove", moveFormula);
}
}
});
function moveFormula(event) {
if (selectedFormula && event.buttons === 1) {
selectedFormula.style.left = event.clientX - offsetX + "px";
selectedFormula.style.top = event.clientY - offsetY + "px";
} else {
selectedFormula = null;
document.removeEventListener("mousemove", moveFormula);
}
}
</script>
</body>
</html>
```
现在,你可以在空白区域点击创建 `<div>`,同时按住 `<div>` 移动鼠标可以拖动它,放开鼠标后不会查看剪贴板的内容。请注意,我在 `mousedown` 事件处理程序中添加了条件判断,以便在点击空白区域时才会读取剪贴板内容。同时,在 `moveFormula` 函数中,我添加了条件判断以确保只有同时按住 `<div>` 并移动鼠标时才会触发拖动操作。感谢你的耐心和理解!
阅读全文