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

Sliding Boxes and Captions with jQuery

Sliding Boxes and Captions with jQuery

This is now a plugin! Check out the announcement post for the Mosaic jQuery plugin. Check the project page for the latest release notes and features!

The Basic Idea

All of these sliding box animations work on the same basic idea. There is a div tag (.boxgrid in my css) that essentially acts as a window where two other items of your choosing “peek” through.

Confused? Cue the helpful diagram -slidebreakdown

From this basic idea we can play around with animations of the sliding element to either show or cover up the viewing area, thus creating the sliding effect.

Step 1 – CSS Foundation Work

Given the basic structure outlined in the helpful image above, we will need to use a little bit of CSS to make it work as intended. The following will make it functional -  review my complete stylesheet in the downloadable file.

The following defines the viewing window (.boxgrid) and sets the default position for images within it to the top left. This is important to make the overlap while sliding work. Dont’ forget that overflow:hidden makes this all possible.

.boxgrid{
	width: 325px;
	height: 260px;
	margin:10px;
	float:left;
	background:#161613;
	border: solid 2px #8399AF;
	overflow: hidden;
	position: relative;
}
.boxgrid img{
	position: absolute;
	top: 0;
	left: 0;
	border: 0;
}

If you aren’t using the semi-transparent captions you are done with CSS – move to Step 2.

.boxcaption{
	float: left;
	position: absolute;
	background: #000;
	height: 100px;
	width: 100%;
	opacity: .8;
	/* For IE 5-7 */
	filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80);
	/* For IE 8 */
	-MS-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";
 	}

Opacity that plays nice in all browsers is a rough topic, educate yourself if you need to.

Now we’ll need to set up the default starting point for the caption box. If you want it fully hidden initially, you will want the distance from the top or left to match the height or width of the window (.boxgrid), depending on which direction it will be sliding. You can also have it partially visible initially, as .caption .boxcaption illustrates.

 .captionfull .boxcaption {
 	top: 260;
 	left: 0;
 }
 .caption .boxcaption {
 	top: 220;
 	left: 0;
 }

Step 2 – Adding the Sliding Animations

This next stage is a matter of choosing which animation suites you, I have included a bunch of pre-formatted potentials to help you along. Play around with them to find one that fits your needs and style.

$(document).ready(function(){
	//To switch directions up/down and left/right just place a "-" in front of the top/left attribute
	//Vertical Sliding
	$('.boxgrid.slidedown').hover(function(){
		$(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
	});
	//Horizontal Sliding
	$('.boxgrid.slideright').hover(function(){
		$(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
	});
	//Diagnal Sliding
	$('.boxgrid.thecombo').hover(function(){
		$(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
	});
	//Partial Sliding (Only show some of background)
	$('.boxgrid.peek').hover(function(){
		$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
	});
	//Full Caption Sliding (Hidden to Visible)
	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
	});
	//Caption Sliding (Partially Hidden to Visible)
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
	});
});

Step 3 – The HTML

There are a few classes that we created simply as selectors for JQuery. Keep these rules in mind:

  • The div class “.cover” should be assigned to whatever is doing the sliding/movement.
  • Within the div .boxgrid, the img should always come first.

Here’s an example of the HTML I would use for the .captionfull animation:

<div class="boxgrid captionfull">
	<img src="jareck.jpg"/>
	<div class="cover boxcaption">
		<h3>Jarek Kubicki</h3>
		<p>Artist<br/><a href="http://www.nonsensesociety.com/2009/03/art-by-jarek-kubicki/" target="_BLANK">More Work</a></p>
	</div>
</div>

Go Forth and Create

I’ve only touched upon a few options you have, these boxes are your canvases, create away. I would encourage you to download the attached files, as it may be easiest to just copy-paste the parts relevant to your project.

Be sure to post any questions, thoughts, or cool things you’ve done in the comments below!

Wordpress.com stats not installed! Posted Tuesday, March 31st, 2009 / Back to Top

I this post. Tweet
SPONSOR

449 Comments 495 Mentions

  1. Karl Author Editor

    Hi, it was all working well until I wanted to put some clickable image buttons beneath the ‘peek’ image, but now my buttons just show on top of the image image instead of getting revealed behind it :(

    November 7, 2011 · Reply

    • SHIVENDU Author Editor

      Use z-index properties

      January 18, 2012 · Reply

  2. Karl Author Editor

    never mind, it was because i had the “cover” before by button images.

    ##### Also, dont put your real email address in this comment box, the day after i had 4 emails bounce back from spam sent out using my address, i’ve had nothing in the 5/6 years before using this site. #####

    November 8, 2011 · Reply

  3. Carpet Cleaning Miami Author Editor

    Some quick tips for common CSS issues and how to avoid easy pitfalls during your CSS coding process.

    November 11, 2011 · Reply

  4. Helen Author Editor

    Awesome, Thank you :)

    November 12, 2011 · Reply

  5. manoj Author Editor

    hi

    November 16, 2011 · Reply

  6. Eileen Author Editor

    Hi, is there any demo or download files?

    November 20, 2011 · Reply

  7. ZR Author Editor

    not working=(

    November 21, 2011 · Reply

  8. Dan Author Editor

    I am able to get the effect I’m looking for in all browsers but IE. When I open the page in IE 8 specifically I get a warning blocking all scripts or Active X controls. Any ideas why this is happening?

    November 21, 2011 · Reply

    • joe Author Editor

      I found that it was because of the alpha channel filter css property. Strange… Just remove:

      filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80)
      -MS-filter: “progid:DXImageTransform.Microsoft.Alpha(Opacity=80)”

      You won’t have transparency in IE but it will not throw the warning/block.

      December 9, 2011 · Reply

  9. Pino Author Editor

    thanks! will try it now!

    November 21, 2011 · Reply

  10. Wczasy Author Editor

    Hi
    thanks!

    November 22, 2011 · Reply

  11. Dien dan SEO Author Editor

    Thanks for this article, I can use for future projects

    November 25, 2011 · Reply

  12. Idris Author Editor

    really interesting and useful…thank you for sharing!!!

    November 26, 2011 · Reply

  13. josan Author Editor

    Nice tutorial and very useful for my homepage

    November 29, 2011 · Reply

  14. SEO Bod Author Editor

    Great gallery, easy to follow tutorial also. I’ll be using these galleries again and again, thanks.

    December 2, 2011 · Reply

  15. raj Author Editor

    hi,
    this nice…

    December 9, 2011 · Reply

  16. centrifuge in india Author Editor

    excellent thats very nice effects found here

    December 10, 2011 · Reply

  17. Dalin UM Author Editor

    that very cool buddy

    December 12, 2011 · Reply

  18. led ekran Author Editor

    thnx for sharing

    December 15, 2011 · Reply

  19. Robin Author Editor

    this is great it’s work fine with me in firefox thanks

    December 21, 2011 · Reply

  20. glenn Author Editor

    excellent stuff,loved it and works in ie.

    December 22, 2011 · Reply

  21. Golf Author Editor

    Thank

    December 25, 2011 · Reply

  22. Nigeria Web design Author Editor

    WOW thanks for the toutorial. I’ll be trying that out on my project.

    December 30, 2011 · Reply

  23. King Author Editor

    this would make a excellent featured slider, thanks!

    January 1, 2012 · Reply

  24. how to get a free ipad 2 no offers Author Editor

    100 % free samples scientifically referred to as the free gifts are a approach used by the firms to judge the desire of the merchandise as well as gauge the actual response on their brand-new product by the individuals just before they start a full-scale current market kick off of the products. These are generally the actual samples that could be found by the people oftentimes free of charge as well as at marked down costs and therefore provided via the internet for all.

    January 4, 2012 · Reply

  25. Mike Author Editor

    In it’s current form this wouldn’t work when the boxgrid is flexible for a responsive site…any ideas if this effect is still possible?

    January 6, 2012 · Reply

  26. Jo Author Editor

    Hi i was wondering: is there a way to have the initial position of the slide the same as “after the hover”?

    thanks!

    January 7, 2012 · Reply

  27. Kathleen Ely Author Editor

    Is it possible to put multiple links in the caption area or are all A HREF just defined on the underlying image?

    TIA

    January 8, 2012 · Reply

  28. Erwin Author Editor

    Demo ???????
    Unbelievable there isn’t one.
    No ONE will use this then.

    January 12, 2012 · Reply

    • Sam Dunn Author Editor

      Hey there – it seems like you missed the “See Demo” button in the upper left sidebar.

      January 12, 2012 · Reply

      • morteza Author Editor

        thank’s, but there isn’t good place for demo link.
        and it isn’t searchable by browser find.

        January 14, 2012 ·

      • Sam Dunn Author Editor

        Top left sidebar

        January 14, 2012 ·

  29. Sonu Author Editor

    Hello Sam,

    I’m trying the Full Caption Sliding (Hidden to Visible) effect

    This is not working in IE 7. It works in all other browsers, including IE 8.

    January 12, 2012 · Reply

  30. darealya Author Editor

    Hey there, thanks Sam for sharing, awesome sample.
    Could you please help me:
    I would like to change the sliding to a text content sliding
    Like http://www.apple.com/fr/why-mac/, is it possible?

    Thanks a lot!

    January 13, 2012 · Reply

  31. LM Author Editor

    Sigh, the only complaint I have is that when I get an error stating I didn’t fill out the right fields, that it not jump to a new page and completely eliminate what I’d written.

    Anyway, this is just what I’ve been looking for. My design is laid out in a way that the gallery has little dedicated space – it’s a minor attribute, unlike most galleries. However, it also needs textual descriptions. Thus, this is the perfect solution.

    Excellent share, Sam. Thank you.

    January 17, 2012 · Reply

  32. Juan Author Editor

    Hi, Please I need help to install.

    I try to install with WordPress’s plugin installer and the installation works, but when I try to activate the Mosaic plugin I obtains this message “Bad header”.

    WordPress version: 3.2.1
    OS: Debian 6.
    Web Server: Apache 2.2.16

    PD: I can’t upgrade my WordPress to the last version, because my web hosting only provides WordPress 3.2.1

    please sorry my bad english.
    Thanks.
    Juan

    January 19, 2012 · Reply

  33. gf Author Editor

    hg

    January 20, 2012 · Reply

  34. Stock Market Author Editor

    Awesome query.must use it. It’s changing the look of our site.

    http://www.sirfpaisa.com/stock-market-guide.php

    January 20, 2012 · Reply

 

Join the Conversation

Back to Top / Comment RSS

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