Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/draggable.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download) (as text)
Fri Dec 4 12:17:36 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 1679 byte(s)
まだ完成していないけど途中経過の記録というか…backupとしてcommit
1
2 var Draggable = function() {
3 var NaN0 = function (v){
4 return isNaN(v) ? 0 : v;
5 };
6 var dragging = false;
7 var dragStartMousePos = null;
8 var dragElementStartPos = null;
9
10 var clickedElement = null;
11 var dragElement = null;
12
13 var onMouseDownHandler = function(e) {
14 dragging = true;
15 clickedElement = Event.element(e);
16 var attr = clickedElement.attributes.getNamedItem("dragtarget");
17 if (attr) {
18 dragElement = $(attr.value);
19 if (!dragElement) {
20 alert("Draggable dragtarget not found!");
21 }
22 }else {
23 dragElement = clickedElement;
24 }
25 dragStartMousePos = Event.pointer(e);
26 dragElementStartPos = [NaN0(parseInt(dragElement.style.left)), NaN0(parseInt(dragElement.style.top))];
27 Event.stop(e);
28 };
29
30 this.attach = function(){
31 var elems = getElementsByClassName('draggable');
32 for (var i=0; i<elems.length; ++i) {
33 var elem = elems[i];
34 Event.observe(elem, 'mousedown', onMouseDownHandler);
35 }
36 };
37 Event.observe(window, 'load', this.attach);
38
39 Event.observe(document, 'mouseup', function(){
40 dragging = false;
41 clickedElement = null;
42 dragElement = null;
43 });
44 Event.observe(document, 'mousemove', function(e){
45 if (!dragging) {
46 return;
47 }
48 var curMousePos = Event.pointer(e);
49 if (curMousePos.x < 0 || curMousePos.y < 0) {
50 return;
51 }
52 var diffMousePos = [curMousePos.x-dragStartMousePos.x, curMousePos.y-dragStartMousePos.y];
53 dragElement.style.left = dragElementStartPos[0] + diffMousePos[0] + "px";
54 dragElement.style.top = dragElementStartPos[1] + diffMousePos[1] + "px";
55 });
56 Event.observe(document, 'blur', function(){
57 dragging = false;
58 });
59
60
61 };
62
63

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26