简体   繁体   English

显示加载图像,直到内容完全加载

[英]show loading image until the content is fully loaded

I'm using image slide show in my site, but when i loading my site it take more time and initially page not look better one by one image. 我在我的网站上使用图片幻灯片放映,但是当我加载我的网站时,它需要更多的时间,并且最初页面看起来不是一个一个更好的图像。 So, i want to load first loading image on the image slide and when div content is fully loaded then hide the loading image and display image slide show. 因此,我想在图像幻灯片上加载第一个加载图像,当div内容完全加载时,隐藏加载图像并显示图像幻灯片放映。 so, hows this possible please help me. 所以,这有可能请帮助我。 i'm not loading content from ajax. 我没有从ajax加载内容。 it's simple loading. 这是简单的装载。

$('#loading-image').show(); // show loading image before request
$('#content').load('my-content-source.php', function() { // load content
   $('#loading-image').hide(); // hide image after content was loaded
});

where loading-image is id of your loading indicatior, and content is id of your content container. 其中loading-image是加载指示符的id, content是内容容器的id。

It depends what you want to display, a simple loading background image could be done like this: 这取决于你想要显示什么,一个简单的加载背景图像可以这样做:

before starting with loading your content you could set a class on your Div element. 在开始加载内容之前,您可以在Div元素上设置一个类。

$('#myDiv').addClass('loading');

in CSS you could have 在CSS中你可以拥有

#myDiv { width: 100px; height; 100px; }
#myDiv.loading { background: url(loading.gif) no-repeat center center; }

after loading you can remove the class 加载后,您可以删除该类

$('#myDiv').load("http://www.mypage.com/contentToLoad.php", function(){
    $('#myDiv').removeClass('loading');
});

if you want to display more markup (html, a div with text and an image ...) you will need to do the same but instead of using addClass() and removeClass , you would need to Show() and Hide() the other element. 如果你想显示更多的标记(html,带有文本和图像的div ......)你需要做同样的事情,而不是使用addClass()removeClass ,你需要Show()Hide()其他因素。

您可以使用图像的onload事件,然后隐藏加载动画。

<img onload="hideLoadingDiv();">

This kind of depends on how you exactly are doing everything right now.. 这种情况取决于你现在如何做好一切......

But this is about as simple as it gets: 但这很简单:

http://jsfiddle.net/JTCpB/1/ Edit: Updated that loading image to something that.. makes sense. http://jsfiddle.net/JTCpB/1/ 编辑:更新了将图像加载到有意义的东西。

<div id="load">
    <img src="http://lorempixum.com/g/400/200/" alt="" />
</div>

#load { 
    width: 400px;
    height: 200px;
    background: url("loading.gif") no-repeat center center; }

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

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