mardi 18 décembre 2018

How to update the value of a variable on window resize via jquery?

I am trying to build a function that triggers when the page is resized and updates the value of a variable if it passes a condition, but I'm unable to update the variable in any capacity. I guess I don't know where I should declare the variable properly or where to update it.

var $padding;

    function checkWidth(){
        if (($(window).width) > 750) {
            $padding = 54;
        }
        else if (($(window).width) < 751) {
            $padding = 100;
        }
        $("main").css("padding", $padding + "px 0 0 0");
    }

    $(window).resize(function(){
        checkWidth();
        console.log($(window).width());
        console.log($padding);
    });

In this case, the value of $padding is always undefined.

Placing the $padding variable on top of the checkWidth or resize functions triggers a "$padding is not defined" so I don't know where else it could go. How do I get the value to update?

Aucun commentaire:

Enregistrer un commentaire