1

I am using https://github.com/paulirish/matchMedia.js/ along with the listener extension, however, I do not know how to write a listener for the match media query below. Any assistance would be much obliged.

JS:

if (matchMedia("(min-width: 52em)").matches) {
  $("details").attr("open", "open");
}
4

1 回答 1

3
var handleMyMediaQuery = function(mql) {
        if (mql.matches) {
            // do match actions
        } else {
            // do unmatch actions
        }
    },
    myMediaQuery = window.matchMedia('(min-width: 52em)');

handleMyMediaQuery(myMediaQuery);
myMediaQuery.addListener(handleMyMediaQuery);

'handleMyMediaQuery' 的第一次使用会立即检查是否与媒体查询匹配,第二个 'myMediaQuery.addListener(handleMyMediaQuery)' 在媒体查询匹配时触发,然后在媒体查询不匹配时再次触发。

希望这是有道理的。

于 2013-06-27T16:10:49.067 回答