Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

Cycle Through Images on Hover with jQuery

A quick way to add a hover responsive, fast cycling, jQuery powered slideshow to your site.

The Basics

I first saw this style slideshow as a Flash solution on Cargo Collective, which has since been updated to Javascript. As it turns out, it’s actually quite easy, making it a quick addition to any site you wish to implement it on.

Here’s what we’ll be doing:

  • Super fast slideshow that only runs when hovered over.
  • Overlay logo that toggles between two different states depending on hover state
  • Inspiration piece for this tutorial: Cargo Collective
  • See our end goal by visiting our demo page.

The HTML

This is the structure for each slideshow, with the images are pulled from the unordered list and turned into a slideshow by jQuery Cycle Plugin.

The .link element is where a URL can be provided for when the slideshow is clicked. It is also how we will swap the logo on hovers.

<!-- Slideshow
=========================-->
<div class="slideshow-block">
	<a href="http://website.com" class="link"></a>
	<ul class="slides">
		<li><img src="image.jpg"/></li>
		<!-- Any other slides -->
	</ul>
</div>

The CSS

The dimensions for my slideshow are 200px by 150px. Depending on the sizes of your logo and slides, the below background position coordinates and overall dimensions may need to get adjusted.

  • The logo is toggled between the two color variations using the background position hover technique.
  • The unordered  slides list is given a class of “active” when hovered over, making it visible on the page.
*{
	margin: 0;
	padding: 0;
	outline: none;
	border: none;
}
.slideshow-block{
	position: relative;
	width: 200px;
	height: 150px;
	overflow: hidden;
	background: #111 url('img/bg.jpg');
}
a.link{
	position:absolute;
	height: 150px;
	width: 200px;
	display: block;
	z-index: 10;
	background: url('img/logo.png') no-repeat center top;
}
a.link:hover{
	background-position: center -150px;
}
.slides{
	z-index:0;
	visibility:hidden;
}
.slides.active{
	visibility:visible;
}

The jQuery

On the jQuery side, we want to accomplish two tasks:

  1. Tie the jQuery Cycle Plugin to our list of images, turning them into a slideshow.
  2. Swap between pause and play states when the slideshow is hovered over. This means the slideshow is hidden and paused when the mouse is not over it.
<script type="text/javascript">
jQuery(function($){

	// Cycle plugin
	$('.slides').cycle({
	    fx:     'none',
	    speed:   1,
	    timeout: 70
	}).cycle("pause");

	// Pause & play on hover
	$('.slideshow-block').hover(function(){
		$(this).find('.slides').addClass('active').cycle('resume');
	}, function(){
		$(this).find('.slides').removeClass('active').cycle('pause');
	});

});
</script>

Closing

I’ve included two versions of the script, one with the TV and one without (for easier copy and paste implementation). As always, when in doubt, try using the downloadable demos as a foundation to build from.

I look forward to seeing what folks are able to make happen – enjoy!

Note: You are able to place multiple slideshows on the same page, meaning it’s feasible for you to make a gallery out of a series of thumbnail slideshows.

Wordpress.com stats not installed! Posted Tuesday, September 27th, 2011 / Back to Top

I this post. Tweet
SPONSOR

31 Comments 3 Mentions

  1. Todd Author Editor

    I decided over this past weekend to try and rebuild that hover cycle on Cargo Collective and implement it in my new site redesign so I was delighted to see this while browsing around in class. Awesome tutorial and Thanks a bunch.

    September 27, 2011 · Reply

  2. Tutoriaux Webmasters Author Editor

    I”l try to implement it in my new project !

    September 28, 2011 · Reply

  3. Amir Author Editor

    I really like

    September 28, 2011 · Reply

  4. Paul Author Editor

    Why are the images all crazy like that? ;-)

    September 28, 2011 · Reply

  5. Marko Wsd Author Editor

    OMG, I have a great use for this, check my website, I got some TV as well, if i put it inside the T.V to create the effect, thats a first.

    I`ll start working on this matter and see the end result :) hopefully it`ll be like this.

    September 30, 2011 · Reply

  6. Dinesh Author Editor

    Really impressive.

    September 30, 2011 · Reply

  7. AndyiBM Author Editor

    Not sure I’d ever have a use for it, but it made me laugh – crazy TV! Thanks.

    October 5, 2011 · Reply

  8. hugo Author Editor

    Smart! Love it! But it would be more useful if it would used sprites for this…

    October 14, 2011 · Reply

  9. Jordan Author Editor

    This is great! thanks for showing sam!

    October 15, 2011 · Reply

  10. Lutfie Tutorial Blog Author Editor

    woww its veri nice thanks for all

    October 22, 2011 · Reply

  11. moedas pw Author Editor

    You have to believe in yourself . That’s the secret of success .

    November 2, 2011 · Reply

  12. rift platinum Author Editor

    You have to believe in yourself . That’s the secret of success

    November 2, 2011 · Reply

  13. Selim Author Editor

    Newbie to this.. Anyone know how I can get this to start on load vs on hover? I couldn’t find anything on the cycle site.

    $(‘.slideshow-block’).hover(function()

    Thanks

    November 15, 2011 · Reply

  14. painting contractors in chennai Author Editor

    i really like this post & i try to do with my site

    November 21, 2011 · Reply

  15. طراحی سایت و سئو Author Editor

    this post help me… tnks a lot

    December 5, 2011 · Reply

  16. Rehana Sulthan Author Editor

    This is really a cool and interesting query with Java. Like you & Java.
    Thanks for wonderful query.

    December 5, 2011 · Reply

  17. Geoff Author Editor

    Is there a way to reset/restart the “slideshow” from the first image on mouseout (instead of pausing)?

    December 8, 2011 · Reply

    • Geoff Author Editor

      I found it:

      $(this).find(‘.slides’).removeClass(‘active’).cycle(‘stop’).cycle({
      startingSlide: 0
      });

      December 9, 2011 · Reply

  18. Stvn Author Editor

    Is there anyway to remove the hovered over. Meaning once login it will just play instead of hovered over. Please help

    December 15, 2011 · Reply

    • DarthMegan Author Editor

      // Pause & play on hover
      $(‘.slideshow-block’).hover(function(){

      change the .hover() to .ready(). That tells it to start when the page has loaded.

      May 10, 2012 · Reply

  19. felipe Author Editor

    this blog will not be update anymore?

    January 6, 2012 · Reply

  20. Khalid Author Editor

    Where did you get that tv from? Can we use it (for a non-commercial purpose in my case)?

    January 8, 2012 · Reply

  21. Michael Author Editor

    Thanks for the tutorial. Can’t believe I have never seen this website before – love the design and layout.

    January 25, 2012 · Reply

  22. Chris Author Editor

    Wow mate, really cool, and unique! I agree I havn’t seen this before like this, yet somewhat such a simple concept :)

    February 6, 2012 · Reply

  23. Beats by Dr Author Editor

    Wow,so many people like you post, so nice

    February 13, 2012 · Reply

  24. Jay Jalaram Extrusions Author Editor

    Awesome 1 Sam Dunn, great posting

    February 16, 2012 · Reply

  25. jacob Author Editor

    That effect was used on entry page at lightandmotion.com awhile back.

    February 28, 2012 · Reply

  26. Forestry Truck Author Editor

    what a neat effect. definitely gave me a lot of ides to work with

    February 29, 2012 · Reply

  27. شات صوتي Author Editor

    You are great and thank for you articles

    March 6, 2012 · Reply

  28. Mark Author Editor

    Great work. A really neat effect. I like using JavaScript to do things you wouldn’t normally see on a standard web page. Thanks for the demo.

    March 22, 2012 · Reply

  29. Brushstroke Design Author Editor

    Nice work, I absolutely love JavaScript, in my opinion, with the way things are going, it won’t be long before Flash is completely gone.

    May 1, 2012 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2012 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top