-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathjquery.hoverdir.js
258 lines (163 loc) · 4.91 KB
/
jquery.hoverdir.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* jQuery.Hoverdir
*
* Modified version of https://github.com/codrops/DirectionAwareHoverEffect
*
* Modifications:
* - Removed CSS3 transitions and Modernizr requirements.
* - Applied CSS classes for improved flexibility via CSS.
*
* @copyright 2015 WebMan - Oliver Juhas, www.webmandesign.eu
*
* @link https://github.com/webmandesign/jquery.hoverdir
*
* @version 1.1.2
*/
/**
* jquery.hoverdir.js v1.1.2
* http://www.codrops.com
*
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright 2012, Codrops
* http://www.codrops.com
*/
( function( factory ) {
'use strict';
if ( typeof define === 'function' && define.amd ) {
define( ['jquery'], factory );
} else if ( typeof exports !== 'undefined' ) {
module.exports = factory( require( 'jquery' ) );
} else {
factory( jQuery );
}
} )( function( $ ) {
'use strict';
function Hoverdir( element, options ) {
this.$el = $( element );
// Set options
this.options = $.extend( true, {}, this.defaults, options );
// All classes that plugin generates
this.allClasses = {
from : this.options.fromPrefix + 'top ' + this.options.fromPrefix + 'right ' + this.options.fromPrefix + 'bottom ' + this.options.fromPrefix + 'left',
to : this.options.toPrefix + 'top ' + this.options.toPrefix + 'right ' + this.options.toPrefix + 'bottom ' + this.options.toPrefix + 'left'
};
// Load the events
this._loadEvents();
} // /Hoverdir
Hoverdir.prototype = {
defaults : {
fromPrefix : 'out-',
toPrefix : 'in-'
},
constructor : Hoverdir,
_loadEvents : function() {
this.$el.on( 'mouseenter.hoverdir mouseleave.hoverdir', $.proxy( function( event ) {
var fromPrefix = this.options.fromPrefix,
toPrefix = this.options.toPrefix,
direction = this._getDir( { x : event.pageX, y : event.pageY } ),
CSSclass = this._getClass( direction );
if ( event.type === 'mouseenter' ) {
this.$el
.removeClass( this.allClasses.from )
.addClass( toPrefix + CSSclass )
.siblings()
.removeClass( this.allClasses.to );
} else {
this.$el
.removeClass( this.allClasses.to )
.addClass( fromPrefix + CSSclass )
.siblings()
.removeClass( this.allClasses.from );
}
}, this ) );
},
/**
* Get the direction when the event is triggered.
* Credits : http://stackoverflow.com/a/3647634
*
* @param {Object} coordinates
*
* @return {Interger}
*/
_getDir : function( coordinates ) {
// The width and height of the current div
var w = this.$el.width(),
h = this.$el.height(),
// Calculate the x and y to get an angle to the center of the div from that x and y.
// Gets the x value relative to the center of the DIV and "normalize" it
x = ( coordinates.x - this.$el.offset().left - ( w / 2 ) ) * ( w > h ? ( h / w ) : 1 ),
y = ( coordinates.y - this.$el.offset().top - ( h / 2 ) ) * ( h > w ? ( w / h ) : 1 ),
// The angle and the direction from where the mouse came in/went out clockwise (TRBL=0123);
// first calculate the angle of the point,
// add 180 deg to get rid of the negative values
// divide by 90 to get the quadrant
// add 3 and do a modulo by 4 to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left).
direction = Math.round( ( ( ( Math.atan2( y, x ) * ( 180 / Math.PI ) ) + 180 ) / 90 ) + 3 ) % 4;
return direction;
},
/**
* Return a class based on cursor direction
*/
_getClass : function( direction ) {
var CSSclass;
switch( direction ) {
case 0:
CSSclass = 'top';
break;
case 1:
CSSclass = 'right';
break;
case 2:
CSSclass = 'bottom';
break;
case 3:
CSSclass = 'left';
break;
}
return CSSclass;
},
/**
* Setting options for plugin binding
*/
setOptions : function (options) {
this.options = $.extend( true, {}, this.defaults, this.options, options );
},
/**
* Unbinds the plugin
*/
destroy : function () {
this.$el.off( 'mouseenter.hoverdir mouseleave.hoverdir' );
this.$el.data( 'hoverdir', null );
},
/**
* Bind the plugin
*/
rebuild : function (options) {
if ( typeof options === 'object' ) {
this.setOptions( options );
}
this._loadEvents();
}
};
$.fn.hoverdir = function( option, parameter ) {
return this.each( function() {
var data = $( this ).data( 'hoverdir' ),
options = typeof option === 'object' && option;
// Initialize hoverdir.
if ( ! data ) {
data = new Hoverdir( this, options );
$( this ).data( 'hoverdir', data );
}
// Call hoverdir method.
if ( typeof option === 'string' ) {
data[ option ]( parameter );
if ( option === 'destroy' ) {
$( this ).data( 'hoverdir', false );
}
}
} );
};
$.fn.hoverdir.Constructor = Hoverdir;
} );