2012-07-05 61 views
1

我试着用下面的功能,以设置div的从顶部100像素位置滚动100像素之后。集DIV位置滚动100px的固定后?

<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
$(window).scroll(function(){ 
    $("#header").css("top",Math.max(0,100-$(this).scrollTop())); 
}); 
</script> 
<div class="header" style="position:fixed;top:100px;background-color:red">something</div> 

它不工作(该div坚持它的固定位置)。似乎功能没有涉及到div。什么是我的问题吗?

回答

3

你的问题IST您divclass头,而不是id。 尝试 <div id="header" style="position:fixed;top:100px;background-color:red">something</div>

+0

谢谢,这个工作。 – user1481850 2012-07-05 09:16:21

0
$(document).ready(function(){ 
    $('.header').scroll(function(){ 
     $(this).css("top",Math.max(0,100-$(this).scrollTop())); 
    }); 
});