This is somewhat related to jquery but is primarily a .js issue.
I am converting an existing PHP photo album. I have the image load working but can't see how to get the respective caption key from the js to pull from $caption_array.
This is the code I am using - where the div class=change is actually a string of thumbnails each with the ID set as the filename. Click one and the div class=bg-wrapper is loaded with the main image.
scratches head.....
the issue so often is defining the question - bare with me.
$data_file has all the captions - not necessarily sequential.
img_cat.jpg|cat caption here
pic_dog.jpg|about the dog
etc.
So, I will also need to split this array and loop to find the line that matches against
$(this).attr("id").replace("tn_","")
Worked a bit last night but couldn't tease out the logic of your code.
This is what I have so far - perhaps a bit 'old school' linear but it works. The alert gives the correct caption but I am unsure how to feed it into a div.
The image div is #bg-wrapper but, again, I can't replicate the logic to feed my caption into another div - say: #img_caption
Code:
<script>
$(function() {
$('<img>').attr({'src':'<?php echo $image; ?>','id':'bg','title':''}).appendTo('#bg-wrapper').parent().fadeIn(1000); // loads first image on page load
var captions = [<?php echo $cap_array; ?>];
var len = captions.length;
$('.change').click(function(e) {
e.preventDefault();
var img_now = $(this).attr("id").replace("tn_","");
for (var i=0; i<len; ++i) { var file = captions[i].split("|");
if (file[0]==img_now) { alert(file[1]); }}
var image = '<?php echo $album; ?>/'+$(this).attr("id").replace("tn_","");
$('#bg').parent().fadeOut(800, function() {
$('#bg').attr('src', ''+image);
$(this).fadeIn(800); }); }); });
</script>
Yes, I figured pic was a bit of mind flotsam.
Cheers - ak
Still poking about.
I have a FF solution but no IE
Code:
<script>
$(function() {
$('<img>').attr({'src':'<?php echo $image; ?>','id':'bg','title':''}).appendTo('#bg-wrapper').parent().fadeIn(1000);
var captions = [<?php echo $cap_array; ?>];
var len = captions.length;
$('.change').click(function(e) {
e.preventDefault();
var img_now = $(this).attr("id").replace("tn_","");
for (var i=0; i<len; ++i) { var file = captions[i].split("|");
if (file[0]==img_now) { var this_cap = file[1]; }} // works in FF but not IE
//var mytext=document.createTextNode(this_cap);
//document.getElementById("img_caption").appendChild(mytext); // Adds - but not replaces
img_caption.innerHTML = this_cap; // seems OK in FF & IE - but no caption from loop. :(
var image = '<?php echo $album; ?>/'+$(this).attr("id").replace("tn_","");
$('#bg').parent().fadeOut(800, function() {
$('#bg').attr('src', ''+image);
$(this).fadeIn(800); }); }); });
</script>
I suspect this may look a bit clumsy but I am a work in progress...
PS - an alert test on var len has FF=18 IE=17 - which is weird.
js is such a muddle for me.
Part of my issue was in the caption array itself - fixed.
My issue now is testing that the caption is not empty - null string - undefined.
if (file[0]==img_now && ?? ) { var this_cap = file[1]; }
tried with ?? as:
file[1].length>1
file[1]!==''
file[1]!==NULL
file[1]!=='undefined'
Fortunately the existing script is working fine. This is just my aiming to add dynamic content - not something I have much experience with. Getting into jquery and such.
I'm sorry... it makes me sleepy just looking at your code.
You should really get at least a basic grip on javascript before branching off into jQuery. And if you need this done now, there are a million ready-written slideshow scripts out there, in javascript and jQuery...
>> sleepy...
Mostly I hack existing js.
The main code of mine above is the i loop and innerhtml. The rest is borrowed.
My PHP is a tad more elegant.
>> a basic grip on javascript
I don't use it a lot.
jquery I try to use out-of-the-box
>> a million ready-written slideshow scripts
Indeed. I am hoping to avoid the heavy image load that most require. Up to 100 pics at around 50kb or more takes a while. I figure most people only look at 5 to 10 images per album.
I have this working well enough for now.
Thanks again for your patience.