Native

There are times you may ask yourself "Why isn't that part of MooTools?" and while there are possibly a lot of answers to that it simply could be that it is something with only small usage.

For that reason MooTools made it easy for you to extend any native object like Arrays, Strings etc. to add the functionality you want.

In this example you will learn how to extend the Elements-Object with two custom methods. For this example we create two methods, the first equalize method calculates the average of a certain property, and applies it with the tween method. The setRandom tweens the property to a random value between the min and max values.

  • Demo
  • CSS
  • HTML
  • JavaScript

Execute Example

Code:

Elements.implement({

	equalize: function(property){
		var sum = 0, i, len;
		len = i = this.length;
		while (i--) sum += this[i].getDimensions()[property];
		var average = Math.round(sum / len);
		i = len;
		while (i--) this.tween(property, average);
		return this;
	},

	setRandom: function(property, min, max){
		var i = this.length, value;
		while (i--){
			value = Math.round(min + (max - min) * Math.random());
			this[i].tween(property, value);
		}
		return this;
	}

});

Usage:

myElements.equalize(property);
myElements.setRandom(property, min, max);