首页 > 代码库 > 可挪动的div

可挪动的div

/-----------------------------DIV 设置id 和 Class----------------------------------------------/
<div id="add" class=" mydiv ">
/-----------------------------js----------------------------------------------/
//可挪动的DIV
var posX;
var posY;
function fwus(num){
    if(num==1){
        fwuss = document.getElementById("add");
    }else{
        fwuss = document.getElementById("update");
    }
    fwuss.onmousedown=function(e){
        posX = event.x - fwuss.offsetLeft;//获得横坐标x
        posY = event.y - fwuss.offsetTop;//获得纵坐标y
        document.onmousemove = mousemove;//调用函数,只要一直按着按钮就能一直调用
    }
    document.onmouseup = function(){
        document.onmousemove = null;//鼠标举起,停止
    }
}
function mousemove(ev){
    if(ev==null) ev = window.event;//IE
    fwuss.style.left = (ev.clientX - posX) + "px";
    fwuss.style.top = (ev.clientY - posY) + "px";
}

/------------------------------CSS-----------------------------/
.mydiv{ position: fixed;z-index: 1000;background: rgba(0, 0, 0, 0);}
.color{right: 0px;}




 

可挪动的div