javascript - set div position to fixed after scrolling 100px? - Stack Overflow

I tried to use the following function in order to set the div's position to 100 px from top after

I tried to use the following function in order to set the div's position to 100 px from top after scrolling 100 px.

<script src=".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>

it is not working(the div stick to it's fixed position). it seems that the function is not relating to the div. what is my problem ?

I tried to use the following function in order to set the div's position to 100 px from top after scrolling 100 px.

<script src="http://code.jquery./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>

it is not working(the div stick to it's fixed position). it seems that the function is not relating to the div. what is my problem ?

Share Improve this question asked Jul 5, 2012 at 9:09 user1481850user1481850 2483 gold badges13 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Your problem ist that your div has the class header, not the id. Try <div id="header" style="position:fixed;top:100px;background-color:red">something</div>

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744398751a4572278.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信