1

大家好,我遇到了一个 jQuery 问题。有时它会起作用,并给我正确的高度和其他人完全不同的高度。也许它与正在加载的订单有关,或者我只是做错了什么。如果您发现那里有错误或者您会做出什么不同,请您看一下并告诉我吗?我会很感激。它在 Wordpress 站点中,并且在标题中....

 <script type="text/javascript">

jQuery(document).ready(function( $ ) {

// boxen gleiche hoehe

    if($(window).width() >= 1600)  {
        $(".home #fb").height($("#homebox").innerHeight(true) + $("#homebox2").innerHeight(true) - 45) +'px';
        }


    $(".home #twitter, .page-template-homeLayout-php #twitter").height($("#homebox").innerHeight(true) + $("#homebox2").innerHeight(true));
 });

 $(window).resize(function() {

        if($(window).width() >= 1600)  {
        $(".page-id-1235 #fb").height($("#homebox").innerHeight(true) + $("#homebox2").innerHeight(true) - 45) +'px';
        }

        $(".home #twitter, .page-template-homeLayout-php #twitter").height($("#homebox").innerHeight(true) + $("#homebox2").innerHeight(true));


});


});

</script>
4

2 回答 2

0

在我看来,这个问题是你的 if 语句中的错误代码的结果,这可以解释这个问题看似随机发生的情况。

这是不准确的:

if($(window).width() >= 1600)  {
 $(".home #fb").height( $("#homebox").innerHeight(true) + $("#homebox2").innerHeight(true) - 45) +'px';
}

应该是这样的:

if($(window).width() >= 1600)  {
 $("#fb").height( 
  $("#homebox").innerHeight(true) + 
  $("#homebox2").innerHeight(true) - 45
 );
}

请注意,每页只允许一个 id,因此选择器只使用 id 就足够了。而且,height()采用 int 值,不需要'px'像 css 属性。

同样适用

if($(window).width() >= 1600)  {
 $(".page-id-1235 #fb").height($("#homebox").innerHeight(true) + $("#homebox2").innerHeight(true) - 45) +'px';
}

应该是

if($(window).width() >= 1600)  {
 $("#fb").height(
  $("#homebox").innerHeight(true) + 
  $("#homebox2").innerHeight(true) - 45);
}
于 2012-11-15T09:07:07.490 回答
0

这是因为您使用 $(window) 作为宽度的基础。而是将您的网站放入容器中

HTML

<div class="container">
 <div>your content</div>
</div>

CSS

.container {
 width: 100%;
 margin: 0 auto;
}

然后在你的 JS 中切换 $(window) 与 $(.container) 加上来自 TravisJ 的编辑

于 2012-11-15T09:08:15.507 回答