如何设置可拖动div的开始位置?

时间:2018-09-07 01:52:04

标签: javascript css position draggable

我有一个可拖动的div,只要我不通过top, right, … CSS更改其位置,它就可以正常工作。我的目标是将div的预拖动位置设置为其窗口的右上角,这是通过top = 0, right = 0完成的;问题是div会停留在该位置,而stretches会在拖动时停留。

dragElement(document.getElementById("myModal"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
  document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;} 
  else {elmnt.onmousedown = dragMouseDown;}

  function dragMouseDown(w) {
    w = w || window.event;
    w.preventDefault();
    pos3 = w.clientX;
    pos4 = w.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;}
    
  function elementDrag(w) {
    w = w || window.event;
    w.preventDefault();
    pos1 = pos3 - w.clientX;
    pos2 = pos4 - w.clientY;
    pos3 = w.clientX;
    pos4 = w.clientY;
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";}

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;}}
#myModal {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
    top: 0;
    right: 0;}
#myModalheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;}
<div id="myModal">
  <div id="myModalheader">Click here to move</div>
  <p>Move</p><p>this</p><p>DIV</p>
</div>

有任何解决方法吗?感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

给元素一个宽度和高度,你应该很好。

library(rio)
library(data.table)
library(dplyr)

PATH <- "~/CASCDE.xlsx"
names(PATH) <- basename(PATH)
DT <- rbindlist(lapply(PATH,import,col_names = FALSE,
                       col_types = "text"),
                  idcol = "source",fill = TRUE)
# Shift all NAME info in one column
 # Cannot use NA instead of "",because R recognize many EXCEL blank as "" not NA 
DT2 <- DT[,NAME_INFO := ifelse(!X__3 == "",X__3,
                                    ifelse(!X__5 == "",X__5,
                                           ifelse(!X__7 == "",X__7,
                                                  "Nothing")))]
# Reshape all GROUP info to another columns
DT2[,`:=`(GROUP = ifelse(!X__2 == "",X__2,NA),
           TEAM = ifelse(!X__4 == "",X__4,NA),
           LINE = ifelse(!X__6 == "",X__6,NA))]

DT2 <- DT2 %>% fill(GROUP,TEAM,LINE) %>%
  as.data.table()
DT2[is.na(DT2)] <- "-" # Represent there in no info
DT2 <- DT2[,!2:7] # Remove the original Cascade part
dragElement(document.getElementById("myModal"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
  document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;} 
  else {elmnt.onmousedown = dragMouseDown;}

  function dragMouseDown(w) {
    w = w || window.event;
    w.preventDefault();
    pos3 = w.clientX;
    pos4 = w.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;}
    
  function elementDrag(w) {
    w = w || window.event;
    w.preventDefault();
    pos1 = pos3 - w.clientX;
    pos2 = pos4 - w.clientY;
    pos3 = w.clientX;
    pos4 = w.clientY;
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";}

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;}}
#myModal {
    position: absolute;
    z-index: 9; height: 200px; width: 200px;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
    top: 0;
    right: 0;}
#myModalheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;}

答案 1 :(得分:1)

您还可以使用flex box将容器放到右侧。

#parentContainer{
    display: flex; justify-content: flex-end
}

答案 2 :(得分:0)

我还有两个选择可以做到这一点:

  1. 完全不要使用right:0;,而要使用left:100%; width:max-content;后跟transform: translateX(-100%);,因此它会根据子对象而不是父对象而偏移: (仅对css文件进行了更改)

dragElement(document.getElementById("myModal"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
  document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;} 
  else {elmnt.onmousedown = dragMouseDown;}

  function dragMouseDown(w) {
    w = w || window.event;
    w.preventDefault();
    pos3 = w.clientX;
    pos4 = w.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;}
    
  function elementDrag(w) {
    w = w || window.event;
    w.preventDefault();
    pos1 = pos3 - w.clientX;
    pos2 = pos4 - w.clientY;
    pos3 = w.clientX;
    pos4 = w.clientY;
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";}

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;}}
#myModal {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
    top: 0;
    left: 100%;                   /**Important*/
    transform: translateX(-100%); /**Important: offsets based on the element's width and not its parent*/
    width:max-content;            /**Important: prevents element from shrinking*/
    top:0;
}
    
#myModalheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
}
<div id="myModal">
  <div id="myModalheader">Click here to move</div>
  <p>Move</p><p>this</p><p>DIV</p>
</div>

  1. 第二个选项是在第一次拖动期间计算元素右侧的偏移量,并在完成拖动后将right:0;设置为right:auto; (仅对javascript文件进行了更改)

dragElement(document.getElementById("myModal"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
  document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;} 
  else {elmnt.onmousedown = dragMouseDown;}

  function dragMouseDown(w) {
    w = w || window.event;
    w.preventDefault();
    pos3 = w.clientX;
    pos4 = w.clientY;
    document.onmouseup = closeDragElement;
    document.onmousemove = elementDrag;}
    
  function elementDrag(w) {
    w = w || window.event;
    w.preventDefault();
    pos1 = pos3 - w.clientX;
    pos2 = pos4 - w.clientY;
    pos3 = w.clientX;
    pos4 = w.clientY;
    //Check if a right offset exists (will only be true
    //during the first time you drag it)
    if(elmnt.style.right != null && elmnt.style.right != "auto"){
      //calculate right offset
      var offsetright = window.innerWidth-elmnt.offsetLeft-elmnt.offsetWidth;
      //add the offset to the right of the element
      elmnt.style.right = (offsetright +pos1) + "px";
    }
    //end of change
    
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";}

  function closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
    elmnt.style.right = "auto";
  }}
#myModal {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
    top: 0;
    right: 0;}
#myModalheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;}
<div id="myModal">
  <div id="myModalheader">Click here to move</div>
  <p>Move</p><p>this</p><p>DIV</p>
</div>