Fixed Position with JQuery

Menggunakan method .scrollTop() untuk memperbaharui nilai 'top' pada elemen relatif, sehingga elemen akan seolah-olah dikenai posisi 'fixed' - https://plus.google.com/108949996304093815163/about

by Taufik Nurrohman

HTML

<h2>Fixed Position with JQuery</h2>
<div></div>

CSS

body {
    padding:10px;
    height:3000px;
    border-left:4px dashed #ccc;
}

div {
    width:200px;
    height:100px;
    background-color:black;
    color:white;
    padding:15px;
    position:relative;
}

h2 {
    font:bold 16px Times,Serif;
    margin:0 0 10px;
}

JavaScript

$(window).on("scroll", function() {
    var position = $(this).scrollTop();
    $('div').css('top', position).html('div { top: ' + position + 'px; }');
});