Direction-Aware Hover Effect with CSS3 and jQuery

How to create a direction-aware hover effect using CSS3 and jQuery. The idea is to slide in an overlay from the direction we are moving with the mouse.
Direction Aware Hover Effect

From our sponsor: Front-end developers, save time and convert designs into clean React, Vue and HTML code using Anima

In today’s tip we’ll show you how to create a direction-aware hover effect using some CSS3 goodness and jQuery. The idea is to have a little overlay slide in on top of some thumbnails from the direction that we are coming from with the mouse. When we “leave” the element, the overlay will slide out to that direction, following our mouse. This will create an interesting-looking effect.

We’ll use an unordered list for the thumbnails and the description overlays:

<ul id="da-thumbs" class="da-thumbs">
		<a href="">
			<img src="images/7.jpg" />
			<div><span>Natalie & Justin Cleaning by Justin Younger</span></div>
		<!-- ... -->
	<!-- ... -->

The list items will be floating left and have a relative positioning because we will make the description overlay absolute:

.da-thumbs li {
	float: left;
	margin: 5px;
	background: #fff;
	padding: 8px;
	position: relative;
	box-shadow: 0 1px 3px rgba(0,0,0,0.1);
.da-thumbs li a,
.da-thumbs li a img {
	display: block;
	position: relative;
.da-thumbs li a {
	overflow: hidden;
.da-thumbs li a div {
	position: absolute;
	background: rgba(75,75,75,0.7);
	width: 100%;
	height: 100%;

What we will do is the following: depending on the place we are entering with the mouse, we’ll apply the respective “from” style which will set the correct initial position of the overlay. Then we will apply the transition and then we add the final state style, so that the overlay slides in. When we leave the element, we will again apply the respective “from” style (although now we are actually sliding out) and remove the previous final state style.

So, the heart of our little plugin is the following part:

this.$el.on( 'mouseenter.hoverdir, mouseleave.hoverdir', function( event ) {
	var $el = $( this ),
		$hoverElem = $el.find( 'div' ),
		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() {
			$ 0, function() {
				var $el = $( this );
				if( ) {
					$el.css( 'transition', self.transitionProp );
				self._applyAnimation( $el,, self.options.speed );

			} );
		}, self.options.hoverDelay );
	else {
		if( ) {
			$hoverElem.css( 'transition', self.transitionProp );
		clearTimeout( self.tmhover );
		self._applyAnimation( $hoverElem, styleCSS.from, self.options.speed );
} );

We basically bind the ‘mouseenter’ and ‘mouseleave’ event to the list item and with the function _getDir we’ll get the direction we are moving in or out (imagine the “detection” area for each direction as a rectangle divided into four triangles).

You’ll see that in the second demo we’ve added a little delay so that we don’t have much animations going on when we move the mouse from an extreme corner to another one.

I hope you enjoyed this little effect and find it useful!

If CSS transitions are not supported the animation will fallback to the jQuery animate

Tagged with:

Mary Lou

ML is a freelance web designer and developer with a passion for interaction design. She studied Cognitive Science and Computational Logic and has a weakness for the smell of freshly ground peppercorns.

Stay up to date with the latest web design and development news and relevant updates from Codrops.

Feedback 59

Comments are closed.
  1. Maybe sound inappropriate but this is SICK!
    Fresh design and amazing idea for an online portfolio

    • Hi theodore! We actually got inspired by an existing portfolio where a second image is used instead of a description:
      Colorz | Creative Digital Agency
      I think with an overlay it seems a bit more fluid 🙂
      Thanks a lot and cheers, ML

  2. As always, this website amazes me with these high-quality tutorials. This is great, guys, thanks a lot! 😀

  3. This was very interesting. I love the effect. It is a little lagged at times, but otherwise effective.

  4. Damm Mary, im in love with u 😛 u always come up with so good idea’s 🙂 😀

  5. First off this is a excellent tutorial, great work on this Mary.

    This might be a very dumb question, but what does the “_”s mean before self?

  6. It’s interacting with Quicksand (quicksand is blocking this hover effect),
    Is there a way to integrate both of ’em?

  7. Hello Mary,
    Very nice tutorial, I’ve learnt something new today. Please do more like this!
    Keep up the good work:)

  8. Mary, you’re awesome ! Nice effect, well explained… ! Is there a way to make it work with :hover AND :focus statement (Accessibility purpose) ?