Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/draggable.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations) (download) (as text)
Mon Dec 7 01:59:08 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 2175 byte(s)
drag操作で前面に来るようにz-index更新
1
2 // Copyright (c) 2009 Katsuhisa Yuasa <berupon [at] gmail.com>
3 // License http://www.opensource.org/licenses/mit-license.html
4
5 var Draggable = function() {
6 var NaN0 = function (v){
7 return isNaN(v) ? 0 : v;
8 };
9 var dragging = false;
10 var dragStartMousePos = null;
11 var dragElementStartPos = null;
12
13 var clickedElement = null;
14 var dragElement = null;
15 var elems = null;
16
17 function endDrag() {
18 dragging = false;
19 clickedElement = null;
20 dragElement = null;
21 }
22
23 function getDragTarget(elem) {
24 var attr = elem.attributes.getNamedItem("dragtarget");
25 var ret = null;
26 if (attr) {
27 ret = $(attr.value);
28 if (!ret) {
29 alert("Draggable dragtarget not found!");
30 return;
31 }
32 }else {
33 ret = clickedElement;
34 }
35 return ret;
36 }
37
38 var onMouseDownHandler = function(e) {
39 clickedElement = Event.element(e);
40 dragElement = getDragTarget(clickedElement);
41 dragging = true;
42 dragStartMousePos = Event.pointer(e);
43 dragElementStartPos = [NaN0(parseInt(dragElement.style.left)), NaN0(parseInt(dragElement.style.top))];
44
45 var maxZIndex = 0;
46 for (var i=0; i<elems.length; ++i) {
47 var elem = getDragTarget(elems[i]);
48 maxZIndex = Math.max(elem.style.zIndex, maxZIndex);
49 }
50 dragElement.style.zIndex = maxZIndex + 1;
51 Event.stop(e);
52 };
53
54 this.attach = function(){
55 elems = getElementsByClassName('draggable');
56 for (var i=0; i<elems.length; ++i) {
57 var elem = elems[i];
58 Event.observe(elem, 'mousedown', onMouseDownHandler);
59 }
60 };
61 Event.observe(window, 'load', this.attach);
62
63 Event.observe(document, 'mouseup', function(){
64 endDrag();
65 });
66 Event.observe(document, 'mousemove', function(e){
67 if (!dragging) {
68 return;
69 }
70 var curMousePos = Event.pointer(e);
71 if (curMousePos.x < 0 || curMousePos.y < 0) {
72 return;
73 }
74 var diffMousePos = [curMousePos.x-dragStartMousePos.x, curMousePos.y-dragStartMousePos.y];
75 dragElement.style.left = dragElementStartPos[0] + diffMousePos[0] + "px";
76 dragElement.style.top = dragElementStartPos[1] + diffMousePos[1] + "px";
77 });
78 Event.observe(document, 'blur', function(){
79 endDrag();
80 });
81
82
83 };
84
85

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