const mqList = [
window.matchMedia('(min-width: 0px) and (max-width: 768px)'),
window.matchMedia('(min-width: 768px) and (max-width: 992px)'),
window.matchMedia('(min-width: 992px)'),
];
function mqIf() {
if (mqList[0].matches) {
document.querySelector('body').style.background = 'red';
}
if (mqList[1].matches) {
document.querySelector('body').style.background = 'white';
}
if (mqList[2].matches) {
document.querySelector('body').style.background = 'green';
}
}
mqIf();
for (let i = 0; i < mqList.length; i += 1) {
mqList[i].addEventListener('change', mqIf);
}
const media = {
queryList: {
mobile: window.matchMedia('(min-width: 0px) and (max-width: 768px)'),
tablet: window.matchMedia('(min-width: 768px) and (max-width: 992px)'),
desktop: window.matchMedia('(min-width: 992px)'),
},
queryIf(func, type) {
if (this.queryList[type].matches) func();
},
query(type, func) {
this.queryIf(func, type);
this.queryList.mobile.addEventListener('change', () => this.queryIf(func, type));
},
};
export default media;
import media from '/path';
media.query('mobile', () => document.querySelector('body').style.background = 'red');
media.query('tablet', () => document.querySelector('body').style.background = 'green');
export const Media = () => {
const mqList = [
window.matchMedia('(min-width: 0px) and (max-width: 768px)'),
window.matchMedia('(min-width: 768px) and (max-width: 992px)'),
window.matchMedia('(min-width: 992px)'),
];
function mqIf() {
if (mqList[0].matches) {
document.querySelector('body').style.background = 'red';
}
if (mqList[1].matches) {
document.querySelector('body').style.background = 'white';
}
if (mqList[2].matches) {
document.querySelector('body').style.background = 'green';
}
}
mqIf();
for (let i = 0; i < mqList.length; i += 1) {
mqList[i].addEventListener('change', mqIf);
}
};
import media from './file-name.js';