1

I have <header> and <nav> blocks that are affected by JavaScript. What I would like is for the <nav> block to become position: static if the user resizes the window to smaller than 1119px wide. The script currently works only on page load, but does not detect resize.

I've tried applying the solution from this question, but with no luck. Here's a link to the webpage in question:

http://alookat.org/resize-js/

Here's my JavaScript so far:

<script>
$(document).ready(function(){
    var $window = $(window);


    function checkWidth() {
        windowsize = $window.width();

        if (windowsize > 1119) {
           $(window).scroll(function() {
                if ($(this).scrollTop()>119)
                {
                    $('header').fadeOut();
                    $('nav').css({position: 'fixed', top: '0px'});
                }
                else
                {
                    $('header').fadeIn();
                    $('nav').css({position: 'absolute', top: 'auto'});
                }
            });
        }
        else {
            $('nav').css({position: 'static', top: '0px'});
        }
    }

    // can trigger the resize handler instead of
    // explicitly calling checkWidth()
    $(window).resize(checkWidth).resize();
});
</script>
3
  • windowsize = $window.width(); try adding var before windowsize. Commented Jan 14, 2014 at 12:20
  • and move the function in global scope outside of doc ready handler. Commented Jan 14, 2014 at 12:21
  • Seems more like a job for CSS media queries to me. Kind of pointless to do this in JS. At least the > 1119 part. The scrolling part will still need to be handled in js Commented Jan 14, 2014 at 12:24

3 Answers 3

1

Use .trigger() for this:

$(window).resize(checkWidth)
         .trigger('resize');

Try following demo. When you open console try to resize window in console log should be outputed when ever you resize it.

Try this code in your server:

$(document).ready(function(){
    var $window = $(window);

    $window.on('resize', function () {
        windowsize = $window.width();

        if (windowsize > 1119) 
        {
            $window.scroll(function() {
                if ($(this).scrollTop()>119)
                {
                    $('header').fadeOut();
                    $('nav').css({position: 'fixed', top: '0px'});
                }
                else
                {
                    $('header').fadeIn();
                    $('nav').css({position: 'absolute', top: 'auto'});
                }
            }).scroll();
        }
        else 
        {
            $('nav').css({position: 'static', top: '0px'});
            $('header').fadeIn();
            $window.off('scroll');
        }
    }).resize();

});

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

I can see the message in the console, which is great. The functionality of the JavaScript is the same as before though—it breaks if I resize from 1119px wide or wider downwards and then scroll. I've added a link to the actual webpage with the code in context here so you know what I mean. Thanks for your help.
can you add debugger; inside of your checkWidth function for testing purpose?
Do you want to do something like this? If it does what you want just let me know I will update my answer accordingly. Or if you want some addition put here you comments.
That is exactly what I meant. Works great. Thanks so much! Legend!
1

Use media queries in CSS (add somewhere in css)

@media (max-width: 1119px) {
  nav {position: 'static' !important; opacity: 1 !important;}
}

You can simplify your js

<script>
$(document).ready(function() {
    $(window).scroll(function() {
        if ($(this).scrollTop()>119) {
            $('header').fadeOut();
            $('nav').css({position: 'fixed', top: '0px'});
        } else {
           $('header').fadeIn();
           $('nav').css({position: 'absolute', top: 'auto'});
        }
     });
});
</script>

The scroll function will still execute, but will not make any visual changes. This is good, because it will keep track of the changes and resizing after scrolled down will react properly.

2 Comments

Thanks for the answer. The problem with the media query (even with the !important;) is that the JavaScript overrides it. I've uploaded a proper webpage with the code to make it easier to understand.
I think you missed !important for nav. Also i see there is a mistake in my code '.nav' should be 'nav'
0

Your current code only runs once when the document loads. You need to use something like:

$(window).on('resize', function () {
// your code here

});

So you bind to the $(window)

1 Comment

This is really close. Thanks for your help. A problem with this code though is that the browser (Chrome, Safari) almost crashes if I spend too long resizing (I think it's checking and noting different window sizes lots of times a second). I also want to retain the ability to check the screen size on page load, as well as page resize. Here's a link to the actual webpage with the original code to make it easier to understand the problem. The page breaks if I resize from 1119px wide or wider downwards and then scroll.

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.