Element Position Swapping Using MooTools 1.2

By  on  

We all know that MooTools 1.2 can do some pretty awesome animations. What if we want to quickly make two element swap positions without a lot of fuss? Now you can by implementing a MooTools swap() method.

MooTools 1.2 Implementation

Element.implement({
	swap: function(other,speed) {
		
		//get the position
		position1 = this.getCoordinates();
		position2 = other.getCoordinates();
		
		//position this where it is
		this.setStyles({
			top: position1.top + 'px',
			left: position1.left + 'px',
			position: 'absolute'
		});
		
		//position the other element where it is
		other.setStyles({
			top: position2.top + 'px',
			left: position2.left + 'px',
			position: 'absolute'
		});
		
		//fx
		var myFx = new Fx.Morph(this,{
			duration: speed
		}).start({
			'top': position2.top + 'px',
			'left': position2.left + 'px'
		});
		
		//fx
		var myFx2 = new Fx.Morph(other,{
			duration: speed
		}).start({
			'top': position1.top + 'px',
			'left': position1.left + 'px'
		});
	}
});

MooTools 1.2 Usage

window.addEvent('domready', function() {
	//swap the two elements when a trigger is clicked
	$('trigger').addEvent('click',function(e) {
		$('element1').swap($('element2'),300);
	});
});

To call the swap method, simply pass the second element and transition speed and watch the element swap position!

What would you use this for? Anything practical or just for fun?

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    Build a Calendar Using PHP, XHTML, and CSS

    One of the website features my customers love to provider their web users is an online dynamic calendar. An online calendar can be used for events, upcoming product specials, memos, and anything else you can think of. I've taken some time to completely...

  • By
    CSS Tooltips

    We all know that you can make shapes with CSS and a single HTML element, as I've covered in my CSS Triangles and CSS Circles posts.  Triangles and circles are fairly simply though, so as CSS advances, we need to stretch the boundaries...

Discussion

  1. I’m using firefox 3 and when i click swap, the height of the main div or something that’s holding that thing drops by half.

    Oh and double click causes also a bug with the starting-ending position of those boxes
    If you continue to double-click eventually those two boxes stay in the same place.

    I mean no harm, i just wanted you to know this :D

  2. @Elmas: Thank you for your comment.

    The “main div” bug is caused by changing the elements to “absolute” positioning — I’m still contemplating whether or not to change the elements to relative positioning when the move is complete.

    The “double-click” issue, technically, isn’t a bug because the items do switch places during the click. I may, however, add some sort of “lock” option.

  3. Michael

    Can you do something similar using floats? I’ve never fully understood why these kind of animations require absolute or relative positioning.

  4. Catar4x

    Good effect, I think that I use this effect for a inventory in PHP Game.
    Thanks ;)

  5. @David
    Very cool method. I think changing the position from absolute to relative isn’t the way to go. Because the markup for the two elements wouldn’t have changed, thus causing the elements to snap to a different position based on their new left and top values. Have you tried just swapping the left and top values and keeping the element position:relative intead of making them absolute?

    As for a ‘lock’ option, since you’re using Fx.Morph, Fx has a link option that you can set to ignore, thus applying the lock option.

    @Michael
    I think you can still do this with floats. The reason why position relative/absolute is required is because left, top, right, bottom only work on elements with the position style.

  6. Abba Bryant

    This would work as a visual cue for moving list items from one list to the other. Create the element as an overlay to the li’s, swap them and destroy them.

    I am sure there is a better use for it, but that is what popped into my head when I saw it.

  7. good tips,thanks for sharing.

  8. chintan

    hey i want to swap the elements of the webpage.. means to swap position of two images on some event… how is it posiible using javascript..

  9. hey david i got few questions for you !!! hope you can answer me :)

    is it possible to move fixed div ?

    and

    how can i do to move more than to object on a single click ?

    thks !

  10. fred

    other than postion swap, how about Dom index swap

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!