MediaQueryListEvent: matches ããããã£
Baseline
åºãå©ç¨å¯è½
ãã®æ©è½ã¯åºãå®è£ ããã¦ãããå¤ãã®ãã¼ã¸ã§ã³ã®ç«¯æ«ããã©ã¦ã¶ã¼ã§åä½ãã¾ãã2020å¹´9æä»¥éããã¹ã¦ã®ãã©ã¦ã¶ã¼ã§å©ç¨å¯è½ã§ãã
matches 㯠MediaQueryListEvent ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®èªã¿åãå°ç¨ããããã£ã§ãè«çå¤ã§ãã true ãªãã° document ãç¾å¨ã¡ãã£ã¢ã¯ã¨ãªã¼ãªã¹ãã«ä¸è´ãã¦ãããã¨ã表ãã false ã¯ããã§ãªããã¨ã表ãã¾ãã
å¤
è«çå¤ã§ãã true ãªãã° document ãç¾å¨ã¡ãã£ã¢ã¯ã¨ãªã¼ãªã¹ãã«ä¸è´ãã¦ãããã¨ã表ãã false ã¯ããã§ãªããã¨ã表ãã¾ãã
ä¾
js
const para = document.querySelector("p"); // This is the UI element where to display the text
const mql = window.matchMedia("(max-width: 600px)");
mql.addEventListener("change", (event) => {
if (event.matches) {
// The viewport is 600 pixels wide or less
para.textContent = "This is a narrow screen â less than 600px wide.";
document.body.style.backgroundColor = "red";
} else {
// The viewport is more than 600 pixels wide
para.textContent = "This is a wide screen â more than 600px wide.";
document.body.style.backgroundColor = "blue";
}
});
仿§æ¸
| 仿§æ¸ |
|---|
| CSSOM View Module > # dom-mediaquerylistevent-matches > |