简体   繁体   English

完全加载后淡入Ajax内容

[英]Fade in Ajax content after it's fully loaded

My current solution gets some content via AJAX, Inserts it into a DIV then hides it and fade it in. 我当前的解决方案通过AJAX获取一些内容,将其插入DIV中,然后将其隐藏并淡入。

The issue with that is the content contains images and an iframe, The solutions flickers and the fadeIn doesn't look smooth. 问题在于内容包含图像和iframe,解决方案闪烁,并且fadeIn看起来不流畅。

What i'd like to do is to only fadeIn after the iframe and pictures are fully loaded. 我想做的只是在iframe和图片完全加载后fadeIn

How to do that? 怎么做?

This will wait until all child content has finished loading: 这将等待所有子内容完成加载:

$("#wrapperDivID").load(function (){
    // do something, e.g.
    $(this).fadeIn();
});

See .load() . 参见.load()

In your specific case the following should work: 在您的特定情况下,以下方法应该起作用:

$('#content').load('/echo/html/',data, function() {
  alert('fired');
}); 

I would change the duration for fadeIn so that it will take little long. 我会更改fadeIn的持续时间,以便花一点时间。

 $("#content").load("getform.php"), function() {
                $(this).fadeIn(1500);  // or another bigger number based on your load time
 });

If you know what all images are going to be in the page, Preload all those images in the page where you have the div "content", so that you will save some loading time. 如果您知道页面中将包含所有图像,请将所有这些图像预加载到具有div“内容”的页面中,这样可以节省一些加载时间。

another way you can do this: 另一种方法是:

$( "#content" ).fadeOut(200, function() {
    $(this).load( url, function() {
        $(this).fadeIn(200);
    });
});

change the fadeIn and Out times to whatever is good for you. 将淡入和淡出时间更改为对您有利的时间。

If you use $.ajax to get the HTML you can wrap the resulting HTML like below: 如果使用$.ajax来获取HTML,则可以包装生成的HTML,如下所示:

$.ajax('some/path/to.html',{
    'success':function(html){
        var result = $(html).load(function(){
            $('#content').fadeIn()
        });
        $('#content').html(result);
    }
});

jsfiddle example jsfiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM