滚动100px后将div位置设置为固定? [英] set div position to fixed after scrolling 100px?
本文介绍了滚动100px后将div位置设置为固定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用以下功能,以便在滚动100像素后将div的位置从顶部设置为100像素。
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.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无关。我的问题是什么?
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 ?
推荐答案
你的问题是你的 div
有类
标题,而不是 id
。
尝试
< div id =headerstyle =position:fixed; top:100px; background-color:red> something< / div>
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>
这篇关于滚动100px后将div位置设置为固定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文