/** * jquery.hoverdir.js v1.1.0 * 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( $, window, undefined ) { 'use strict'; $.hoverdir = function( options, element ) { this.$el = $( element ); this._init( options ); }; // the options $.hoverdir.defaults = { speed : 300, easing : 'ease', hoverdelay : 0, inverse : false }; $.hoverdir.prototype = { _init : function( options ) { // options this.options = $.extend( true, {}, $.hoverdir.defaults, options ); // transition properties this.transitionprop = 'all ' + this.options.speed + 'ms ' + this.options.easing; // support for css transitions this.support = modernizr.csstransitions; // load the events this._loadevents(); }, _loadevents : function() { var self = this; this.$el.on( 'mouseenter.hoverdir, mouseleave.hoverdir', function( event ) { var $el = $( this ), $hoverelem = $el.find( 'div.dirhov' ), direction = self._getdir( $el, { x : event.pagex, y : event.pagey } ), stylecss = self._getstyle( direction ); if( event.type === 'mouseenter' ) { $hoverelem.hide().css( stylecss.from ); cleartimeout( self.tmhover ); self.tmhover = settimeout( function() { $hoverelem.show( 0, function() { var $el = $( this ); if( self.support ) { $el.css( 'transition', self.transitionprop ); } self._applyanimation( $el, stylecss.to, self.options.speed ); } ); }, self.options.hoverdelay ); } else { if( self.support ) { $hoverelem.css( 'transition', self.transitionprop ); } cleartimeout( self.tmhover ); self._applyanimation( $hoverelem, stylecss.from, self.options.speed ); } } ); }, // credits : http://stackoverflow.com/a/3647634 _getdir : function( $el, coordinates ) { // the width and height of the current div var w = $el.width(), h = $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 - $el.offset().left - ( w/2 )) * ( w > h ? ( h/w ) : 1 ), y = ( coordinates.y - $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; }, _getstyle : function( direction ) { var fromstyle, tostyle, slidefromtop = { left : '0px', top : '-100%' }, slidefrombottom = { left : '0px', top : '100%' }, slidefromleft = { left : '-100%', top : '0px' }, slidefromright = { left : '100%', top : '0px' }, slidetop = { top : '0px' }, slideleft = { left : '0px' }; switch( direction ) { case 0: // from top fromstyle = !this.options.inverse ? slidefromtop : slidefrombottom; tostyle = slidetop; break; case 1: // from right fromstyle = !this.options.inverse ? slidefromright : slidefromleft; tostyle = slideleft; break; case 2: // from bottom fromstyle = !this.options.inverse ? slidefrombottom : slidefromtop; tostyle = slidetop; break; case 3: // from left fromstyle = !this.options.inverse ? slidefromleft : slidefromright; tostyle = slideleft; break; }; return { from : fromstyle, to : tostyle }; }, // apply a transition or fallback to jquery animate based on modernizr.csstransitions support _applyanimation : function( el, stylecss, speed ) { $.fn.applystyle = this.support ? $.fn.css : $.fn.animate; el.stop().applystyle( stylecss, $.extend( true, [], { duration : speed + 'ms' } ) ); }, }; var logerror = function( message ) { if ( window.console ) { window.console.error( message ); } }; $.fn.hoverdir = function( options ) { var instance = $.data( this, 'hoverdir' ); if ( typeof options === 'string' ) { var args = array.prototype.slice.call( arguments, 1 ); this.each(function() { if ( !instance ) { logerror( "cannot call methods on hoverdir prior to initialization; " + "attempted to call method '" + options + "'" ); return; } if ( !$.isfunction( instance[options] ) || options.charat(0) === "_" ) { logerror( "no such method '" + options + "' for hoverdir instance" ); return; } instance[ options ].apply( instance, args ); }); } else { this.each(function() { if ( instance ) { instance._init(); } else { instance = $.data( this, 'hoverdir', new $.hoverdir( options, this ) ); } }); } return instance; }; } )( jquery, window );