Overlay

Overlay is a compact, simple overlay class which allows for maximum control over its use.

Download @ Forge JS

 

MooTools Javascript Class

/*
---

name: Overlay

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires: [Core/Class, Core/Element.Style, Core/Element.Event, Core/Element.Dimensions, Core/Fx.Tween]

provides:
  - Overlay
...
*/

var Overlay = new Class({

	Implements: [Options, Events],

	options: {
		id: 'overlay',
		color: '#000',
		duration: 500,
		opacity: 0.5,
		zIndex: 5000/*,
		onClick: function(){},
		onClose: function(){},
		onHide: function(){},
		onOpen: function(){},
		onShow: function(){}
		*/
	},

	initialize: function(container, options){
		this.setOptions(options);
		this.container = document.id(container);

		this.bound = {
			'window': {
				resize: this.resize.bind(this),
				scroll: this.scroll.bind(this)
			},
			overlayClick: this.overlayClick.bind(this),
			tweenStart: this.tweenStart.bind(this),
			tweenComplete: this.tweenComplete.bind(this)
		};

		this.build().attach();
	},

	build: function(){
		this.overlay = new Element('div', {
			id: this.options.id,
			styles: {
				position: (Browser.ie6) ? 'absolute' : 'fixed',
				background: this.options.color,
				left: 0,
				top: 0,
				'z-index': this.options.zIndex,
				opacity: 0
			}
		}).inject(this.container);
		this.tween = new Fx.Tween(this.overlay, {
			duration: this.options.duration,
			link: 'cancel',
			property: 'opacity'
		});
		return this;
	}.protect(),

	attach: function(){
		window.addEvents(this.bound.window);
		this.overlay.addEvent('click', this.bound.overlayClick);
		this.tween.addEvents({
			onStart: this.bound.tweenStart,
			onComplete: this.bound.tweenComplete
		});
		return this;
	},

	detach: function(){
		var args = Array.prototype.slice.call(arguments);
		args.each(function(item){
			if(item == 'window') window.removeEvents(this.bound.window);
			if(item == 'overlay') this.overlay.removeEvent('click', this.bound.overlayClick);
		}, this);
		return this;
	},

	overlayClick: function(){
		this.fireEvent('click');
		return this;
	},

	tweenStart: function(){
		this.overlay.setStyles({
			width: '100%',
			height: this.container.getScrollSize().y,
			visibility: 'visible'
		});
		return this;
	},

	tweenComplete: function(){
		var event = this.overlay.getStyle('opacity') == this.options.opacity ? 'show' : 'hide';
		if (event == 'hide') this.overlay.setStyle('visibility', 'hidden');
		return this;
	},

	open: function(){
		this.fireEvent('open');
		this.tween.start(this.options.opacity);
		return this;
	},

	close: function(){
		this.fireEvent('close');
		this.tween.start(0);
		return this;
	},

	resize: function(){
		this.fireEvent('resize');
		this.overlay.setStyle('height', this.container.getScrollSize().y);
		return this;
	},

	scroll: function(){
		this.fireEvent('scroll');
		if (Browser.ie6) this.overlay.setStyle('left', window.getScroll().x);
		return this;
	}

});

Class: Overlay

Implements:

Options, Events

Overlay Method: constructor

Syntax:

var overlay = new Overlay(document.body);

Arguments:

  1. container - (string) The ID of the container (usually document.body) which will host the overlay.
  2. options - (**) The options for the Overlay instance.

Options:

  • id - (string, defaults to 'overlay') - The ID of the overlay to be created.
  • color - (string, defaults to '#000') - The base background color of the overlay. Keep in mind the actual screen color will change with the opacity level.
  • duration - (integer, defaults to 500) - The open/close duration of the overlay.
  • opacity - (float, defaults to 0.5) - The destination opacity level of the overlay.
  • zIndex - (integer, defaults to 5000) - The z-index of the overlay.

Events:

onClick

  • (function) Function to execute when the overlay is clicked

Signature

onClick(fn)

Example

onClick: function() {
    this.close();
}

onClose

  • (function) Function to execute immediately after the close directive has been given (before the opacity fade has completed).

Signature

onClose(fn)

onHide

  • (function) Function to execute when the overlay has been completely hidden (after the "close" event).

Signature

onHide(fn)

onOpen

  • (function) Function to execute immediately after the open directive has been given (before the opacity fade has completed).

Signature

onOpen(fn)

onShow

  • (function) Function to execute when the overlay has completed it's fade in (after the "open" event)

Signature

onShow(fn)