简体   繁体   中英

Dynamically add class on browser resize (viewport width)

I have an element called jobfilter, which I want to add a class to depending on the viewport width, ie. when I resize my browser to <1000px, I want to add the class .hidden to .jobfilter.

Now, I managed to get it half-working with the help of this link: $(window).width() not the same as media query . Using this:

  function checkPosition() {
    if (window.matchMedia('(max-width: 767px)').matches) {
        //...
    } else {
        //...
    }
}

Here's my JSFiddle: http://jsfiddle.net/ck55Lf01/10/ .

If you resize the browser and refresh the page, the jobfilter hides, but I'd like that to happen dynamically, not on the refresh of the page, thank you for your help!

This is a fucnction I use to dynamically check the window width on resize, I wrapped it in a document ready function that passes the $ as a parameter to prevent any conflicts that might occur with other javascript libraries that use the $. An example would be if you were to use your function inside of a wordpress theme or plugin.

Jsfiddle example: http://jsfiddle.net/larryjoelane/ck55Lf01/24/

Javascript:

/*
document ready function used to prevent conflict with other javascript libraries
that use the $ parameter
*/
jQuery(document).ready(function($){

     //get the window width
     var winWidth =  $(window).width();

      //set the maxWidth
     var maxWidth = 1000;

          //if the window width is less than the maxWidth pixels on document loading
          if(winWidth < maxWidth){//begin if then

            //add the class hidden to .jobFilter         
            $(".jobfilter").addClass("hidden");

         }//end if then



$(window).resize(function(){//begin resize event


    //get the window width
     var winWidth =  $(window).width();

      //set the maxWidth
     var maxWidth = 1000;    


         //if the window width is less than maxWidth pixels
         if(winWidth < maxWidth){//begin if then

            //add the class hidden to .jobFilter         
            $(".jobfilter").addClass("hidden");

         }
         else{

            //remove the class hidden                 
            $(".jobfilter").removeClass("hidden");


     }//end if then else


     });//end window resize event

});//end document ready function

Little known fact: matchMedia has an event listener:

function handleMedia(mql) {

    if (mql.matches) {
        // media query matches
    } else {
        // media query does not match
    }

}

var mql = window.matchMedia('(max-width: 767px)').addListener(handleMedia);
handleMedia(mql);

The event listener will execute every time the browser matches or unmatches the media query. In your case, I'd recommend firing the handler manually as well (as shown in the example) to be sure to get the correct class set on load.

This example is taken from MDN (although it's pretty well hidden).

you have to listen on the resize event.

$( window ).resize(function() {
   checkPosition();
});

Use the resize event to check if the window is resized or not. Use this code

$( window ).resize(function() {
function checkPosition() {
if (window.matchMedia('(max-width: 767px)').matches) {
    //...
} else {
    //...
}

}
}

Hope this helps you

$(document).ready(function(){
   DOaction(); // run function after document ready
    $(window).on('resize',DOaction); // on resize window run function
});
// function to add and remove hidden class
function DOaction(){
    if($(window).width() <= 1000){
        $(".jobfilter").addClass('hidden');
    }else{
        $(".jobfilter").removeClass('hidden');
    }
}

Jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM