简体   繁体   English

滚动100px后将div位置设置为固定?

[英]set div position to fixed after scrolling 100px?

I tried to use the following function in order to set the div's position to 100 px from top after scrolling 100 px. 我尝试使用以下函数,以便在滚动100像素后将div的位置从顶部设置为100 px。

<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>

it is not working(the div stick to it's fixed position). 它不起作用(div坚持它的固定位置)。 it seems that the function is not relating to the div. 似乎该功能与div无关。 what is my problem ? 我的问题是什么?

Your problem ist that your div has the class header, not the id . 你的问题是你的divclass标题,而不是id Try <div id="header" style="position:fixed;top:100px;background-color:red">something</div> 尝试<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()));
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM