1

My ultimate goal is to get my header to switch from a position of relative to fixed when the window gets to a certain point. In trying to get there I came up with this function which doesn't give my any errors but also dosen't appear to do anything. Could someone help me understand why? It's the last script loaded on the page and jquery is called before it..

<script type="text/javascript">
$(function() {
var $window = $(window);
function top() {
var $top = $window.scrollTop();
if( $top > 100 ) {
    $("header").css("position","absolute"); 
}
else {
    $("header").css("position","fixed");    
}
};
});
</script>

What if i wanted to limit it to mobil applications? shouldn't something like this work...

<script type="text/javascript">
 $(function() {
     var $window = $(window).width();
     function windowWidth() {
         if ( $window < 480 ) {
      function top() {
          var $top = $window.scrollTop();
          if( $top > 100 ) {
             $("header").css("position","absolute"); 
           }
           else {
              $("header").css("position","fixed");    
          }
     };
     $(window).scroll(top);
         }
     };
   });
</script>

3 Answers 3

1

Use $(window).scroll(top) or use setTimeInterval(top, 100);

Assuming header as id

<script type="text/javascript">
 $(function() {
     var $window = $(window);
      function top() {
          var $top = $window.scrollTop();
          if( $top > 100 ) {
             $("#header").css("position","absolute"); 
           }
           else {
              $("#header").css("position","fixed");    
          }
     };
     $(window).scroll(top);
   });
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Window resize will only work if the window changes size. It doesn't track the position of the scroll bar.
@Styphon My intentation was scroll by mistake I wrote resize.
Thanks for the help! I took your code and put it in and it works great. Now i'm wondering how i would limit it when in happens to when it's viewed by mobil phones. I changed it up a bit but not getting much luck.
@StephenCrane header is a class or id? If it is class use $(".header") and if it is id use $('#header')
@Sushil Neither it's the html header tag.
|
0

The reason it's not doing anything is you're not calling it. You have nothing to trigger the code. I assume it's this script you found. If you notice it's wrapped in a $(window).scroll, instead of just function, to trigger it.

You need to modify your code thusly:

$(window).scroll(function() {
    if($(window).scrollTop() > 100) {
        $("header").css("position","absolute"); 
    } else {
        $("header").css("position","fixed");    
    }
});

Comments

0

I think you have not called the .scroll() function anywhere in the given code so it never going to be fired:

<script type="text/javascript">
  $(function() {
     var $window = $(window);
     function top() {
        var $top = $window.scrollTop();
        if( $top > 100 ) {
            $("header").css("position","absolute"); 
        }else {
            $("header").css("position","fixed");    
        }
     };
     $window.scroll(top);
  });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.