ITT #5: Transparent Overlay with Slide-Out Description
jQuery + CSS to Add a Cool Entry Preview to Your Site
I'm going to try something a little different this week and talk about CSS. I'm working on a client project that needed to overlay a PNG over the top of various thumbnails in order to make the thumbnails sit in nicely with the design. I'd never done anything like this before, so I thought it might be worth sharing.
If you're deeply offended that I've strayed away from programming-related posts, let me know in the comments, but don't worry! I added some jQuery in for good measure!
View the Demo | Download the Files
The Markup
I'm a firm believer in using as few HTML elements and classes as possible. I also really hate seeing empty elements that only exist to have styles applied to them, so I try to make sure that every element I use makes sense in an unstyled document.
For this example, we'll be creating previews of two different blog entries. To easily display all the information necessary to preview the entries, we'll start with a <div> to be a wrapper for all the entries, then create a link for each entry that contains an image tag, plus a span with information about the entry (the title plus a brief description). Here's what we end up with:
<div id="wrapper">
<a href="#" class="overlay">
<img src="images/blogimage.jpg" alt="Image for Blog 01" />
<span>
<strong>Blog Post Number One</strong>
<em>This would be the short description of
the entry, used to entice a user to click through
and read the rest...</em>
</span>
</a>
<a href="#" class="overlay alt">
<img src="images/blogimage2.jpg" alt="Image for Blog 02" />
<span>
<strong>Blog Post Number Two</strong>
<em>This is another description for the
second blog post. Hopefully everyone who sees
this will want to continue reading...</em>
</span>
</a>
</div>
No wasted classes or extra elements, just friendly, descriptive XHTML.
The CSS
The first thing we need to do to style our thumbnails is to reset all the properties of the elements we'll be using. To avoid some of the weird issues that can accompany inline elements, we'll set each element's display property to block, as well as setting the margin and padding to zero.
div, a, span, strong, em, img {
display:block;
margin:0;
padding:0;
}
Next, we want to set the wrapper up to display centered and at the width of our thumbnails. While we're at it, let's define styles for the "overlay" class, which should be the height and width of the thumbnails as well. Because we're going to be moving text around, we'll also set the overflow property to hidden. Then, to get rid of the underline that shows up under links by default, we set the text-decoration property to none. The image will also have a border on it due to the <a> tag, so we reset that as well.
#wrapper {
width:140px;
margin:0 auto;
}
.overlay {
overflow:hidden;
position:relative;
width:140px;
height:140px;
text-decoration:none;
}
.overlay img {
border:0;
}
To get the transparent overlay to sit over the top of the thumbnail, we need to set its position to absolute and align it to the top left of the document. The PNG itself is set as the background image of the span; to make sure it's visible over the top of the image, we add a z-index value of 2000. Also, I added a color property to fit the text into the color scheme I chose for the demo (this is optional -- there are several optional CSS properties I've used, which I've marked in the CSS).
.overlay span {
position:absolute;
top:0;
left:0;
width:140px;
height:140px;
background:url('../images/overlay.png') top left no-repeat;
z-index:2000;
/* Begin optional properties */
color:#333;
/* End optional properties */
}
To control the text, we're going to push the <strong> and <em> tags over 140px (out of the visible area of the "overlay" class) and apply some optional formatting to make them look nice when they're revealed.
.overlay strong {
position:relative;
left:140px;
width:196px;
/* Begin optional properties */
margin:14px 2px 4px;
padding:0 0 1px;
border-bottom:1px dotted #DDD;
/* End optional properties */
}
.overlay em {
position:relative;
left:140px;
width:192px;
/* Begin optional properties */
margin:0 4px;
font-style:normal;
font-size:80%;
/* End optional properties */
}
As a last touch, we want to be able to switch up the overlay to add a little variety to our previews. To do this, we'll define another class called "alt" that will change the background image of the span it encloses. We can then add this class to the <a> tag right along with the "overlay" class (i.e. class="overlay alt") to change the PNG that's laid over the top of the thumbnail.
.alt span {
background-image:url('../images/overlay2.png');
}
With all these rules in place, we should have a nicely styled set of two thumbnails with different overlays. The text isn't visible yet, but we'll get to that in the next step.
Adding jQuery Effects
To show the text for our entries, we need to add a little jQuery. Keep in mind that if JavaScript is disabled, the user can still click on the link; they just won't get the description.
In an attempt to make the entry previews really eye-catching, we'll animate a "sliding" preview when a user hovers over the thumbnail. We'll accomplish this using jQuery's handy-dandy hover() and animate() functions.
$(document).ready(function() {
$('.overlay').hover(function() {
$(this).animate({
width: '340px'
});
}, function() {
$(this).animate({
width: '140px'
});
});
});
And that's it! Make sure you've included the latest build of jQuery at the bottom of your page, right before the script above and the </body> tag.
Finishing Touches
If you've ever used PNGs in a layout, you've probably dealt with the gross way that IE6 handles them. To circumvent this with our entries, we'll have to employ some flavor of PNG fix. I chose to use DD_belatedPNG, as it's really easy to use and doesn't require a blank GIF image to function.
I had luck with this code in FF3, Opera 9.51, Safari 3.2.1, and IE7. IE6 works, but it has some quirks. I chose not to worry about it because, really, it works well enough, and I'm getting a little closer every day to dropping support for IE6 altogether.
View the Demo | Download the Files
I'd love to hear your thoughts in the comments!
Comments for This Entry
Neat effect! My only gripe is that if you move the pointer over each one in quick succession, and then move the pointer completely off of both of them, they still alternate their animations for several iterations.
But then, I'm just goofing around. Hopefully users wouldn't actually do that.
This is a pretty cool technique, but it kind of falls apart when JS is disabled. I know that's a really annoying thing to say because most people will have it enabled, but it should be built as progressive enhancement.
The way I see this working is that you set the width via css then with the JS restrict the width then expand/animate on hover.
That being said, great effect! Keep up the great work!
I did that. @Thomas J Brown
If you didn't want to use an image and instead wanted to use a widget or something that uses script, would this technique still work?
What would need to be changed so that a description would slide out when someone mouse-overs a script object?
@thefigg88:
I think it would still work, but I'm unclear on your meaning.
I know it can be done, and it will very likely be possible using a technique similar to this one.
Let me know if you have any luck!
@Jason
Sorry, I'm a newb at this.
Essentially, I have a < script > code that ends up being a 180x190 size widget for Twitter (http://www.widgetbox.com/widget/twidget).
But I've tried everything I can think of but the sliding description is never to the right of the widget like with your images it's always underneath or below and to the right with the sliding all messed up.
If you could help that would be amazing.
@thefigg88:
That's a little more complicated. The script is adding a Flash element into your page. You'll need to use a developer tool like Firebug to see what the HTML is that's inserted, then use jQuery to manipulate its style.
It may be easier, in this case, to use something Flash-free (there are quite a few Twitter feed-reading JS scripts out there).
Good luck!
Hi,
I'm a newb pure and simple, and I can't seem to get any of the 'demo's on your blog to work :( When I click on one of the demo links such as for this blog enter ( jQuery + CSS to Add a Cool Entry Preview to Your Site) and I click on the Demo links, the link takes me to the opt of your site but i don't see any demo only the beginning of the blog entry. I'm using OS X on a mac, I've tried this in both Firefox and Safari - is it a mac thing? Am I not doing something right?
Any help would be appreciated as it would help me to see what it is that your all talking about.
Thanks - Max
Hello,
It's me again - I tried downloading the files, which downloaded as a .zip file which i can normally unzip, but no matter what I try to do I can't unzip them. Do i need some kind of pc to open the files? Is it not possible to do on a mac (Php stuff is open source yes? Am i being silly or dumb?)
Thanks and sorry to bother - just trying to learn.
-Max
Oh. My. God.
I was able to get it to work to see what it does - which wasn't quite what I thought but it was well worth the process of the attempting to get this to work to see what it is. I learned a smidge about jquery (i.e. that jquery exists), how to install a jquery library and some stuff about pathing - which once i resolved got the code from that posted to work. This is very cool - thanks!
I'd like to see a simple script for uploading an image to gallery - thats my thing - trying to build a little gallery of images, which I hope to incorportate this neat jquery thing into.
Thanks again!
@Max:
Sorry you had trouble with the demos. I just updated the guts of my blog, and a few things are still out of whack. Thanks for pointing that out!
Also, I've got an older tutorial on uploading images that might help out: http://ennuidesign.com/blog/SimpleCMS+Part+2+-+Images/
That goes into object-oriented programming, which may seem confusing at first. I've got another article that introduces the topic here: http://ennuidesign.com/blog/ITT+%2310%3A+Understanding+OOP/
Good luck!
Post a Comment
Want to show your face? Get a gravatar!