2013-04-05 21 views
-2

我有一個網頁,並且當用戶在任何點滾動時,我想要一個div標籤將其類從「頂部」更改爲「頂部滾動」。用戶在網頁上滾動後將類添加到<body>

我認爲你必須使用.scrollTop()來做到這一點,但不太清楚如何使用它。下面是HTML代碼

<div class="thetop"> 
     <?php /** Begin Header **/ if ($gantry->countModules('header')) : ?> 
     <div id="rt-header"> 
      <div class="rt-container"> 
      <?php echo $gantry->displayModules('header','standard','standard'); ?> 
      <div class="clear"></div> 
      </div> 
     </div> 
     <?php /** End Header **/ endif; ?> 
    </div> 

我也想包括body標籤「滾動」用戶已經滾動到應用單獨的CSS樣式命名的類。

+1

爲什麼我downvoted這個問題:http://meta.stackexchange.com/a/149138/133242 – 2013-04-05 04:12:39

回答

1

是的,您可以使用ScrollTop函數。

你可以用它在身上像這樣:

$('body').scrollTop(); 

它將返回滾動的像素數。

比方說,你想添加一類經過500個像素滾動:

$('body').scroll(function(){ 
    // This function will be called at each scroll event 
    if($('body').scrollTop()>=500){ 
     $('.thetop').removeClass('thetop').addClass('thetopscrolled'); 
     $('body').unbind(); // You don't want to add the class at each scroll event, just once. 
    } 
});