Javascript 如何设置可拖动div的起始位置?
我有一个可拖动的Javascript 如何设置可拖动div的起始位置?,javascript,css,position,draggable,Javascript,Css,Position,Draggable,我有一个可拖动的div,只要我不通过top,right,…CSS改变它的位置,它就可以正常工作。我的目标是将div的预拖动位置设置在窗口的右上角,我使用top=0,right=0;问题是,拖动时,div会粘在该位置 dragElement(document.getElementById(“myModal”); 功能牵引装置(elmnt){ 变量pos1=0,pos2=0,pos3=0,pos4=0; if(document.getElementById(elmnt.id+“header”)){
div
,只要我不通过top,right,…
CSS改变它的位置,它就可以正常工作。我的目标是将div的预拖动位置设置在窗口的右上角,我使用top=0,right=0
;问题是,拖动时,div
会粘在该位置
dragElement(document.getElementById(“myModal”);
功能牵引装置(elmnt){
变量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;}
功能下拉列表(w){
w=w | | window.event;
w、 预防默认值();
pos3=w.clientX;
pos4=w.clientY;
document.onmouseup=关闭DrageElement;
document.onmousemove=elementDrag;}
功能元素拖动(w){
w=w | | window.event;
w、 预防默认值();
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”;}
函数closeDrageElement(){
document.onmouseup=null;
document.onmousemove=null;}}
#myModal{
位置:绝对位置;
z指数:9;
背景色:#f1f1;
文本对齐:居中;
边框:1px实心#D3;
排名:0;
右:0;}
#粘虫头{
填充:10px;
光标:移动;
z指数:10;
背景色:#2196F3;
颜色:#fff;}
单击此处移动
移动此分区
给元素一个宽度和高度,你应该很好
dragElement(document.getElementById(“myModal”);
功能牵引装置(elmnt){
变量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;}
功能下拉列表(w){
w=w | | window.event;
w、 预防默认值();
pos3=w.clientX;
pos4=w.clientY;
document.onmouseup=关闭DrageElement;
document.onmousemove=elementDrag;}
功能元素拖动(w){
w=w | | window.event;
w、 预防默认值();
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”;}
函数closeDrageElement(){
document.onmouseup=null;
document.onmousemove=null;}}
#myModal{
位置:绝对位置;
z指数:9;高度:200px;宽度:200px;
背景色:#f1f1;
文本对齐:居中;
边框:1px实心#D3;
排名:0;
右:0;}
#粘虫头{
填充:10px;
光标:移动;
z指数:10;
背景色:#2196F3;
颜色:#fff;}
单击此处移动
移动此分区
给元素一个宽度和高度,你应该很好
dragElement(document.getElementById(“myModal”);
功能牵引装置(elmnt){
变量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;}
功能下拉列表(w){
w=w | | window.event;
w、 预防默认值();
pos3=w.clientX;
pos4=w.clientY;
document.onmouseup=关闭DrageElement;
document.onmousemove=elementDrag;}
功能元素拖动(w){
w=w | | window.event;
w、 预防默认值();
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”;}
函数closeDrageElement(){
document.onmouseup=null;
document.onmousemove=null;}}
#myModal{
位置:绝对位置;
z指数:9;高度:200px;宽度:200px;
背景色:#f1f1;
文本对齐:居中;
边框:1px实心#D3;
排名:0;
右:0;}
#粘虫头{
填充:10px;
光标:移动;
z指数:10;
背景色:#2196F3;
颜色:#fff;}
单击此处移动
移动此分区
您也可以使用flex box将容器放在右侧
#parentContainer{
display: flex; justify-content: flex-end
}
您也可以使用flex box将容器放到右侧
#parentContainer{
display: flex; justify-content: flex-end
}
我还有两个选择:
右键:0代码>并使用左:100%;宽度:最大含量代码>后接转换:translateX(-100%)代码>因此它基于子对象而不是父对象进行偏移:
(仅对css文件进行了更改)
dragElement(document.getElementById(“myModal”);
功能牵引装置(elmnt){
变量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;}
功能下拉列表(w){
w=w | | window.event;
w、 预防默认值();
pos3=w.clientX;
pos4=w.clientY;
document.onmouseup=关闭DrageElement;
document.onmousemove=elementDrag;}
功能元素拖动(w){
w=w | | window.event;
w、 预防默认值();
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”;}
函数closeDrageElement(){
document.onmouseup=null;
document.onmousemove=null;}}
#myModal{
位置:绝对位置;
z指数:9;
背景色:#f1f1;
文本对齐:居中;
边框:1px实心#D3;
排名:0;
左边