jQuery: Fire function only when certain media query is true

user1706680

I have a jQuery function which should only be fired when a certain media query is true (both on ready and resize).

I found a cool trick to check if a certain media query is true:

HTML

<div id="isthin"></div>

CSS

#isthin {
    display: inline-block;
    content: '';
    width: 1px;
    height: 1px;
    overflow: hidden;
}

@media only screen and (max-width: 1047px) {
    #isthin {
        display: none;
    }
}

jQuery

$(document).ready(function(){
    if ( $('#isthin').is(":visible") ) {
        // function should be fired
    } else {
        // function should not be fired
    }
});

$(window).resize(function(){
    if ( $('#isthin').is(":visible") ) {
        // function should be fired
    } else {
        // function should not be fired
    }
});

Separate jQuery function

$(function () {
      $('nav li ul').hide().removeClass('sub-nav');
      $('nav li').hover(function () {
        $('ul', this).stop().slideToggle(300);
      });
});    

That was my logic but somehow the function still runs on resizing when the media query changes.

Karl-André Gagnon

The probleme is not the media query itself, but the way you are binding your event.

You're binding it on every resize when the query fits. You should really detach all your function and use a flag. Just like that :

var flag = true;

$(window).resize(function(){

    flag =  $('#isthin').is(":visible");

    if(flag){
        $('nav li ul').hide().removeClass('sub-nav');
    }else{
        $('nav li ul').show().addClass('sub-nav');
    }

})

$(document).ready(function(){

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

$(function () {
    $('nav li').hover(function () {
        if(flag){
            $('ul', this).stop().slideToggle(300);
        }
    });
});  

Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Only fire jQuery function in responsive view

From Dev

jQuery .change() function must Fire only once

From Dev

Function of media query "only screen" syntax?

From Dev

jQuery validate fire second method only if first was true

From Dev

Fire appendTo() only once when document is ready (jQuery)

From Dev

Kindle Fire 8.9 HDX media query

From Dev

Kindle Fire 8.9 HDX media query

From Dev

How to only use jquery .css() on a specific media query?

From Dev

CSS media query (@media function)

From Dev

css @media query + jQuery button to show/hide div when screen gets smaller than a certain width + automatically restore it back when it gets larger

From Dev

jQuery fire function when element specific class change

From Dev

jQuery fire function when element specific class change

From Dev

Media query using jquery

From Dev

Run jQuery split() function only if div contains certain text

From Dev

Media Only Query Not Functioning as Intended

From Dev

Media Query to Target ONLY Mobile

From Dev

Only first media query working

From Dev

Media Query to Target ONLY Mobile

From Dev

jQuery: fire call on fadeIn function

From Dev

Apply jQuery autocomplete to an input field only when it has a certain class

From Dev

gwt checkbox fire handler when setValue(true)

From Dev

if class when hover then fire a function

From Dev

Media query certain on size of html element?

From Dev

Why does `not (some-width: Xem)` media query never fire?

From Dev

Why does `not (some-width: Xem)` media query never fire?

From Dev

LESS function that builds a media query

From Dev

Jquery .slideToggle() with CSS media query

From Dev

How to convert this media query to jquery?

From Dev

Jquery .slideToggle() with CSS media query

Related Related

  1. 1

    Only fire jQuery function in responsive view

  2. 2

    jQuery .change() function must Fire only once

  3. 3

    Function of media query "only screen" syntax?

  4. 4

    jQuery validate fire second method only if first was true

  5. 5

    Fire appendTo() only once when document is ready (jQuery)

  6. 6

    Kindle Fire 8.9 HDX media query

  7. 7

    Kindle Fire 8.9 HDX media query

  8. 8

    How to only use jquery .css() on a specific media query?

  9. 9

    CSS media query (@media function)

  10. 10

    css @media query + jQuery button to show/hide div when screen gets smaller than a certain width + automatically restore it back when it gets larger

  11. 11

    jQuery fire function when element specific class change

  12. 12

    jQuery fire function when element specific class change

  13. 13

    Media query using jquery

  14. 14

    Run jQuery split() function only if div contains certain text

  15. 15

    Media Only Query Not Functioning as Intended

  16. 16

    Media Query to Target ONLY Mobile

  17. 17

    Only first media query working

  18. 18

    Media Query to Target ONLY Mobile

  19. 19

    jQuery: fire call on fadeIn function

  20. 20

    Apply jQuery autocomplete to an input field only when it has a certain class

  21. 21

    gwt checkbox fire handler when setValue(true)

  22. 22

    if class when hover then fire a function

  23. 23

    Media query certain on size of html element?

  24. 24

    Why does `not (some-width: Xem)` media query never fire?

  25. 25

    Why does `not (some-width: Xem)` media query never fire?

  26. 26

    LESS function that builds a media query

  27. 27

    Jquery .slideToggle() with CSS media query

  28. 28

    How to convert this media query to jquery?

  29. 29

    Jquery .slideToggle() with CSS media query

HotTag

Archive