开发者

Prevent div from collapsing while using jQuery.load

开发者 https://www.devze.com 2023-01-07 04:12 出处:网络
开发者_如何学编程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 so
开发者_如何学编程

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!


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);


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;
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消