如何用jquery制作页面的预加载效果?

发布于 2024-10-19 20:41:50 字数 1091 浏览 1 评论 0原文

我想到的第一件事是创建一个容器,并在页面加载开始时让它充满屏幕,而元素仍在预加载掩码后面加载,例如

    var window_width = $(window).width();
    var window_height = $(window).height();

    $('.pre-modal-window').each(function () {

        var modal_height = parseInt($('.pre-modal-window').css("height"));
        var modal_width = parseInt($('.pre-modal-window').css("width"));

        var top = (window_height - modal_height) / 2;
        var left = (window_width - modal_width) / 2;

        $(this).css({ 'top': top, 'left': left });

    })

        //set display to block and opacity to 0 so we can use fadeTo
        $('#pre-mask').css({ 'display': 'block', opacity: 0 }).fadeTo(50, 1.0);

        //show the modal window
        $('.pre-modal-window').fadeIn(500);

使用 SetInterval 设置它在几秒钟后消失( )函数(我知道它是假的,但我不知道如何判断元素已经下载了……)。

它就像 jquery 插件 queryloader 一样。但是,它在我的项目中效果不佳,所以我想自己写一个

上面描述的想法只有当页面中只有很少的元素时才有效,当元素变得越来越多时,效果就不再起作用了……我怎样才能让它在页面开始下载时显示?顺便说一句,我怎样才能判断所有元素都已经下载了?谢谢

The first thing I think about is make a container, and let it full of the screen at the begining of the page loadding,While the element still are loading behind the pre-load mask ,like

    var window_width = $(window).width();
    var window_height = $(window).height();

    $('.pre-modal-window').each(function () {

        var modal_height = parseInt($('.pre-modal-window').css("height"));
        var modal_width = parseInt($('.pre-modal-window').css("width"));

        var top = (window_height - modal_height) / 2;
        var left = (window_width - modal_width) / 2;

        $(this).css({ 'top': top, 'left': left });

    })

        //set display to block and opacity to 0 so we can use fadeTo
        $('#pre-mask').css({ 'display': 'block', opacity: 0 }).fadeTo(50, 1.0);

        //show the modal window
        $('.pre-modal-window').fadeIn(500);

Setting it disappeared after few second by using SetInterval() function(well I konw it is fake,but I don't konw how to judge the element have downloaded already……) .

It just like the jquery plugin queryloader.However, it does't work well in my project.So I want write one by myself

The thought described above only work when page just have few elements in my page,when the elements become more and more,the effect does't work again ……How can I let it show when the page begin download?BTW,how can I judge all the elements have downloaded already?Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-10-26 20:41:50

查看文档的 jQuery .ready() 函数: http://api.jquery.com/ready/

从 jQuery API:指定
当 DOM 存在时执行的函数
满载。

示例如下:

<script type="text/javascript">
$(document).ready(function() {
  // hide the loading mask here.
});
</script>

Have a look at jQuery's .ready() function for the document: http://api.jquery.com/ready/

From the jQuery API: Specify a
function to execute when the DOM is
fully loaded.

An example would be as follows:

<script type="text/javascript">
$(document).ready(function() {
  // hide the loading mask here.
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文