Develop and Download Open Source Software

Browse Subversion Repository

Contents of /js/draggable.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations) (download) (as text)
Sat Dec 5 02:36:36 2009 UTC (14 years, 5 months ago) by berupon
File MIME type: application/x-javascript
File size: 1809 byte(s)
ライセンス表記
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
16 var onMouseDownHandler = function(e) {
17 dragging = true;
18 clickedElement = Event.element(e);
19 var attr = clickedElement.attributes.getNamedItem("dragtarget");
20 if (attr) {
21 dragElement = $(attr.value);
22 if (!dragElement) {
23 alert("Draggable dragtarget not found!");
24 }
25 }else {
26 dragElement = clickedElement;
27 }
28 dragStartMousePos = Event.pointer(e);
29 dragElementStartPos = [NaN0(parseInt(dragElement.style.left)), NaN0(parseInt(dragElement.style.top))];
30 Event.stop(e);
31 };
32
33 this.attach = function(){
34 var elems = getElementsByClassName('draggable');
35 for (var i=0; i<elems.length; ++i) {
36 var elem = elems[i];
37 Event.observe(elem, 'mousedown', onMouseDownHandler);
38 }
39 };
40 Event.observe(window, 'load', this.attach);
41
42 Event.observe(document, 'mouseup', function(){
43 dragging = false;
44 clickedElement = null;
45 dragElement = null;
46 });
47 Event.observe(document, 'mousemove', function(e){
48 if (!dragging) {
49 return;
50 }
51 var curMousePos = Event.pointer(e);
52 if (curMousePos.x < 0 || curMousePos.y < 0) {
53 return;
54 }
55 var diffMousePos = [curMousePos.x-dragStartMousePos.x, curMousePos.y-dragStartMousePos.y];
56 dragElement.style.left = dragElementStartPos[0] + diffMousePos[0] + "px";
57 dragElement.style.top = dragElementStartPos[1] + diffMousePos[1] + "px";
58 });
59 Event.observe(document, 'blur', function(){
60 dragging = false;
61 });
62
63
64 };
65
66

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