【问题标题】:$(window).height() returning wrong value$(window).height() 返回错误值
【发布时间】:2014-10-31 04:09:33
【问题描述】:

www.presenskonferens.se 上运行$(window).height() 时,我无法获得正确的值。在所有浏览器中都是一样的。

这是我正在使用的代码:

// Screen height sections
$(function() {

    function screen_height() {
        $('.screen-height').css({'height': $(window).height() });
    }

    screen_height();
    window.addEventListener("orientationchange", screen_height );
    $(window).resize( screen_height );

});

这是我尝试过的:

  • 使用正确的文档类型声明 (<!DOCTYPE html>)
  • 删除相关 div 中的所有内容
  • 移除js文件中除screen_height函数外的所有内容
  • 删除 screen_height 函数中除$('.screen-height').css({'height': $(window).height() }); 之外的所有内容
  • $(window).height() 生成console.log 并得到12191 作为回报

我被难住了。据我所知,当网站更新到 WordPress 4.0 时发生了错误,但它没有使用 WordPress 中捆绑的 jQuery 版本。感谢您提供任何和所有帮助。

【问题讨论】:

  • 为什么不使用window.outerHeightwindow.innerHeight
  • 此外,您应该将 googleanalytics-script 移动到 HTML 中 - 目前您的页面以 doctype-declaration 之前的脚本开始,并且在不关闭 body 和 html 的情况下结束(=

标签: jquery wordpress window height


【解决方案1】:

当文档类型设置不正确时,jQuery 将$(window).height() 视为$(document).height()

您使用了正确的文档类型 (<!DOCTYPE html>),但通过在其前面添加分析代码,您已使其无效。

将您的分析代码移到头部,就在结束 </head> 标记之前,它应该可以正常工作。

编辑:我刚刚注意到您有两个不同的分析代码块。去掉最顶部的那个(在第 1 行)。

【讨论】:

  • 太棒了,谢谢。分析代码是通过我不知道客户端已安装的插件添加的。停用它解决了这个问题。
猜你喜欢
  • 2013-10-08
  • 2012-05-22
  • 2013-04-22
  • 1970-01-01
  • 2023-03-09
  • 2012-05-21
  • 2012-08-19
  • 2013-03-19
  • 1970-01-01
相关资源
最近更新 更多