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
    39 Shirts – Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

Incredible Demos

  • By
    Implement the Google AJAX Search API

    Let's be honest...WordPress' search functionality isn't great. Let's be more honest...no search functionality is better than Google's. Luckily for us, Google provides an awesome method by which we can use their search for our own site: the Google AJAX Search API.

  • By
    Highlighter: A MooTools Search & Highlight Plugin

    Searching within the page is a major browser functionality, but what if we could code a search box in JavaScript that would do the same thing? I set out to do that using MooTools and ended up with a pretty decent solution. The MooTools JavaScript Class The...

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!