tags:

views:

26

answers:

3

I have a div tag <div id="customError">Error</div> I want this div tag to always appear at the top of the page using jquery, so with otherwords even if the page is scrollable I want it to be always visible and on top of the page almost like the stackoverflow notification bar. Is this possible using JQuery? I tried a number of things and it disappears when the page is scrolled down. Any help advise will be appreciated.

+1  A: 

CSS:

.top {
   top: 0px;
   position: fixed;
}

should meet your requirements.

If you, for some reason, need to dynamically adapt the position of an element, try this:

$(function(){
    var $myelement = $('#element_that_should_stay_on_top');

    $(window).bind('scroll', function(){
       $myelement.css({
           position: 'absolute',
           top: $(window).scrollTop()
       });
    });
});
jAndy
+1  A: 

use this

#customError { 
   position:fixed;
   top:0;
   left:0;
}
Starx
+3  A: 

You can use css only:

#customError {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;

  /* IE6 */
  _position: absolute;
  _top: expression(document.getElementsByTagName("body")[0].scrollTop + "px");
}
fantactuka
ugh, a CSS expression. It works, yes; but the performance will be abysmal. Maybe use just `_position:absolute` for IE6 (graceful degradation)?
Piskvor
I think that 1 expression won't harm the performance.
fantactuka