<script type="text/javascript">
window.onload = setPositionDrag;
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
var dragObject = null;
var mouseOffset = null;
function setPositionDrag(){
var lst = document.getElementsByTagName("div");
for(var i=0; i < lst.length; i++){
var item = lst[i];
if( item.getAttribute('name') == 'draggable' ){
item.style.cursor = "move";
item.style.display = "inline";
item.style.zIndex = "1000";
item.onmousedown = function(ev){
dragObject = this;
mouseOffset = getMouseOffset(this, ev);
return false;
}
item.ondblclick = function(ev){
if(this.id){
setCookie(this.id+'top' , "" );
setCookie(this.id+'left' , "" );
}
this.style.display='none';
}
if(item.id){
var top = getCookie(item.id + "top");
var left = getCookie(item.id + "left");
if( (top!="") && (left!="")){
item.style.position = "absolute";
item.style.left = left + "px";
item.style.top = top + "px";
}
}
}
}
}
function mouseCoords(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
function getMouseOffset(target, ev){
ev = ev || window.event;
var docPos = getPosition(target);
var mousePos = mouseCoords(ev);
return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}
function getPosition(e){
var left = 0;
var top = 0;
while (e.offsetParent){
left += e.offsetLeft;
top += e.offsetTop;
e = e.offsetParent;
}
left += e.offsetLeft;
top += e.offsetTop;
return {x:left, y:top};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mouseCoords(ev);
if(dragObject){
dragObject.style.position = 'absolute';
dragObject.style.top = mousePos.y - mouseOffset.y + 'px';
dragObject.style.left = mousePos.x - mouseOffset.x + 'px';
return false;
}
}
function mouseUp(ev){
if(dragObject){
ev = ev || window.event;
var mousePos = mouseCoords(ev);
setCookie(dragObject.id+'top' , mousePos.y - mouseOffset.y, 365);
setCookie(dragObject.id+'left', mousePos.x - mouseOffset.x, 365);
}
dragObject = null;
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
</script>
No comments:
Post a Comment