在页面加载之前,使用jQuery将容器居中

Centering a container with jQuery before page load

本文关键字:jQuery 使用 加载      更新时间:2024-04-23

我有一个divimg,它们需要以窗口为中心。我已经扩展了jQuery的功能,以便使用以下功能:

jQuery.fn.center = function () {
                this.css("position","absolute");
                this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) +$(window).scrollTop()) + "px");
                this.css("left", Math.max(0, (($(window).width() - this.outerWidth()) / 2) +  $(window).scrollLeft()) + "px");
                return this;
            }

这个功能非常有效,但唯一的问题是图像首先加载到默认位置,然后移动到中心。如何在页面加载时使图像直接显示在中心?

编辑:我的意思是:www.foxteam.net/portfolio.php

我实际上需要查看调用此函数的代码来帮助您,但这里有一个快速建议。

首先用display:none;渲染img,然后在.center()之后链接.fadeIn()函数或者使用回调使其同步。

这样,即使在显示图像之前,您也可以将图像居中。