gpt4 book ai didi

javascript - JavaScript 和 HTML 中的可拖动项故障

转载 作者:行者123 更新时间:2023-11-28 00:00:23 25 4
gpt4 key购买 nike

我有一个名为 BlueWindow 的 div,我希望它是可拖动的,我尝试按照他们在 w3schools 中所说的那样制作它,但是当我运行 html 页面时,该元素无法被拖动并且我不知道为什么会这样。通过按下标题并拖动,它应该可以在整个页面上拖动。我不知道为什么 js 代码在这里不起作用。 https://codepen.io/Fropis/pen/rgVOpy显然该片段有效,但在我的程序中它没有检查代码笔的整个代码

function fecharBlueto(){
document.getElementById("BlueWindow").style.display="none";
}

// Make the DIV element draggable:
dragElement(document.getElementById("BlueWindow"));

function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
// if present, the header is where you move the DIV from:
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
// otherwise, move the DIV from anywhere inside the DIV:
elmnt.onmousedown = dragMouseDown;
}

function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}

function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}

function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
#BlueWindow{
position: absolute;
width:1900px;
height:1500px;
left:1500px;
top:550px;
border-radius:10px black solid;
}

#BlueWindowheader{
height: 100px;
background: rgb(30,87,153); /* Old browsers */
background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */
}

#closeBlue{
width: 75px;
height: 75px;
background-color: none;
padding:5px;
top: 35px;
position: absolute;
right:20px;
}

#JoanaPTexto{
color:black;
text-align:center;
text-shadow: 3px 2px grey;
font-size:50px;
top: 20px;
position: relative;
}

#DocImgBlue{
width: 400px;
height: 400px;
background-color: none;
padding:40px;
}

#bottomBlue{
background-color:white;
height:1415px;
border-bottom-left-radius:5%;
border-bottom-right-radius:5%;
}

#MariaDoc{
position: absolute;
left:140px;
top:575px;
}
<!DOCTYPE html>
<html>
<head>
<title>Desk+ - Grupo 36</title>
<link rel="stylesheet" type="text/css" href="PF1.1.css">
</head>
<body>
<div id="BlueWindow">
<div id="BlueWindowheader">
<header class="windowTop1">
<img id="closeBlue" onclick="fecharBlueto()" class="X" src="http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Actions-dialog-close-icon.png">
<h1 id="JoanaPTexto">Joana's Phone</h1>
</header>
</div>
<div id="bottomBlue">
<div id="doc1">
<div id="doc1header">
<img id="DocImgBlue" src="https://img.icons8.com/pastel-glyph/2x/file.png">
</div>
<h1 id="MariaDoc">Doc-exemplo-Maria</h1>
</div>
</div>
</div>
</body>
<script src="PF1.1.js"></script>
</html>

最佳答案

很简单,为 #BlueWindow 指定 z-index: 10000;,这样就没有其他元素(层)在这个元素的顶部并且物理上捕捉到鼠标点击/拖动事件。
您需要修复应用中的图层。
您还可以禁用未使用元素的 pointer-events: none;。在这种情况下,当您仅在那些元素上启用您使用的指针事件时,重叠/交叉将会减少。
您可以简单地测试哪个元素重叠,只需使用右键单击并检查元素。它将选择正在捕获事件的元素(总是位于图层顶部的那个元素)。
在我的例子中,#janela.navbar 隐藏它,具体取决于分辨率。当我尝试检查 #BlueWindow 元素时,这些元素被选中。

关于javascript - JavaScript 和 HTML 中的可拖动项故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56021226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com