MediaQueryList: removeListener() 方法
已棄用:此特性不再推薦。雖然某些瀏覽器可能仍然支援它,但它可能已經從相關的網路標準中刪除,可能正在刪除過程中,或者可能僅為相容性目的而保留。請避免使用它,如果可能,請更新現有程式碼;請參閱本頁底部的相容性表格以指導您的決策。請注意,此特性可能隨時停止工作。
MediaQueryList 介面的 removeListener() 方法從 MediaQueryListener 中移除一個監聽器。
在較舊的瀏覽器中,MediaQueryList 尚未從 EventTarget 繼承,因此提供了此方法作為 EventTarget.removeEventListener() 的別名。如果需要支援的瀏覽器中可以使用 removeEventListener(),則應使用它而不是 removeListener()。
語法
js
removeListener(func)
引數
func-
一個函式或函式引用,表示要移除的回撥函式。
返回值
無(undefined)。
示例
js
const paragraph = document.querySelector("p");
const mediaQueryList = window.matchMedia("(width <= 600px)");
function screenTest(e) {
if (e.matches) {
/* the viewport is 600 pixels wide or less */
paragraph.textContent = "This is a narrow screen — 600px wide or less.";
document.body.style.backgroundColor = "pink";
} else {
/* the viewport is more than 600 pixels wide */
paragraph.textContent = "This is a wide screen — more than 600px wide.";
document.body.style.backgroundColor = "aquamarine";
}
}
mediaQueryList.addListener(screenTest);
// Later on, when it is no longer needed
mediaQueryList.removeListener(screenTest);
規範
| 規範 |
|---|
| CSSOM 檢視模組 # dom-mediaquerylist-removelistener |
瀏覽器相容性
載入中…