2012-07-05 17 views
1

我發現這個代碼無法實現的JavaScript代碼

$(window).scroll(function(){ 
    $('#header').css({ 
     //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to remain at 15px left 
     'left': $(this).scrollLeft() + 15 
    }); 
}); 

而且它正是我需要的,但我不知道如何實現它在我的HTML或CSS。誰能幫我嗎?我真的很感激它。

+0

你可以發佈你的HTML嗎? – honyovk 2012-07-05 15:36:44

+2

如果它滿足你的需求,你卡在哪裏? – 2012-07-05 15:36:50

+0

看看包含Javascript的_any_頁面,看看它是如何完成的? – TheZ 2012-07-05 15:36:52

回答

3

該代碼使用jQuery,因此您需要在頁面中包含jQuery。

因爲它是JavaScript,所以您需要使用<script />標籤。你可以添加內聯代碼(我會這樣做,因爲它更容易解釋),但是你應該考慮將它添加到單獨的JS文件中幷包含它。

將此項添加到您的HTML任意位置;

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <!-- Include jQuery --> 
<script type="text/javascript"> 
    $(window).scroll(function(){ 
     $('#header').css({ 
      //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to remain at 15px left 
      'left': $(this).scrollLeft() + 15 
     }); 
    }); 
</script> 

通常情況下,你不得不添加一個現成的處理程序,但你不知道,如果你連接到窗口,因爲它總是存在的。

+0

非常感謝,完美!我非常感謝你的幫助。我不知道如何激活jQuery ...每個人都在某處開始:) – user1500354 2012-07-05 18:07:43