Et lille uddrag fra mit eget (ufærdige) JavaScript lib:
(Godt nok event-baseret, men det burde give dig ideen)
- /***********ZenLib*************
- **
- * By: Jakob - "Saebekassebil"
- * Last modified: 20/08-2008
- **
- ******************************/
-
- window.onload = function()
- {
-
- /* SETTINGS START */
-
- var opacityValue = "0.5" // Opacity for drag objects. Set to 1.0 for no opacity.
- var cursorValue = "move" // Cursor for drag objects. Set to auto for normal cursor.
- var regExpClassName = "dragAble"; // The classname that makes an object dragable.
- var showMenu = true; // If this is set to true, a menu will be showed when Ctrl + click.
-
- /* SETTINGS END */
-
- var dragTarget = null;
- var dragState = false;
- var mouse_x = 0;
- var mouse_y = 0;
- var offset_x = 0;
- var offset_y = 0;
- var i = 0;
- var initTargets = new Array();
- var arrayIndex;
- var ctrlDown = false;
-
- function in_array(haystack, needle)
- {
- for(var x in haystack)
- {
- if(haystack[x][0] == needle[0])
- return x;
- }
-
- return -1;
- }
-
- function getTarget(element)
- {
- if(!element.target.id)
- return;
-
- var target = document.getElementById(element.target.id);
-
- var regPat = new RegExp(regExpClassName);
- if(!regPat.test(target.className))
- return;
-
- arrayIndex = in_array(initTargets, Array(target.id, 0));
- if(arrayIndex == -1)
- {
- initTargets.push(Array(target.id, 0));
- arrayIndex = initTargets.length-1;
- }
-
- target.style.position = "absolute";
- target.style.zIndex = "1";
- target.style.opacity = opacityValue;
- target.style.filter = "alpha(opacity="+(opacityValue*100)+")";
- target.style.cursor = cursorValue;
- if(initTargets[arrayIndex][1] == 0)
- {
- target.style.left = target.offsetLeft + "px";
- target.style.top = target.offsetTop + "px";
- var someLeft = target.style.left;
- var someTop = target.style.top;
- offset_x = mouse_x - (someLeft.substr(0, (someLeft.length-2)));
- offset_y = mouse_y - (someTop.substr(0, (someTop.length-2)));
- }
- else
- {
- var someLeft = target.style.left;
- var someTop = target.style.top;
- offset_x = mouse_x - (someLeft.substr(0, (someLeft.length-2)));
- offset_y = mouse_y - (someTop.substr(0, (someTop.length-2)));
- }
-
- initTargets[arrayIndex][1] = 1;
- return target;
- }
-
- function resetDrag()
- {
- dragTarget.style.cursor = "auto";
- dragTarget.style.opacity = "1.0";
- dragTarget.style.filter = "alpha(opacity=100)";
- dragTarget.style.zIndex = "0";
- dragTarget = null;
- dragState = false;
- offset_x = 0;
- offset_y = 0;
- }
-
- function doDrag(element)
- {
- dragTarget.style.left = (mouse_x - offset_x) + "px";
- dragTarget.style.top = (mouse_y - offset_y) + "px";
- }
-
- function getMouseXY(element)
- {
- if(document.all)
- {
- mouse_x = element.clientX+document.body.scrollLeft;
- mouse_y = element.clientY+document.body.scrollTop;
- }
- else
- {
- mouse_x = element.pageX;
- mouse_y = element.pageY;
- }
- }
-
-
- if(!document.addEventListener) // IE
- {
- document.addEventListener = function(type, handler)
- {
- document.attachEvent('on'+type, function(e)
- {
- e.target = e.srcElement;
- return handler(e);
- });
- }
- }
-
- document.addEventListener('mousedown', function(e)
- {
- if(ctrlDown == false)
- dragTarget = getTarget(e);
- else if(ctrlDown == true && showMenu == true)
- alert("Menu!");
- if(dragTarget)
- {
- dragState = true;
- }
-
- }, false);
-
- document.addEventListener('mousemove', function(e)
- {
- getMouseXY(e);
- if(dragState)
- {
- doDrag(e);
- }
-
- }, false);
-
- document.addEventListener('mouseup', function(e)
- {
- if(dragState)
- {
- resetDrag();
- }
-
- }, false);
-
- document.addEventListener('keydown', function(e)
- {
- if(window.event) // IE
- {
- keynum = e.keyCode;
- }
- else if(e.which)
- {
- keynum = e.which;
- }
-
- if(keynum == 17)
- {
- ctrlDown = true;
- }
- }, false);
-
- document.addEventListener('keyup', function(e)
- {
- if(window.event) // IE
- {
- keynum = e.keyCode;
- }
- else if(e.which)
- {
- keynum = e.which;
- }
-
- if(keynum == 17)
- {
- ctrlDown = false;
- }
- }, false);
- }