determine the element top

determine the element top

Hi,

I want to determine the top of my nav menu so when the user scrolls up and the nav reaches top position 40 then I want to change its position to fixed. I tried below code but didn't work.

kindly help..

  1. <script type="text/javascript">
  2. $(document).ready(function()
  3. {
  4. $(window).scroll(function(){
  5. // if($(window).scrollTop() > 80)
  6. if($("#nav").position().top <= 40)
  7. {
  8. $("#logoheader").hide( "drop", { direction: "down" }, "slow" );
  9. $("#imageSmallLogo").show( "drop", { direction: "down" }, "slow" );
  10. $("#nav").css("position", "fixed");
  11. }
  12. else
  13. {
  14. $("#logoheader").show( "drop", { direction: "up" }, "slow" );
  15. $("#imageSmallLogo").hide( "drop", { direction: "up" }, "slow" );
  16. }
  17. });
  18. });
  19. </script>