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

66 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

    • phphunger Author Editor

      im trying to implement the same for one of my flash games portal..i am trying this same using only css and the javascript onmouseover event..but any how thanks for providing such a great solution like this…really jQuery rocks…

      June 22, 2012 · 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

      • mayWz Author Editor

        Thank you! It help me a lot. ;-D

        July 4, 2012 ·

  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

  30. Dheeraj Author Editor

    Really amazed to know that jQuery can work in this way also.
    Great work !
    Thanks …

    June 9, 2012 · Reply

  31. phphunger Author Editor

    exactly what i am looking for this…cool work…great!!!!

    June 22, 2012 · Reply

  32. Christian Louboutin Author Editor

    Don’t forget the things you once you owned. Treasure the things you can’t get. Don’t give up the things that belong to you and keep those lost things in memory.  

    June 28, 2012 · Reply

  33. François Author Editor

    Hi, thanks for your work,

    I just implemented the script on my page. It works but sometimes the animation blocks for a second or two. Even in your demo page the animation blocks like this sometime.

    Is it normal. Is there anything that can be done to make this more fluid ?

    Another question : Can I get a fadein effet ?

    July 3, 2012 · Reply

    • Pedro Author Editor

      Hello François, did you try to get the fade effect between slides?
      Thanks

      October 24, 2012 · Reply

  34. Ash Author Editor

    Very interesting, good work.

    July 13, 2012 · Reply

  35. saha Author Editor

    Nice sharing. Very informative tutorial. jQuery is awesome. Thanks for sharing this post and code snippets.

    July 19, 2012 · Reply

  36. Bob Author Editor

    anyone else experiencing problems with this in IE8??? It works in all other browsers for me except that.

    August 28, 2012 · Reply

  37. freelance web designer kerala Author Editor

    Nice Work….. Thanks…. :)

    September 19, 2012 · Reply

  38. Rachel Author Editor

    Thanks for the tutorial–works great. Just one question, is there a way to slow down the slideshow? Thanks!

    September 27, 2012 · Reply

  39. Mike Author Editor

    I was just wondering the same thing, Rachel.

    September 27, 2012 · Reply

  40. Mike Author Editor

    Figured it out. The “speed” option is set in milliseconds. speed = 1000 will cycle the slides once every second.

    September 27, 2012 · Reply

  41. manona Author Editor

    thanks a lot!
    one question: i have three links for “slides”, “slides2″ and “slides3″
    я делала
    // Cycle
    $(‘.slides’).cycle({ fx: ‘none’, speed: 1, timeout: 70 }).cycle(“pause”);
    // Pause & play on hover
    $(‘.link1′).hover(function(){ $(‘.slideshow-block’).find(‘.slides’).addClass(‘active’).cycle(‘resume’); }, function(){ $(‘.slideshow-block’).find(‘.slides’).removeClass(‘active’).cycle(‘pause’); });

    // Cycle $(‘.slides2′).cycle({ fx: ‘none’, speed: 1, timeout: 70 }).cycle(“pause”);
    // Pause & play on hover $(‘.link2′).hover(function(){ $(‘.slideshow-block’).find(‘.slides2′).addClass(‘active’).cycle(‘resume’); }, function(){ $(‘.slideshow-block’).find(‘.slides2′).removeClass(‘active’).cycle(‘pause’); });

    // Cycle $(‘.slides3′).cycle({ fx: ‘none’, speed: 1, timeout: 70 }).cycle(“pause”);
    // Pause & play on hover $(‘.link3′).hover(function(){ $(‘.slideshow-block’).find(‘.slides3′).addClass(‘active’).cycle(‘resume’); }, function(){ $(‘.slideshow-block’).find(‘.slides3′).removeClass(‘active’).cycle(‘pause’); });

    Chrome show me images of “slides”, after “refresh page” show images of all: “slides”, “slides2″ and “slides3″, Firefox show images of “slides” only… where is mistake?

    October 11, 2012 · Reply

  42. Liita Author Editor

    Thanks for the tut.

    Just letting you know that I’m getting a malware warning from Google ( see : i46.tinypic.com/5l9sab.png ) when trying to reach this page.
    (I’m glad that this time I ignored it, and just clicked through.)

    October 29, 2012 · Reply

  43. mobile app development Author Editor

    jQuery is really amazing stuff. But I was aware of very basic codes & effects. Now this post will help me to learn more advance things.

    November 9, 2012 · Reply

  44. آموزش زبان Author Editor

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

    November 10, 2012 · Reply

  45. medafighterX Author Editor

    hello! is it possible to use a div instead of the image? I want the list to work on a div as well so that I could use a background image instead

    November 20, 2012 · Reply

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

    hello! very niceeeeeeeeeee!!!!!!

    December 8, 2012 · Reply

  47. Ink Cartridges Author Editor

    I like it!

    December 10, 2012 · Reply

  48. خرید آپارتمان در استانبول Author Editor

    this is funny design,but i like this one.maybe i use it

    December 29, 2012 · Reply

  49. Bill Author Editor

    Much mahalos for this my friend

    February 5, 2013 · Reply

  50. mkaya Author Editor

    Hi,thanks for ur sharing. I did in my site but it did not work in internet explorer How can i solve that?

    March 1, 2013 · Reply

  51. David Author Editor

    Hi,

    is there a way to run the animation automatically on page load and stop the animation on a certain image?

    Thanks!

    March 5, 2013 · Reply

  52. درب اتوماتیک Author Editor

    very goof thanks admin

    March 7, 2013 · Reply

  53. Ankit Jain Author Editor

    Awesome

    March 9, 2013 · Reply

  54. Graphic Designer Vancouver Author Editor

    Sam you have provided an awesome tutorial. However, this does not work for IE; if you can provide some solution, please let me know. I will be thankful to you.

    April 2, 2013 · Reply

  55. طراحی سایت Author Editor

    good. tnx

    April 3, 2013 · Reply

  56. Ashley Author Editor

    Hey there! Way awesome. Just what I was looking for.
    Any way to make it stop on a certain frame?
    I want it to start cycling through the images on load and then when the user clicks/hovers, I want it to stop (permanently)on the frame that I have assigned.
    Please help, I need this immediately :)

    April 25, 2013 · Reply

  57. طراحی سایت|برنامه نویسی|بهینه سازی سایت|آموزش|مقاله|کامپیوتر Author Editor

    We’re a gaggle of volunteers and starting a brand new scheme in our community. Your website offered us with useful information to paintings on. You’ve performed a formidable job and our entire community might be thankful to you.

    April 29, 2013 · Reply

  58. abhay Author Editor

    how to add fade in fade out effect in this plugin

    May 13, 2013 · Reply

    • Liita Author Editor

      It’s in the jQuery docs. With examples : api.jquery.com/category/effects/fading/

      May 13, 2013 · Reply

  59. Kalyan Author Editor

    How to make my first image as default visible image.?

    May 18, 2013 · Reply

  60. david Author Editor

    really great coding !

    just a question : Can the images from the animation load ONLY WHEN the hover is effective ?

    cause when using this per example 20 times on one page, plus an average of 10 pics per set (20×10=200 thumbs)… is starting to get heavy and are not good for page request.

    regards !
    David

    May 23, 2013 · Reply

 

Join the Conversation

Back to Top / Comment RSS

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