<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>designing4u.de &#187; image caption</title>
	<atom:link href="http://www.designing4u.de/tag/image-caption/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Simple image caption with jQuery</title>
		<link>http://www.designing4u.de/2010/05/simple-image-caption-with-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=simple-image-caption-with-jquery</link>
		<comments>http://www.designing4u.de/2010/05/simple-image-caption-with-jquery/#comments</comments>
		<pubDate>Thu, 06 May 2010 10:52:43 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[image caption]]></category>
		<category><![CDATA[object oriented javascript]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=55</guid>
		<description><![CDATA[Displaying a transparent caption over an image is a nice way to show the visitors what a particular image is about. In this tutorial I will show you how you can accomplish that with less than 50 lines of jquery without downloading any fully featured plug-ins. I will also try to accomplish it in an [...]]]></description>
			<content:encoded><![CDATA[<p>Displaying a transparent caption over an image is a nice way to show the visitors what a particular image is about. In this tutorial I will show you how you can accomplish that with less than 50 lines of jquery without downloading any fully featured plug-ins. I will also try to accomplish it in an object oriented way. I tested it in Opera 10.01, Firefox 3.5.1, IE6, IE7 and IE8 and it works great.</p>
<p>You can see a working demo of this tutorial on this <a href="http://www.designing4u.de/examples/image-caption/index.html">page.</a></p>
<p><span id="more-55"></span></p>
<p>Let's start with the CSS and HTML markup. To make this script work you will need to follow the structure of a box for every single image. Create a div container with the CSS class box. Using this class our script will know where to register a hover event for images. Paste an image and another div container with the CSS class caption as children. The CSS for this example is pretty straight forward. Position the box container relatively and the caption container absolutely. To get a nice transparent effect I used opacity.</p>
<h4>CSS Code</h4>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>    
<span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>    
<span style="color: #6666ff;">.caption</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
    opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.8</span><span style="color: #00AA00;">;</span>
    filter<span style="color: #00AA00;">:</span> alpha<span style="color: #00AA00;">&#40;</span>opacity<span style="color: #00AA00;">=</span><span style="color: #cc66cc;">80</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>    
<span style="color: #6666ff;">.caption</span> div <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>    
img <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<h4>HTML Code</h4>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;box&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;img.jpg&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;caption&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The JavaScript part is a little bit more interesting. In the second line create a new object. For this object I created two methods.</p>
<h4>JavaScript Code</h4>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> caption <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    getCaptionHeight<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> imageHeight <span style="color: #339933;">=</span> e.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #003366; font-weight: bold;">var</span> captionHeight <span style="color: #339933;">=</span> e.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.caption'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>captionHeight <span style="color: #339933;">&gt;</span> imageHeight<span style="color: #009900;">&#41;</span>
            captionHeight <span style="color: #339933;">=</span> imageHeight<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> captionHeight<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    register<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>clas<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span>clas<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> captionHeight <span style="color: #339933;">=</span> caption.<span style="color: #660066;">getCaptionHeight</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> realCaptionHeight <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.caption'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.caption'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bottom'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span> realCaptionHeight <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.caption'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#123;</span>
                    bottom<span style="color: #339933;">:</span> <span style="color: #3366CC;">'+='</span> <span style="color: #339933;">+</span> captionHeight
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
                <span style="color: #CC0000;">300</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> captionHeight <span style="color: #339933;">=</span> caption.<span style="color: #660066;">getCaptionHeight</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.caption'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#123;</span>
                    bottom<span style="color: #339933;">:</span> <span style="color: #3366CC;">'-='</span> <span style="color: #339933;">+</span> captionHeight
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
                <span style="color: #CC0000;">300</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
caption.<span style="color: #660066;">register</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Method getCaptionHeight will be responsible for retrieving the height of the caption. In this method I compared the height of the caption with the height of the image and in the case that the caption is higher, I reduced its height to equal the height of the image. This will prevent long captions going out of the boundaries of the image. I will later use this information for the animation.</p>
<p>The second method registers a hover event for all elements matching the CSS selector provided as the argument. In the hover event of the first argument I created a closure, which retrieves the height of the caption using the previously mentioned method and positions it below the image using the real caption height. After that the animation fires up and the caption is shown. As the second argument for hover event I created another closure. Here I do the same thing as in the first closure but in the reverse order. The animations fires up again but this time the caption will be hidden.</p>
<p>In the last line I just register all HTML elements where this event should be registered (or bound to), in my case, the div container with the CSS class box. You can specify more elements if you want, as long as you follow the selectors specification of jQuery. I understand that this method is not perfect, therefore I'm waiting for your comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2010/05/simple-image-caption-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

