Twitter Bootstrap - how to detect when media queries starts

后端 未结 16 770
抹茶落季
抹茶落季 2020-12-14 02:19

which is the fastest and easy way to fire when bootstrap-responsive.css media queries go in action?

go in action = when you resize window to mobile width and site is

16条回答
  •  萌比男神i
    2020-12-14 02:57

    Improving the excellent @falsarella answer, here is a shorter version that works with Bootstrap 4 :

    CSS :

    #mq-detector {
        visibility: hidden;
    }
    

    HTML :

    JAVASCRIPT :

    //Define function that returns the currently used media query
    function getBsMq(){
        var currMq;
        var mqDetector = $('#mq-detector [data-mq]');
        mqDetector.each(function(i){
            if ($(this).is(':visible')) {
                currMq = $(this).attr('data-mq');
            }
        });
        return currMq;
    }
    
    
    //Call the function and get the currently used media query
    alert(getBsMq());
    

提交回复
热议问题