views:

45

answers:

2

Hi, I load external content to replace a DIV.

Problem is, when the ajax-loader.gif replaces the initial content the page shrinks in height and the scrollbar is likely to disappear. As soon as the external content is loaded the scrollbar reappears. That jerking takes away the smoothness.

Is there a smoother way? Maybe preserve the height of the div until the external content is loaded? I cannot use fixed heights. Here's my function:

function(){
    $('.filter a').click(function(){
    $('#mydiv').html('<p><img src="ajax-loader.gif" /></p>');
    $('#mydiv').load('/site/?key=Value');
  return false;
});

(Project is a faceted search in Wordpress).

Thank you!

+5  A: 

You can load the content using $.ajax() and set the html of the div to the content it returns...

$.ajax({
  url: '/site/?key=Value',
  success: function(data) {
    $('#mydiv').html(data);
  }
});

Edit: I assume your shifting is due to the loading image being smaller from your other comment... this should fix that:

var storedHeight = $('#mydiv').height();
$('#mydiv').html('<p><img src="ajax-loader.gif" /></p>').find('p').height(storedHeight);
Andir
his div is still going to shrink when he puts the loading icon in. using $.ajax to load the new content isn't going to help.
Patricia
I am setting the height of the <p> to the height of the element. When it adds that content the height should be the same. When the content is loaded it will automatically remove the <p> and it's respective height. There's no need to deal with the #myDiv height.
Andir
ahh yea, i see you added that edit. that hadn't been refreshed when i made my comment. that would, in theory, work.
Patricia
That looks good to me. I have to stop working now, but will try it tomorrow!
Christoph
Works out perfectly! Thank you so much!
Christoph
+1  A: 

have you tried setting the min-height?

something like this might work:

$(function(){
    $('.filter a').click(function(){
       var height = $('#mydiv').height();
        $('#mydiv').attr('style', 'min-height:' + height);
        $('#mydiv').html('<p><img src="ajax-loader.gif" /></p>');
        //changed            
        $('#mydiv').load('/site/?key=Value', function(){
            $('#mydiv').removeAttr('style');
            $('#mydiv').attr('style', 'height:auto;');            
        });
        //end of changed
        return false;
    });
});
Patricia
Hmmm, that's it... almost. If the new content is shorter than the old one the page is too long. I have to clear the min-height somehow.(and the height has to be in pixel: .attr('style', 'min-height:' + height + 'px');)
Christoph
@Christoph: Have you tried using the ajax method I suggested yet? It will adjust the size of the element the content is going into to the size it needs and not do any funky shifting.
Andir
try setting the height to auto after it's loaded. i've updated the answer.
Patricia
Hey, thank you, that works fine! I will try Andirs ajax method though, because it seems nifty to me.
Christoph