我很困惑,我已经连续盯着这个代码一个小时了
我想制作一个可拖动的弹出窗口(得到那部分),但我希望它能够关闭和打开?我希望按钮位于可拖动元素内。这就是我到目前为止所得到的,任何事情都会非常有帮助,因为我不知道我做错了什么:,D
ps!我只能使用 html+css+javascript,不能使用 jquery :(
var mydragg = function() {
return {
move: function(divid, xpos, ypos) {
divid.style.left = xpos + 'px';
divid.style.top = ypos + 'px';
},
startMoving: function(divid, container, evt) {
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
divTop = divid.style.top,
divLeft = divid.style.left,
eWi = parseInt(divid.style.width),
eHe = parseInt(divid.style.height),
cWi = parseInt(document.getElementById(container).style.width),
cHe = parseInt(document.getElementById(container).style.height);
document.getElementById(container).style.cursor = 'move';
divTop = divTop.replace('px', '');
divLeft = divLeft.replace('px', '');
var diffX = posX - divLeft,
diffY = posY - divTop;
document.onmousemove = function(evt) {
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
aX = posX - diffX,
aY = posY - diffY;
if (aX < 0) aX = 0;
if (aY < 0) aY = 0;
if (aX + eWi > cWi) aX = cWi - eWi;
if (aY + eHe > cHe) aY = cHe - eHe;
mydragg.move(divid, aX, aY);
}
},
stopMoving: function(container) {
var a = document.createElement('script');
document.getElementById(container).style.cursor = 'default';
document.onmousemove = function() {}
},
}
}();
function close() {
document.getElementById('elem').style.display = 'none';
}
.container {
position: absolute;
background-color: blue;
}
.elem {
position:absolute;
background-color: green;
-webkit-user-select: none;
-moz-user-select: none;
-o-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
<div class="container" id='container' style="width: 600px;height: 400px;top:50px;left:50px;">
<div class="elem" id="elem" onmousedown='mydragg.startMoving(this,"container",event);' onmouseup='mydragg.stopMoving("container");' style="width: 200px;height: 100px;">
<button onClick="close()"> x </button>
</div>
</div>
close
关键字是保留的,这就是为什么不调用该函数的原因。您需要更改函数名称。
var mydragg = function() {
return {
move: function(divid, xpos, ypos) {
divid.style.left = xpos + 'px';
divid.style.top = ypos + 'px';
},
startMoving: function(divid, container, evt) {
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
divTop = divid.style.top,
divLeft = divid.style.left,
eWi = parseInt(divid.style.width),
eHe = parseInt(divid.style.height),
cWi = parseInt(document.getElementById(container).style.width),
cHe = parseInt(document.getElementById(container).style.height);
document.getElementById(container).style.cursor = 'move';
divTop = divTop.replace('px', '');
divLeft = divLeft.replace('px', '');
var diffX = posX - divLeft,
diffY = posY - divTop;
document.onmousemove = function(evt) {
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
aX = posX - diffX,
aY = posY - diffY;
if (aX < 0) aX = 0;
if (aY < 0) aY = 0;
if (aX + eWi > cWi) aX = cWi - eWi;
if (aY + eHe > cHe) aY = cHe - eHe;
mydragg.move(divid, aX, aY);
}
},
stopMoving: function(container) {
var a = document.createElement('script');
document.getElementById(container).style.cursor = 'default';
document.onmousemove = function() {}
},
}
}();
function closePopup() {
document.getElementById('elem').style.display = 'none';
}
function openPopup() {
document.getElementById('elem').style.display = 'block';
}
.container {
position: absolute;
background-color: blue;
}
.elem {
position:absolute;
background-color: green;
-webkit-user-select: none;
-moz-user-select: none;
-o-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
<div class="container" id='container' style="width: 600px;height: 400px;top:50px;left:50px;">
<div class="elem" id="elem" onmousedown='mydragg.startMoving(this,"container",event);' onmouseup='mydragg.stopMoving("container");' style="width: 200px;height: 100px;">
<button onClick="closePopup()"> x </button>
</div>
</div>
<button onclick="openPopup()">Open Popup</button>