0

I had a question about something weird. I have been using jquery window.with and window.resize but I don't feel like it is accurate, I have set it to do something at max-width 1200px, but it does it at 1280px? Is there something wrong with my code?

jQuery(window).resize(function() {
        if (jQuery(window).width() <= 1200) {
            if(!jQuery(".navbar-collapse .nav li").hasClass("fa fa-chevron-down")) {
                document.querySelector('style').textContent += "@media screen and (max-width: 1200px) { " +
                    ".fa, .fa-chevron-down:hover { color: black; }" +
                    ".fa-chevron-down:before {content: normal;}" +
                    ".nav .parent:before {display: inline-block;\n" +
                    "        content: '\\f078';\n" +
                    "        -webkit-border-after-width: 20px;\n" +
                    "        top: 7px;\n" +
                    "        position: absolute;\n" +
                    "        right: 24px;\n" +
                    "        font-size: 25px;\n" +
                    "        z-index: 8;}" +
                    "" +
                    "}";
            }
            jQuery('.navbar-collapse .nav li').addClass("fa fa-chevron-down");
        }
        else if(jQuery(window).width() >= 1201) {
            jQuery('.navbar-collapse .nav li').removeClass("fa fa-chevron-down");
        }
    });

Does anyone know how to fix this problem so it just adds the class at 1200px and removes it as 1201px?

3
  • it's rather weird to add a media query through JS under the resize event - why not just put it directly in your CSS? Commented Nov 24, 2017 at 8:53
  • Mmh, I added it because for some reason if I didnt it started to do weird things with this code, but with the solution below I could put it back in css? Commented Nov 24, 2017 at 9:02
  • 1
    Definitely. Using JS as a crutch for the UI is a really bad idea Commented Nov 24, 2017 at 9:10

2 Answers 2

4

I believe the problem is, that the window.width() returns the width of the window including scrollbars etc. (pls, correct me if I'm wrong...). I have once needed something similar and I used a function to determine the width:

function viewport() {
  var e = window, a = 'inner';
  if (!('innerWidth' in window )) {
    a = 'client';
    e = document.documentElement || document.body;
  }
  return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

With this you can check for the window-width as follows:

if ((viewport().width <= 1200)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this worked like a charm! I didn't know that it took the scrollbars :O
I'm glad it worked... I had the same issue once and I found somewhere, that the window width includes the scrollbars and then found this solution somewhere along the way. :)
0

remove jQuery(window).resize(function()

fiddle link that works fair simple

if (jQuery(window).width() < 767) {
$("p").append("<b>Appended text less than 767</b>");
}
else if(jQuery(window).width() > 767) {
$("p").append("<b>Appended text greater than than 767</b>");
}

1 Comment

answer of Kathara also correct but getting width and then check for width is not coool one

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.