html - 可拖动的 HTML 弹出窗口

标签 html css

https://jsfiddle.net/Lv0pusah/6/

尝试使弹出窗口可拖动,以便它可以在屏幕上四处移动。目前它不会停留在被拖到的位置。可能吗?

HTML:

<body>
<h2>Popup</h2>
<button id="myBtn">Open popup</button>
<div id="myModal"  class="modal">
  <div class="modal-content" draggable="true" style="width:400px">
    <div class="modal-header" >
      <span class="close">&times;</span>
    </div>
    <div class="modal-body">
      <p>Some text in popup</p>
      <p>Some other text...</p>
    </div>
      </div>
</div>
</body>

Javascript:

var modal = document.getElementById('myModal');
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
    modal.style.display = "block";
}
span.onclick = function() {
    modal.style.display = "none";
}
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

最佳答案

您必须删除draggable 属性

var modal = document.getElementById('myModal'),btn = document.getElementById("myBtn"),span = document.getElementsByClassName("close")[0];
btn.onclick = function() {modal.style.display = "block"}
span.onclick = function() {modal.style.display = "none"}
window.onclick = function(event) {if (event.target == modal) {modal.style.display = "none"}}

//Get the item to move
var modalcontent = document.getElementsByClassName("modal-content")[0],
  startTop = 0,
  startLeft = 0;
function move(){
  //It is important that the element is in absolute position
  modalcontent.style.position="absolute";
  var event = arguments[0];
  modalcontent.style.top=event.pageY-startTop+"px";
  modalcontent.style.left=event.pageX-startLeft+"px";
}
//Get initial values when the mouse is pressed
modalcontent.addEventListener("mousedown", function(e){
  startTop = e.pageY - modalcontent.offsetTop;
  startLeft = e.pageX - modalcontent.offsetLeft;
  //Start the movement
  document.addEventListener("mousemove", move);
});
//When the mouse is released
document.addEventListener("mouseup", function(){
  //Stop motion
  document.removeEventListener("mousemove", move);
});
.modal {display: none;/* Hidden by default */position: fixed;/* Stay in place */z-index: 1;/* Sit on top */padding-top: 100px;/* Location of the box */left: 0;top: 0;width: 100%;/* Full width */height: 100%;/* Full height */overflow: auto;/* Enable scroll if needed */background-color: rgb(0, 0, 0);/* Fallback color */background-color: rgba(0, 0, 0, 0.4);/* Black w/ opacity */}/* Modal Content */.modal-content {position: relative;background-color: #fefefe;margin: auto;padding: 0;border: 2px solid gray;width: 80%;box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);-webkit-animation-name: animatetop;-webkit-animation-duration: 0.4s;animation-name: animatetop;animation-duration: 0.4s}/* Add Animation */@-webkit-keyframes animatetop {from {top: -300px;opacity: 0}to {top: 0;opacity: 1}}@keyframes animatetop {from {top: -300px;opacity: 0}to {top: 0;opacity: 1}}/* The Close Button */.close {color: white;float: right;font-size: 28px;font-weight: bold;}[draggable=true] {cursor: move;}.close:hover,.close:focus {color: #000;text-decoration: none;cursor: pointer;}.modal-header {padding: 2px 16px;background-color: green;color: white;height: 30px;}.modal-body {padding: 2px 16px;}.modal-footer {padding: 2px 16px;background-color: #5cb85c;color: white;}
<h2>Popup</h2><button id="myBtn">Open popup</button><div id="myModal" class="modal"><div class="modal-content" style="width:400px"><div class="modal-header"><span class="close">&times;</span></div><div class="modal-body"><p>Some text in popup</p><p>Some other text...</p></div></div></div>

关于html - 可拖动的 HTML 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102349/

相关文章:

javascript - 将事件添加到 HTML 输入数组的所有元素并获取数组中的值

jquery - 是否可以构建一个 HTML 页面,其中并非所有元素都可以通过 CSS 选择器/XPath 访问?

javascript - 从表中选择一个单元格并为其指定唯一的类

javascript - 在链接上悬停操作后图像叠加悬停失败

javascript - 如何使用 jQuery 实时检查 html 中的元素插入情况。

html - 在 Bootstrap 中居中一个 4 列的 Div

html - Sublime Text 2 - HTML 自动缩进问题

jquery - 单页应用程序 (SPA) 中的不同 JavaScript 库

IE 上的 CSS 视觉错误,带有 wordpress 主题

html - 元素周围的虚线边框,左边框的顶部倾斜/成 Angular