Jquery.height()使用F5或CTRL + F5返回不同的结果 [英] Jquery.height() returns different results using F5 or CTRL+F5

查看:103
本文介绍了Jquery.height()使用F5或CTRL + F5返回不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图找到我的图像的高度,然后添加一个上边距,这使我能够施加一个垂直中心。

So I am trying to find the height of my images then add a top margin this enables me to impose a a vertical center.

我正在运行此代码,并且在 F5刷新我得到正确的高度,但在 CTRL + F5刷新它给我一个更小的高度。我有点假设这是一个加载/延迟的东西,但我正在使用文档准备好所以不确定最新情况。我尝试使用php函数,但它让网站速度惊人,所以必须坚持使用jquery。

I'm running this code, and on an F5 refresh I get correct height but on CTRL+F5 refresh it gives me a much smaller height. I kind of assume this is a loading/delay thing, but I am using document ready so not really sure whats going on. I tried using a php function but it slows the site down amazingly so have to stick with jquery.

你可以看到它在这里工作。 www.mzillustration.com

you can see it working here. www.mzillustration.com

 jQuery(document).ready(function() {

 if (jQuery('.imagedisplay').length != 0) {
                jQuery('.imagedisplay').each(function(){ 

                var imgheight = jQuery(this).find('img').height();

                var topmarg = ((240 - imgheight) / 2) ; 

                jQuery(this).find('img').css({'margin-top':topmarg+'px'});



            });

});

非常感谢任何想法/帮助/解释。
谢谢

any ideas/help/explanation much appreciated. thanks

推荐答案

onload和onready之间存在差异。

There is a difference between onload and onready.

ready将等到实际的DOM树完成,而onload将等待,直到页面上显示的所有内容都完成加载。所以解释一下,当清除缓存并刷新时,dom树的完成速度比图像快得多,因此给出了错误的高度。

ready will wait until the actual DOM-tree is done, while onload will wait until ALL of the content displayed on the page is finnished loading. So an explanation would be that when clearing the cache and refreshing, the dom tree finishes much faster than the images, hence giving the wrong heigh.

尝试使用onload-event相反,看看你是否得到了不同的结果。

Try using the onload-event instead and see if you get a different result.

这篇关于Jquery.height()使用F5或CTRL + F5返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆