簡體   English   中英

使用jQuery向div添加固定位置

[英]Using jQuery to add a fixed position to a div

我正在嘗試實現div滾動的效果,直到到達頂部並停留在頂部為止。

我已經做到了:

的HTML

<div id="nav">this is nav</div>
    <div id="mooey">
    <div id="theFixed" style="position:fixed; background-color:red">SOMETHING</div>
</div>

的CSS

#mooey {
    background: green;
    min-height:250px;
    margin-top:300px;

}

#nav {
    background:#000000;
    position:fixed;
    top:0;
    width:100%;
    height:100px;
}

的JavaScript

$(window).scroll(function(){
    $("#theFixed").css("top", Math.max(100, 300 - $(this).scrollTop()));
});

我想做什么,而不是說div theFixed是固定在HTML樣式中的。 我想知道是否有一種使用代碼來應用此方法的方法。

原因是是,如果腳本不能夠或失敗不管出於什么原因-我希望theFixed DIV與一起滾動mooey卡在頁面中間的div而不是。

您可以在這里查看我的操作:

http://jsfiddle.net/susannalarsen/4J5aj/7/

有什么想法嗎?

使用$('#theFixed').css('position','fixed'); 固定下來。

<script>
$(document).ready(function(){
  $("#FixedElement").css("position","fixed");
});
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM