views:

410

answers:

3

Please please someone put me out of my misery....i have poured hour upon hour into this...

I have (this is abbreviated) been creating a function which adds boxes to a page using append. The problem is once they have been added the fadeIn function doesnt work. It will work however if i hard code the element to the page:

Here is my javascript:

       //Loop through the images and print them to the page
   for (var i=0; i < totalBoxes; i++){
    //$("p").add(arr).appendTo('#bg');
    $.ajax({
     url: "random.php?no=",
     cache: false,
     success: function(html){
    $(html).fadeIn(html).appendTo('#bg');
     }
    });
   }

 //Choose the image to be faded in
  $(".pf-box img").hover(function(){
  var largePath = $(this).attr("rel");
   $(this).fadeOut("slow", function() {
     $(this).attr({ src: largePath }).fadeIn("slow");
   });
   return false;
  });

the random.php literally prints however many boxes...here is a printed sample:

<div class="pf-box" style="">
<a href="#">
This is where the image is printed with the rel attribute on the image tag. (stackoverflow didnt allow the posting of the img tag because its my first post)
</a>
</div>
+3  A: 

It looks like the parameters to your fadeIn function are incorrect. I think you will also want to append the html to your document before you can fade it in.

Try this, in your success function?

function(html) {
    $('#bg').append(html).fadeIn('slow');
}

You may also find the doc page for fadeIn to be helpful.

Good luck!


EDIT/UPDATE

OK, I think I know what you're trying to do now. After you have fixed the statement I described above (appending and then fading in), what you need to do is assign your hover event after the ajax retrieval/appending has had a chance to complete.

What is happening is that your first block of code is firing off a bunch of asynchronous requests to the webserver for the images you want. Then, instantly after your first block has "completed", but (importantly!) before these requests have had a chance to complete, your second block of code is (trying to) execute. It is looking for the selector ".pf-box img" to try and add the hover event, but the problem is, these images do not yet exist on the page!

What you need to do is wait until the images have come back from the server before you attempt to select them or add events to them. This is done via callbacks, which you already have partially working in your success function...

(Please note, I haven't tested this, it is meant to demonstrate only...)

// Loop through the images and print them to the page.
// Attach hover event within the callback, after image has been added.
for (var i=0; i < totalBoxes; i++){
    $.ajax({
        url: "random.php?no=",
        cache: false,
        success: function(html) {
            // following line I originally suggested, but let's make it better...
            //$('#bg').append(html).fadeIn('slow');
            // also note the fine difference between append and appendTo.
            var $d = $(html).hide().appendTo('#bg').fadeIn('slow');
            $('img', $d).hover(function() {
                var largePath = $(this).attr("rel");
                $(this).fadeOut("slow", function() {
                    $(this).attr({ src: largePath }).fadeIn("slow");
                });
            });
        }
    });
}
Funka
Unfortunately this didnt work. It simply created a loop of fading...the loading to the page is working as it should i think...the problem is that the second part of the function where it picks out the attribute wont work if its appended...only if i hard code it in...thanks for your help though
Andy
Hi, ... could you let us know what it is you want to do? I read your question as the fadeIn not working after using append. However perhaps you are asking instead about the hover functionality? I've looked at what you posted and cannot really see what it is you want to do (the end-result).
Funka
I think you will find that the `hover` function takes two params, one for the "over" event and another for the "out" event. See http://docs.jquery.com/Events/hover
Funka
Based on some of the other comments you've left in this question, I've added an update which will hopefully address the real issue you're encountering?
Funka
Also, I didn't see in your original question where the `i` variable is passed to your random.php script? If you can tell the random.php script how many divs you want, i think you could also just do away with the `for` loop altogether...
Funka
A: 

You are misunderstanding the fadeIn method.

fadeIn operates on an existing element(s), and animates the opacity of the element, just like fadeOut. You want to append the HTML to your #bg element and then fade it in.

For example:

                      success: function(html){
                            $(html).appendTo('#bg').fadeIn("slow");
                      }
SLaks
If i add this function it stops the fade working correctly...I am going to try and explain myself better...it takes the window viewport height and width. IT then figures out how many are needed to fill the screen. It then loops through the total amount selecting it from random.php and fades it into position. This all works correctly. Once faded in using the append it the images have a rel attribute which is pulled in via jquery to be faded in over the existing image in its respective box. However this only works if its hard coded. Not if its appended. Thanks for everyones help.
Andy
A: 

Funka...you are on the ball mate and thats exactly the issue...thank you so much!

Could i ask one more question about this code?

Currently it fades out the image before fading in...im trying to get the effect of the colour image fading through the black and white one...and then when you remove the hover status it should fade back to black and white. I am using the two images to do this as you know.

How could i rewrite the fadeout part to simply fade the other in over it and then on mouseout go back to its original state...?

You have been a tremendous help and its greatly appreciated.

Andy
Hi Andy, I'm glad I could be of help! However my suggestion would probably be for you to (A) pick an answer to and close this particular question; then (B) reformat and refine your problem into a new question, with a nice descriptive title. This way, your problem is more likely to get more people casting fresh eyes upon it, rather than being buried further in this thread. Good luck!
Funka