r/JavaScriptTips • u/EugeneGlassMenagerie • Feb 19 '22
Could use a little help with Javascript Media Queries, not sure whats wrong. I feel like it's so close but i'm doin something incorrect. also if there's a spot i can look at some JS media queries examples that have else if statements would be rad also. youtube only had a few, i'm doin Vanilla.
3
Upvotes
2
u/mao034 Feb 20 '22
I think you are unnecessarily mixing the media query js api with window resize event, you can either use one or the other for the same purpose.
If you want to use window resize event you'll have to change line 97 if statement because
x.matchesis a boolean property and not a number afaik, you could try changingx.matchesbywindow.innerWidthand remove line 93 and 94.If you want to use media query api; replace window resize event at line 116 by
x.addEventListener('change', myfun);and change your if statement (line 97) toif (x.matches) { ... }. You'll also have to change your media query string to something like"(min-width: 1024px) and (max-width: 1200px)".