2016-03-07 30 views
0

我試圖改變我的h1的填充,通過檢測我的頁面高度,我會改變它,如果我的屏幕高度 700:800像素,但我的代碼不工作:(jquery如果statment檢測到的高度

if ($('.background-wrapper').height() <= 800 && $('.background-wrapper').height() >= 700) { 
     $(".login > background-wrapper > h1").css("padding-top", "50px") 
    } 

HTML:

<div class="background-wrapper"> 
    <div class="container"> 
     <div class="row"> 
      <h1> CRM INTERACTIVE MAP SYSTEM</h1> 
     </div> 
    </div> 
</div> 
+2

你不使用CSS媒體查詢此的原因嗎? – CBroe

回答

2

請與CSS媒體查詢

@media only screen and (min-height: 700px) and (max-heigth: 800px) { 
    .login > background-wrapper > h1 { 
     padding-top: 50px; 
    } 
} 
0

媒體查詢時你r最好的選擇。請參閱其他答案。

如果有你不向我們展示的東西,可能會要求你這樣做在javascript:

.background-wrapper是不會準確地給你的屏幕尺寸,它只會給你本身的高度(在某些情況下也可能是屏幕的高度)。

你想要使用的是$(window).height()window.innerHeight

也@NiZa指出你的選擇是不正確的。

0

如果你不想這樣做media queries(參見@MarcosPérezGude答案),你應該改變一些東西。首先,您需要更改selector。刪除>,因爲中間有一個div。其次,您可以嘗試將其放入$(document).ready()函數中。

最後,請看傑克的答案。使用$(window).height()比div本身更好。

$(document).ready(function() { 
    if ($(windo).height() <= 800 && $(window).height() >= 700) { 
    $(".login > background-wrapper h1").css("padding-top", "50px") 
    } 
});