Project

General

Profile

1
/* Smart Hover Box for Mootools 1.2
2
 * v1.0
3
 * Dedicated to public domain 
4
 * troy@consideropen.com
5
 * www.consideropen.com/blog
6
*/
7

    
8
/* Usage:
9
 * to implement smart hover box, 
10
 * take the id of the element you want to hover over, 
11
 * then use the id +  the "smartBoxSuffix" as the id for the hover element.
12
 * Finally, create a new SmartHoverBox class and set any options you want to change.
13
*/
14

    
15
var SmartHoverBox = new Class({    
16
	Implements: Options,  
17
	options: {  
18
		boxTimer        : 1000, // how many milliseconds before the box hides after mouseleave
19
		yOffset         : -10, // up and down offset in px - accepts negative ints		
20
		xOffset         : -10, // left and right offset in px - accepts negative ints
21
		smartBoxSuffix  : '_smarthbox', //suffix that creates a hover box
22
		smartBoxClose   : 'smarthbox_close', //this class will add "close" click event to element
23
		lockY   		: '', // 'top', 'bottom'
24
		lockX    		: '' // 'left', 'right'
25
	},  
26
	initialize: function(options) {  
27
		this.setOptions(options);
28
		this.pos = [];
29
		this.smartBoxes = $$('[id$=' + this.options.smartBoxSuffix + ']');
30
		this.closeElem = $(document.body).getElements('.' + this.options.smartBoxClose); 
31
		this.closeElem.addEvent('click', function(e){ 
32
			e.preventDefault();								
33
			this.closeBox();
34
		}.bind(this)).setStyle('cursor', 'pointer');
35
		this.smartBoxes.setStyle('display', 'none');
36
		this.showHideBox();
37
		this.closeBox();
38
	},
39
	showHideBox: function(){
40
		this.smartBoxes.each(function(item){
41
			this.getCurrentBox(item);
42
			item.addEvent('mouseleave', function(){this.closeBoxTimer()}.bind(this));	
43
			item.addEvent('mouseenter', function(){$clear(this.delay)}.bind(this));
44
			$(this.currentBox).addEvent('mouseleave', function(){this.closeBoxTimer()}.bind(this));		
45
			$(this.currentBox).addEvent('mouseenter', function(){	
46
				this.getCurrentBox(item);
47
				$clear(this.delay);
48
				this.smartBoxes.setStyle('display', 'none');
49
				item.setStyles({ display: 'block', position: 'absolute' }).setStyle('z-index', '1000000');				
50
				
51
				this.positioning(item, this.currentBox);
52
				
53
			}.bind(this)).setStyle('cursor', 'pointer');	
54
		}.bind(this));
55
	},
56
	closeBoxTimer: function(){
57
		this.hideEm = function(){this.closeBox()}.bind(this);
58
		this.delay = this.hideEm.delay(this.options.boxTimer);
59
	},
60
	closeBox: function() {
61
		this.smartBoxes.setStyle('display', 'none');
62
	},
63
	positioning: function(currentItem, currentLink){
64
		this.pos.windowSize = $(window).getSize(); 
65
		this.pos.windowScroll = $(window).getScroll();
66
		this.pos.boxSize = currentItem.getSize();
67
		this.pos.inputPOS = $(currentLink).getCoordinates();
68
		this.pos.inputCOOR = $(currentLink).getPosition();
69
		this.pos.inputSize = $(currentLink).getSize();
70
		this.pos.halfWindowY = this.pos.windowSize.y / 2;
71
		this.pos.halfWindowX = this.pos.windowSize.x / 2;
72
		this.pos.inputBottomPOS = this.pos.inputPOS.top + this.pos.inputSize.y; 
73
		this.pos.inputBottomPOSAdjust = this.pos.inputBottomPOS - this.pos.windowScroll.y
74
		this.pos.inputLeftPOS = this.pos.inputPOS.left + this.options.xOffset;
75
		this.pos.inputRightPOS = this.pos.inputPOS.right;
76
		this.pos.leftOffset = this.pos.inputCOOR.x + this.options.xOffset;
77
		
78
		if(this.pos.halfWindowY < this.pos.inputBottomPOSAdjust && this.options.lockY == 'none' || this.options.lockY == 'top') { 
79
			currentItem.setStyle('top', this.pos.inputPOS.top - this.pos.boxSize.y - this.options.yOffset); //top
80
			if (this.pos.inputLeftPOS < this.pos.halfWindowX && this.options.lockX == 'none' || this.options.lockX == 'left' ) { 
81
				currentItem.setStyle('left', this.pos.leftOffset); //top left
82
			}
83
			else { currentItem.setStyle('left', (this.pos.inputPOS.right - this.pos.boxSize.x) - this.options.xOffset); }; //top right
84
		}
85
		else {
86
			currentItem.setStyle('top', this.pos.inputBottomPOS + this.options.yOffset); //bottom
87
			if (this.pos.inputLeftPOS < this.pos.halfWindowX && this.options.lockX == 'none' || this.options.lockX == 'left' ) { 
88
				currentItem.setStyle('left', this.pos.leftOffset); //bottom left
89
			} 
90
			else { currentItem.setStyle('left', (this.pos.inputPOS.right - this.pos.boxSize.x) - this.options.xOffset);};//bottom right
91
		};
92
	},
93
	getCurrentBox: function(currentItem){
94
		this.currentBox = currentItem.getProperty('id');
95
		this.currentBox = this.currentBox.replace('' + this.options.smartBoxSuffix + '', '');	
96
	}
97
}); 	
98

    
(20-20/20)