r/JavaScriptTips • • 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.

Post image
3 Upvotes

2 comments sorted by

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.matches is a boolean property and not a number afaik, you could try changing x.matches by window.innerWidth and 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) to if (x.matches) { ... }. You'll also have to change your media query string to something like "(min-width: 1024px) and (max-width: 1200px)".

2

u/EugeneGlassMenagerie Feb 20 '22

Dude Thank you so much!! This was such a treat to wake up to!! It's working & you gave an excellent explanation. i was having the tuffest time with it, losing sleep over it actually lol. it's my first media query in javascript, usually i use css, but it was stretching out the canvas funny. Thank you again, i had been trying for 4 days straight. was getting mixed up majorly. Bro you made my day for sure

i went with the media query api one, cause it seemed to work with the knowledge you shared right off the bat, so i was like i can work with this. Now i can get back to dialing this project in and making it fit all screen sizes. Thank you again!!