Mam prosty skrypt do obsługi drag & drop. Nie działa jeśli funkcja move odwołuje się do swojego obiektu poprzez "this". Jeśli robi to bezpośrednio za pomocą nazwy wszystko jest OK (wykomentowany kod). Co może być tego przyczyną?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
.window_style
{
background-color: #66CCCC;
border: 1px #000000 solid;
position: absolute;
width:100px;
height:100px;
}
<script LANGUAGE="JavaScript"> <!--
function Draging()
{
//pozycja początkowa okna
this.coordX = 0;
this.coordY = 0;
//pozycja początkowa kursora
this.offsetX = 0;
this.offsetY = 0;
//okno
this.target = null;
//pobiera dane początkowe o oknie i kursorze
this.drag = function drag(e, obj)
{
e = (e) ? e : window.event;
this.target = obj;
this.offsetX = e.clientX;
this.offsetY = e.clientY;
this.coordX = parseInt(this.target.style.left);
this.coordY = parseInt(this.target.style.top);
this.target.onmousemove = this.move;
}
//przesuwa okno
this.move = function move(e)
{
e = (e) ? e : window.event;
this.target.style.left = this.coordX + e.clientX - this.offsetX + 'px';
this.target.style.top = this.coordY + e.clientY - this.offsetY + 'px';
/*
//odwołanie przez winDrag, this nie działa
winDrag.target.style.left = winDrag.coordX + e.clientX - winDrag.offsetX + 'px';
winDrag.target.style.top = winDrag.coordY + e.clientY - winDrag.offsetY + 'px';
*/
return false;
}
//zaprzestaje przesuwania okna, czyści dane o obiekcie
this.drop = function drop(e)
{
e = (e) ? e : window.event;
this.target.onmousemove = null;
this.target = null;
this.offsetX = 0;
this.offsetY = 0;
this.coordX = 0;
this.coordY = 0;
}
}
var winDrag = new Draging;
//-->
<div id="win1" class="window_style" style="top:10px; left:50px" onMouseDown="winDrag.drag(event, this);" onMouseUp = "winDrag.drop()">Win1
</div>