Tibor SzászInteraction matters

  • Home
  • About me
  • Projects
  • Reqest price offer
  • Contact me
  • RSS

2009 1119

How to write drag and drop code from scratch?

How to write drag and drop code from scratch? Drag and drop is a very cool feature in the browsers too, if really needed. Most JS libraries has this functionality, from jQuery to MooTools. But how they do it?

Well, it's not black magic, actually really simple.


The idea
You need to combine two different mouse events: mouseDown and mouseMove, then track the mouse position, update an elements position, finally cancel the mouseMove event on mouseUp.

The goal
Create a freely movable window that you can move with a handle.

Click "read more" to see the codes.


The codes
This is the HTML markup.

Drag me
CSS styles:
	#todrag{
		position: absolute;
		left: 100px;
		top: 10px;
		color: #0f0;
		background: #000;
		width: 200px;
		height: 100px;
	}
	#todrag span{
		display: block;
		position: relative;
		left: 1px;
		top: 1px;
		width: 190px;
		height: 20px;
		background: #333;
		padding: 4px;
		cursor: move;
	}
JavaScript code
var win = document.getElementById('todrag');
var handle = document.getElementById('handle');

handle.onmousedown = function(e)
{
	// This is where you have to handle browser exceptions
	var diffX = parseInt(win.style.left) - e.clientX;
	var diffY = parseInt(win.style.top) - e.clientY;

	// The mouseMove event is tracked, when the mouse button is down.
	document.onmousemove = function(x)
	{
		win.style.left = x.clientX + diffX +'px';
		win.style.top  = x.clientY + diffY +'px';
	}
}
/**
 *   Cancel the mouseMove event
 */
handle.onmouseup = function(e){ 
	document.onmousemove = function(){}
}
This is it.
Please note that this solution is intended to show you only the concept, not a complete, all in one method. Do some research, evolve it to your needs.

attachmentFile attached to post: simple-drag-n-drop.zip


Comments


#01

Ehhez igen hasonlatos egy drag-n-drop megoldás, amit a jQuery megoldás. De ez lényegesen kevesebb és okosabb megoldás.

Ezt egy inntelligens kezelő "konténer" megoldással egész érdekes shop megoldásokat is lehetne belőle csinálni. :)

Posted by: cyrus @ 2009-11-19 21:38

#02

Szia Cyrus,

Igen, ez egy nagyon buta, nagyon fapados megoldás, a dolog célja az volt, hogy bemutassam, hogyan működik a drag and drop az alapjait tekintve, a fenti módszer még explorerben sem megy ;)

UPD: Tervezem amúgy megírni ennek a folytatásást, extra opciók hozzáadásást, objektumba való csomagolását.

Posted by: Tibor @ 1970-01-01 00:00

Add new comment


Your name
Email
Your avatar
Website
Comment

Briefing

Me myself and I
I love coding, especially for the web. I'm in the online industry for 6 years. My favourite toys are:
JavaScript, PHP, XHTML, MySQL, Canvas, Flash
Váltás magyar nyelvre

Topics

  • Coffee break
  • Design & stuff
  • Generative arts
  • Programming

Friends

  • Kárpáti Róbert
  • Royaljerry
  • Bagi Zoltán
  • Pegazoo
  • Charles Alexander Deli
  • Bence Bangócs
  • Lunakid
  • Rapp design

Feeling so social

  • Facebook
  • IWIW
  • TurulMeme

Tags

javascript (12) php (6) mootools (4) iscaffold (3) codeigniter (3) crud (3) tutorial (2) google (2) renders (2) structure-synth (2) css (2) template (2) free (2) google-buzz (1) script (1) forge (1) hack (1) table (1) open-source (1) rc1 (1) infovis (1) rss (1) interface (1) canvas (1) design, (1) user (1) experience (1) soundcloud (1) resizing (1) joomla (1) bermuda-triangle (1) wordpress (1) drupal (1) data-visualisation (1) data-uri (1) konami-codes (1) tagging (1) slimbox (1) bugfix (1) cfdg (1) blackout (1) firefox-extension (1) utf8 (1) class (1) slideshow (1) nl2br (1) experiment (1) svg (1) library (1) ajax (1) sitebuild (1) tabs (1) inspiration (1) lists (1) drag-and-drop (1) wikipedia (1) dns (1) trick (1)




All rights reserved, ©2008-2010 - Built on CodeIgniter framework - Konami codes - Mostly Valid XHTML 1.1 - Valid CSS 2.1