2017-07-30 92 views
1

我想添加兩個.height()的一起,以獲得我的onScroll事件響應號碼如何將兩個這些添加到一起?將兩個.height()一起添加? jquery

我是一般人,我只是試圖讓底層的元素上方的底部觸發動畫。除了增加以上所有內容的高度之外,還有其他方法可以做到嗎?

window.addEventListener('load', function() { 
    $(window).scroll(function() { 
     var eventScroll = window.pageYOffset; 
     var eventScrollAmount = ($("#sectionOneImage").height() + $("#bannerOne").height()); 

     if(eventScroll > eventScrollAmount) { 
      $("#sectionOneImage").addClass("animated slideInLeft "); 
     } 
     else { 
      $("#sectionOneImage").removeClass("animated slideInLeft "); 
     } 
    }); 
}); 
+0

聽起來你正在尋找['$(...).offset().top'(http://api.jquery.com/offset/) –

+0

哦,其實我認爲是。謝謝! – Vezro

回答

2

下面是如何使用香草JS抓住兩個高度的例子。

var sizeOfOne = document.getElementById('one').offsetHeight; 
 
var sizeOfTwo = document.getElementById('two').offsetHeight; 
 

 
document.getElementById('hook').innerHTML = 'The first div is ' + sizeOfOne + ' pixels in height, the second div is ' + sizeOfTwo + ' pixels in height. The total size of both div\'s are ' + (sizeOfOne + sizeOfTwo) + ' pixels.';
#one { 
 
    height: 50px; 
 
    background: red; 
 
} 
 

 
#two { 
 
    height: 75px; 
 
    background: blue; 
 
}
<div id="hook"></div> 
 

 
<div id="one"></div> 
 
<div id="two"></div>