Yahreen
Yahreen

Reputation: 1669

If / Else on Function

I am using Paul Hayes' CSS Transitions Media Query script and can not figure out how to do a simple if/else.

Here is what I am going after:

mql('all and (max-width: 959px)', change);
mql('all and (min-width: 960px)', change);

function change(mql) {
    if(max-width: 959px) {
        console.log("Do this");
    } else {
        console.log("Do that");
    }
}

Here is mql / paul's script:

mql = (function(doc, undefined) {

    var docElem = doc.documentElement,
        refNode = docElem.firstElementChild || docElem.firstChild,
        idCounter = 0;
        if(!doc.getElementById('mq-style')) {
          style = doc.createElement('style');
          style.id = 'mq-style';
          style.textContent = '.mq { -webkit-transition: width 0.001ms; -moz-transition: width 0.001ms; -o-transition: width 0.001ms; -ms-transition: width 0.001ms; width: 0; position: absolute; top: -100em; }\n';
          docElem.insertBefore(style, refNode);          
        }

     var transitionEnds = Array('transitionend','webkitTransitionEnd','oTransitionEnd','msTransitionEnd');

     for(var i in transitionEnds) { 
    if ('on'+ transitionEnds[i].toLowerCase() in window)  transitionEnd = transitionEnds[i];
     }  

    return function(q, cb) {

        var id = 'mql-' + idCounter++,
            callback = function() {
                // perform test and send results to callback
                cb({
                    matches: (div.offsetWidth == 42),
                    media: q
                });
            },
            div = doc.createElement('div');

        div.className = 'mq'; // mq class in CSS declares width: 0 and transition on width of duration 0.001ms
        div.id = id;
        style.textContent += '@media ' + q + ' { #' + div.id + ' { width: 42px; } }\n';        

        // add transition event listener
        div.addEventListener(transitionEnd, callback, false); 

        docElem.insertBefore(div, refNode);

        // original polyfill removes element, we need to keep element for transitions to continue and events to fire.

        return {
            matches: div.offsetWidth == 42,
            media: q
        };
    };

})(document);

Upvotes: 0

Views: 171

Answers (3)

albinohrn
albinohrn

Reputation: 626

The only way I can se this be done with the information provided in the change function is this:

mql('all and (max-width: 959px)', change);
mql('all and (min-width: 960px)', change);

function change(mql) {
    if(mql.media.indexOf('max-width: 959px') !== -1 && mql.matches) {
        console.log("Do this");
    } else {
        console.log("Do that");
    }
}

But I have to say that it doesn't feel that good... I would consider two separate callbacks one for each media-query.

Upvotes: 0

xyz
xyz

Reputation: 396

var mq = window.matchMedia( "(min-width: 959px)" ); 
mq.addListener(WidthChange);
WidthChange(mq); 

function WidthChange(mq) {
    if (mq.matches) {
        console.log("Do this"); // window width is at least 959px
    } else {
        console.log("Do this"); // window width is at less than 959px
    }
}

Upvotes: 0

Faraday
Faraday

Reputation: 2954

Your if needs to change to something akin to:

mql('all and (max-width: 959px)', change);
mql('all and (min-width: 960px)', change);

function change(mql) {
    if(max-width == '959px') {
        console.log("Do this");
    } else {
        console.log("Do that");
    }
}

Upvotes: 1

Related Questions