2013-08-02 82 views
3

使用jquery,如何設置一個事件,當垂直滾動可見時,以及從頁面的上半部分切換到下半部分時,觸發事件,反之亦然。因此,例如,如果垂直滾動條在那裏,然後我正在頁面上半部分的頁面上查找某處,然後向下移動,因此我處於頁面的下半部分,該功能發生。然後如果我移回來,所以我又在上半場,這個功能就會發生。如何使用jquery檢測垂直滾動位置?

謝謝。

回答

1

以及jQuery不會有這些方法,您可以在元素上使用:

.scrollTop() - 獲得在視口中的元素在上面滾動位置

.scrollLeft() - 獲得在視口中

一個元素左滾動位置

也有是在滾動事件 - 觸發另一個(或窗口)內,當視口/元件滾動:

.scroll()

4
var midHeight = jQuery(window).height()/2 //Splits screen in half 
$(window).scroll(function() { 
    if ($(window).scrollTop() > midHeight) { 
     //Do something on bottom 
    } else { 
     //Do something on top 
    } 
}) 
0
$(window).scroll(function() { 
    if ($(window).scrollTop() > $('body').height()/2) { 
     //code goes here... 
    } 
}); 

檢測滾動

var s = $('body').scrollTop(); 

jQuery的scrollTop()

說明:獲取滾動條的當前垂直位置爲 第一個元素集合中匹配的元素或爲每個匹配元素設置滾動條的垂直位置 。

你可能有興趣知道:

$().scrollTop()//how much has been scrolled 
$().innerHeight()// inner height of the element 
DOMElement.scrollHeight//height of the content of the element 

JSFIDDLE DEMO