Sto usando il javascript "jquery.easydrag.js" per muovere degli oggetti all'interno del sito. Solo che vorrei limitarne il movimento all'interno di un certo div. Come posso fare?
codice:/** * EasyDrag 1.1 - Drag & Drop jQuery Plug-in * * For usage instructions please visit http://fromvega.com * * Copyright (c) 2007 fromvega */ (function($){ // to track if the mouse button is pressed var isMouseDown = false; // to track the current element being dragged var currentElement = null; // callback holders var dropCallbacks = {}; var dragCallbacks = {}; // global position records var lastMouseX; var lastMouseY; var lastElemTop; var lastElemLeft; // returns the mouse (cursor) current position $.getMousePosition = function(e){ var posx = 0; var posy = 0; if (!e) var e = window.event; if (e.pageX || e.pageY) { posx = e.pageX; posy = e.pageY; } else if (e.clientX || e.clientY) { posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; } return { 'x': posx, 'y': posy }; } // updates the position of the current element being dragged $.updatePosition = function(e) { var pos = $.getMousePosition(e); var spanX = (pos.x - lastMouseX); var spanY = (pos.y - lastMouseY); $(currentElement).css("top", (lastElemTop + spanY)); $(currentElement).css("left", (lastElemLeft + spanX)); } // when the mouse is moved while the mouse button is pressed $(document).mousemove(function(e){ if(isMouseDown){ // update the position and call the registered function $.updatePosition(e); if(dragCallbacks[currentElement.id] != undefined){ dragCallbacks[currentElement.id](e); } return false; } }); // when the mouse button is released $(document).mouseup(function(e){ if(isMouseDown){ isMouseDown = false; if(dropCallbacks[currentElement.id] != undefined){ dropCallbacks[currentElement.id](e); } return false; } }); // register the function to be called while an element is being dragged $.fn.ondrag = function(callback){ return this.each(function(){ dragCallbacks[this.id] = callback; }); } // register the function to be called when an element is dropped $.fn.ondrop = function(callback){ return this.each(function(){ dropCallbacks[this.id] = callback; }); } // set an element as draggable $.fn.easydrag = function(){ return this.each(function(){ // if no id is defined assign a unique one if(undefined == this.id) this.id = 'easydrag'+time(); // change the mouse pointer $(this).css("cursor", "move"); // when an element receives a mouse press $(this).mousedown(function(e){ // set it as absolute positioned $(this).css("position", "absolute"); // set z-index $(this).css("z-index", "10000"); // update track variables isMouseDown = true; currentElement = this; // retrieve positioning properties var pos = $.getMousePosition(e); lastMouseX = pos.x; lastMouseY = pos.y; lastElemTop = parseInt($(this).css("top")); lastElemLeft = parseInt($(this).css("left")); // IE doesn't return a valid value for 'top' and 'left' if they aren't explicit set // so we use a quick work around and set a specific position to start with if(isNaN(lastElemTop )) lastElemTop = lastMouseY - this.offsetHeight/2; if(isNaN(lastElemLeft)) lastElemLeft = lastMouseX - this.offsetWidth/2; $.updatePosition(e); return false; }); }); } })(jQuery);

Rispondi quotando
