views:

30

answers:

2

I am trying to implement a simple floating navigation using the DOM and Javascript. The following script is triggered using the onscroll event, but nothing happens and debugging through firebug has not been all that enlightening.

function float_nav() {
  nav = document.getElementById("nav_container");
  offset = window.pageYOffset + 'px;';
  nav.style.top=offset;
}
A: 

You don't have to add the ; at the end of px. Otherwise I see nothing wrong.

Additional: why don't you use jQuery or something similar?

Makram Saleh
I'm a tard, taking out the ';' did it. I just wanted something as lightweight as possible.
agentargo
jQuery, imo, isn't a good replacement for something so simple... Remember that there's always "plain" JavaScript with DOM manipulation. Adding jQuery would add quite a few kB to the end file, compared to the few hundred bytes required to simply make an element position:fixed, left:0px, top:0px
ItzWarty
+1  A: 

You can just set up it in styles without javascript

#nav_container {
  top: 0;
  left: 0;
  position: fixed;
  /* IE6 fix for position: fixed */
  _position: absolute;
  _top: expression( document.body.scrollTop + 'px' );
}
fantactuka
I say let ie6 fail and clean up your css.
Kenneth J
I'm using a grid that I prefer to keep intact to keep my css clean, so I vied to use JS
agentargo