簡體   English   中英

滾動100px后將div位置設置為固定?

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

我嘗試使用以下函數,以便在滾動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>

它不起作用(div堅持它的固定位置)。 似乎該功能與div無關。 我的問題是什么?

你的問題是你的divclass標題,而不是id 嘗試<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