jQuery(document).ready(function(){
var eTop = jQuery('#img_upload').offset().top; //get the offset top of the element
jQuery(window).scroll(function() { //when window is scrolled
if((eTop – jQuery(window).scrollTop())<0){
jQuery('#img_upload').attr('style','width:180px; z-index: 1000; position: fixed; top: 0px;');
}else{
jQuery('#img_upload').attr('style','');
}
});
})
参考文件
http://stackoverflow.com/questions/3714628/jquery-get-the-location-of-an-element-relative-to-window
http://stackoverflow.com/questions/9880472/determine-distance-from-the-top-of-a-div-to-top-of-window-with-javascript