lauthiamkok
09-23-2009, 01:35 AM
Hi,
I made this code to create image caption from the alt sometime ago. But recently I notice there is a bug or an error.
The width of the div of the image caption should be following the width of the individual image. It picks up the width of each images successfully. But it only take the width of the last image when the code has ran completely.
This is the html code,
<div id="content">
<p><img src="bw1.jpg" alt="Ut wisi enim ad minim veniam" width="300" /></p>
<p><img src="bw2.jpg" alt="pic 2" width="550"/></p>
<p><img src="lights1.jpg" alt="pic 3" width="500"/></p>
</div>
this is the jquery,
$(document).ready(function(){
$("#content img").each(function () {
var imgwidth = $(this).width();
var title = $(this).attr("title");
var alt = $(this).attr("alt");
alert(imgwidth);
$("<div class='image_description'>"+"<span class='image_title'>"+ title +"</span>"+"<span class='image_alt'>"+ alt +"</span>"+"</div>").insertAfter(this);
$('.image_description').css({
color: '#666666',
fontSize: '11px',
margin: '0px 0px 0px 0px',
clear: 'both',
width: imgwidth
});
$('.image_title').css({
fontStyle: 'italic'
});
});
});
this is the live link,
http://lauthiamkok.net/tmp/jquery/jquery.image_caption/
Many thanks if u know why...
Thanks,
Lau
I made this code to create image caption from the alt sometime ago. But recently I notice there is a bug or an error.
The width of the div of the image caption should be following the width of the individual image. It picks up the width of each images successfully. But it only take the width of the last image when the code has ran completely.
This is the html code,
<div id="content">
<p><img src="bw1.jpg" alt="Ut wisi enim ad minim veniam" width="300" /></p>
<p><img src="bw2.jpg" alt="pic 2" width="550"/></p>
<p><img src="lights1.jpg" alt="pic 3" width="500"/></p>
</div>
this is the jquery,
$(document).ready(function(){
$("#content img").each(function () {
var imgwidth = $(this).width();
var title = $(this).attr("title");
var alt = $(this).attr("alt");
alert(imgwidth);
$("<div class='image_description'>"+"<span class='image_title'>"+ title +"</span>"+"<span class='image_alt'>"+ alt +"</span>"+"</div>").insertAfter(this);
$('.image_description').css({
color: '#666666',
fontSize: '11px',
margin: '0px 0px 0px 0px',
clear: 'both',
width: imgwidth
});
$('.image_title').css({
fontStyle: 'italic'
});
});
});
this is the live link,
http://lauthiamkok.net/tmp/jquery/jquery.image_caption/
Many thanks if u know why...
Thanks,
Lau