Search code examples
jqueryresponsive-designbrowser-width

Is it possible to perform a jQuery function in between certain browser widths?


Hello I have a jQuery function that I would like to work only between 1199px and 700px. Is this possible? I found the following code, it works so when it hits 1199px the function kicks in but how do I make it so at 700px the function stops working?

function checkWidth() {
    if (jQuery(window).width() < 1199) {
        //do something
    } else {

    }
};
checkWidth();

Thanks!


Solution

  • function checkWidth() {
        if (jQuery(window).width() < 1199 && jQuery(window).width() > 700) {
            //do something
        } else {
    
        }
    };
    checkWidth();
    

    Also:

    I would store the width as a variable so you don't have to call that twice.

    function checkWidth() {
        var windowWidth = jQuery(window).width();
        if (windowWidth < 1199 && windowWidth > 700) {
            //do something
        } else {
    
        }
    };
    checkWidth();