Extracting caption from alt attribute and inserting into a div
I am using the rather lovely jQuery slideviewer 1.1 plugin on a site at the moment, but would like to extract the alt attribute from images displayed and insert them into a div at the appropriate time.
The current code for the plugin is shown below for reference:
jQuery(function(){
jQuery("div.svw").prepend("<img src='/template/theme/designdistillery/img/bg-portfolio-loading.gif' class='ldrgif' alt='loading...'/ >");
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
settings = jQuery.extend({
easeFunc: "easeInOutExpo",
easeTime: 750,
toolTip: false
}, settings);
return this.each(function(){
var container = jQuery(this);
container.find("img.ldrgif").remove(); // removes the preloader gif
container.removeClass("svw").addClass("stripViewer");
var pictWidth = container.find("img").width();
var pictHeight = container.find("img").height();
var pictEls = container.find("li").size();
var stripViewerWidth = pictWidth*pictEls;
container.find("ul").css("width" , stripViewerWidth); //assegnamo la larghezza alla lista UL
container.css("width" , pictWidth);
container.css("height" , pictHeight);
container.each(function(i) {
jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
jQuery(this).find("li").each(function(n开发者_JAVA技巧) {
jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");
});
jQuery("div#stripTransmitter" + j + " a").each(function(z) {
jQuery(this).bind("click", function(){
jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current"); // wow!
var cnt = -(pictWidth*z);
container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
return false;
});
});
// next image via image click 14/01/2009
jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
jQuery(this).bind("click", function(){
var ui = jQuery(this).parent().parent().parent().next().find("a");
if(z+1 < pictEls){
ui.eq(z+1).trigger("click");
}
else ui.eq(0).trigger("click");
});
});
jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
if(settings.toolTip){
container.next(".stripTransmitter ul").find("a").Tooltip({
track: true,
delay: 0,
showURL: false,
showBody: false
});
}
});
j++;
});
};
The first image has no caption because the caption displays only after onclick
.
This can be fixed by inserting:
var altText = $(this).find("img").eq(0).attr("alt");
$("#caption").html(altText).fadeIn("slow");
It's as simple as:
$("div").html( $("#myImg").attr("alt") );
When do you want this to happen?
The image is activated (or brought into view) on screen by this line
container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc);
you need to modify this line to:
container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc, function(){
// z is the index of the currently showing picture,
// it comes from the .each call above
var altText = $(this).find("img").eq(z).attr("alt"); //get alt value
// now insert the text from the alt value
$("#id_of_your_text_display_div").html(altText);
});
Hope this helps....
Thanks for all the help guys!
I tweaked ekhaled's version of the script so that the caption was displayed underneath the first image in the series, as well as a simple fade effect for captions for any subsequent images - it's all working brilliantly now!
Thanks again :)
jQuery(function(){
jQuery("div.svw").prepend("<img src='/template/theme/designdistillery/img/bg-portfolio-loading.gif' class='ldrgif' alt='loading...'/ >");
});
var j = 0;
var quantofamo = 0;
jQuery.fn.slideView = function(settings) {
settings = jQuery.extend({
easeFunc: "easeInOutExpo",
easeTime: 750,
toolTip: false
}, settings);
return this.each(function(){
var container = jQuery(this);
container.find("img.ldrgif").remove(); // Removes the preloader gif
container.removeClass("svw").addClass("stripViewer");
var pictWidth = container.find("img").width();
var pictHeight = container.find("img").height();
var pictEls = container.find("li").size();
var stripViewerWidth = pictWidth*pictEls;
container.find("ul").css("width" , stripViewerWidth);
container.css("width" , pictWidth);
container.css("height" , pictHeight);
container.each(function(i) {
var altTextstart = $(this).find("img").attr("alt");
$("#caption").html(altTextstart);
jQuery(this).after("<div class='stripTransmitter' id='stripTransmitter" + (j) + "'><ul><\/ul><\/div>");
jQuery(this).find("li").each(function(n) {
jQuery("div#stripTransmitter" + j + " ul").append("<li><a title='" + jQuery(this).find("img").attr("alt") + "' href='#'>"+(n+1)+"<\/a><\/li>");
});
jQuery("div#stripTransmitter" + j + " a").each(function(z) {
jQuery(this).bind("click", function(){
$("#caption").html(altTextstart).hide();
jQuery(this).addClass("current").parent().parent().find("a").not(jQuery(this)).removeClass("current");
var cnt = -(pictWidth*z);
container.find("ul").animate({ left: cnt}, settings.easeTime, settings.easeFunc,function(){
// z is the index of the currently showing picture,
// It comes from the .each call above
var altText = $(this).find("img").eq(z).attr("alt"); // Get alt value
// Now insert the text from the alt value
$("#caption").html(altText).fadeIn("slow");
});
return false;
});
});
jQuery("div#stripTransmitter" + j + " a").parent().parent().parent().prev().find("img").each(function(z) {
jQuery(this).bind("click", function(){
var ui = jQuery(this).parent().parent().parent().next().find("a");
if(z+1 < pictEls){
ui.eq(z+1).trigger("click");
}
else ui.eq(0).trigger("click");
});
});
jQuery("div#stripTransmitter" + j).css("width" , pictWidth);
jQuery("div#stripTransmitter" + j + " a:first").addClass("current");
if(settings.toolTip){
container.next(".stripTransmitter ul").find("a").Tooltip({
track: true,
delay: 0,
showURL: false,
showBody: false,
});
}
});
j++;
});
};
精彩评论