Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ;( function( $, window, undefined ) {
  3.    
  4.     'use strict';
  5.  
  6.     $.HoverDir = function( options, element ) {
  7.        
  8.         this.$el = $( element );
  9.         this._init( options );
  10.  
  11.     };
  12.  
  13.     // the options
  14.     $.HoverDir.defaults = {
  15.         speed : 300,
  16.         easing : 'ease',
  17.         hoverDelay : 0,
  18.         inverse : false
  19.     };
  20.  
  21.     $.HoverDir.prototype = {
  22.  
  23.         _init : function( options ) {
  24.            
  25.             // options
  26.             this.options = $.extend( true, {}, $.HoverDir.defaults, options );
  27.             // transition properties
  28.             this.transitionProp = 'all ' + this.options.speed + 'ms ' + this.options.easing;
  29.             // support for CSS transitions
  30.             this.support = Modernizr.csstransitions;
  31.             // load the events
  32.             this._loadEvents();
  33.  
  34.         },
  35.         _loadEvents : function() {
  36.  
  37.             var self = this;
  38.            
  39.             this.$el.on( 'mouseenter.hoverdir, mouseleave.hoverdir', function( event ) {
  40.                
  41.                 var $el = $( this ),
  42.                     $hoverElem = $el.find( 'div' ),
  43.                     direction = self._getDir( $el, { x : event.pageX, y : event.pageY } ),
  44.                     styleCSS = self._getStyle( direction );
  45.                
  46.                 if( event.type === 'mouseenter' ) {
  47.                    
  48.                     $hoverElem.hide().css( styleCSS.from );
  49.                     clearTimeout( self.tmhover );
  50.  
  51.                     self.tmhover = setTimeout( function() {
  52.                        
  53.                         $hoverElem.show( 0, function() {
  54.                            
  55.                             var $el = $( this );
  56.                             if( self.support ) {
  57.                                 $el.css( 'transition', self.transitionProp );
  58.                             }
  59.                             self._applyAnimation( $el, styleCSS.to, self.options.speed );
  60.  
  61.                         } );
  62.                        
  63.                    
  64.                     }, self.options.hoverDelay );
  65.                    
  66.                 }
  67.                 else {
  68.                
  69.                     if( self.support ) {
  70.                         $hoverElem.css( 'transition', self.transitionProp );
  71.                     }
  72.                     clearTimeout( self.tmhover );
  73.                     self._applyAnimation( $hoverElem, styleCSS.from, self.options.speed );
  74.                    
  75.                 }
  76.                    
  77.             } );
  78.  
  79.         },
  80.         // credits : http://stackoverflow.com/a/3647634
  81.         _getDir : function( $el, coordinates ) {
  82.            
  83.             // the width and height of the current div
  84.             var w = $el.width(),
  85.                 h = $el.height(),
  86.  
  87.                 // calculate the x and y to get an angle to the center of the div from that x and y.
  88.                 // gets the x value relative to the center of the DIV and "normalize" it
  89.                 x = ( coordinates.x - $el.offset().left - ( w/2 )) * ( w > h ? ( h/w ) : 1 ),
  90.                 y = ( coordinates.y - $el.offset().top  - ( h/2 )) * ( h > w ? ( w/h ) : 1 ),
  91.            
  92.                 // the angle and the direction from where the mouse came in/went out clockwise (TRBL=0123);
  93.                 // first calculate the angle of the point,
  94.                 // add 180 deg to get rid of the negative values
  95.                 // divide by 90 to get the quadrant
  96.                 // add 3 and do a modulo by 4  to shift the quadrants to a proper clockwise TRBL (top/right/bottom/left) **/
  97.                 direction = Math.round( ( ( ( Math.atan2(y, x) * (180 / Math.PI) ) + 180 ) / 90 ) + 3 ) % 4;
  98.            
  99.             return direction;
  100.            
  101.         },
  102.         _getStyle : function( direction ) {
  103.            
  104.             var fromStyle, toStyle,
  105.                 slideFromTop = { left : '0px', top : '-100%' },
  106.                 slideFromBottom = { left : '0px', top : '100%' },
  107.                 slideFromLeft = { left : '-100%', top : '0px' },
  108.                 slideFromRight = { left : '100%', top : '0px' },
  109.                 slideTop = { top : '0px' },
  110.                 slideLeft = { left : '0px' };
  111.            
  112.             switch( direction ) {
  113.                 case 0:
  114.                     // from top
  115.                     fromStyle = !this.options.inverse ? slideFromTop : slideFromBottom;
  116.                     toStyle = slideTop;
  117.                     break;
  118.                 case 1:
  119.                     // from right
  120.                     fromStyle = !this.options.inverse ? slideFromRight : slideFromLeft;
  121.                     toStyle = slideLeft;
  122.                     break;
  123.                 case 2:
  124.                     // from bottom
  125.                     fromStyle = !this.options.inverse ? slideFromBottom : slideFromTop;
  126.                     toStyle = slideTop;
  127.                     break;
  128.                 case 3:
  129.                     // from left
  130.                     fromStyle = !this.options.inverse ? slideFromLeft : slideFromRight;
  131.                     toStyle = slideLeft;
  132.                     break;
  133.             };
  134.            
  135.             return { from : fromStyle, to : toStyle };
  136.                    
  137.         },
  138.         // apply a transition or fallback to jquery animate based on Modernizr.csstransitions support
  139.         _applyAnimation : function( el, styleCSS, speed ) {
  140.  
  141.             $.fn.applyStyle = this.support ? $.fn.css : $.fn.animate;
  142.             el.stop().applyStyle( styleCSS, $.extend( true, [], { duration : speed + 'ms' } ) );
  143.  
  144.         },
  145.  
  146.     };
  147.    
  148.     var logError = function( message ) {
  149.  
  150.         if ( window.console ) {
  151.  
  152.             window.console.error( message );
  153.        
  154.         }
  155.  
  156.     };
  157.    
  158.     $.fn.hoverdir = function( options ) {
  159.  
  160.         var instance = $.data( this, 'hoverdir' );
  161.        
  162.         if ( typeof options === 'string' ) {
  163.            
  164.             var args = Array.prototype.slice.call( arguments, 1 );
  165.            
  166.             this.each(function() {
  167.            
  168.                 if ( !instance ) {
  169.  
  170.                     logError( "cannot call methods on hoverdir prior to initialization; " +
  171.                     "attempted to call method '" + options + "'" );
  172.                     return;
  173.                
  174.                 }
  175.                
  176.                 if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
  177.  
  178.                     logError( "no such method '" + options + "' for hoverdir instance" );
  179.                     return;
  180.                
  181.                 }
  182.                
  183.                 instance[ options ].apply( instance, args );
  184.            
  185.             });
  186.        
  187.         }
  188.         else {
  189.        
  190.             this.each(function() {
  191.                
  192.                 if ( instance ) {
  193.  
  194.                     instance._init();
  195.                
  196.                 }
  197.                 else {
  198.  
  199.                     instance = $.data( this, 'hoverdir', new $.HoverDir( options, this ) );
  200.                
  201.                 }
  202.  
  203.             });
  204.        
  205.         }
  206.        
  207.         return instance;
  208.        
  209.     };
  210.    
  211. } )( jQuery, window );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement