forked from movsim/traffic-simulation-de
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME_mediaQuery2js
More file actions
31 lines (23 loc) · 878 Bytes
/
README_mediaQuery2js
File metadata and controls
31 lines (23 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
https://modernweb.com/using-media-queries-in-javascript/
Motivation: Do not display all elements if small screen such as
smartphone
var mq = window.matchMedia('@media all and (max-width: 700px)');
if(mq.matches) {
// the width of browser is more then 700px
} else {
// the width of browser is less then 700px
}
// listener for changes: mq->nameOfFunction (here "changed")
mq.addListener(function(changed) {
if(changed.matches) {
// the width of browser is more then 700px
} else {
// the width of browser is less then 700px
}
});
// also works for more complex filters:
@media all and (orientation: portrait) { ... }
@media screen and (min-resolution: 2dppx) { ... }
var mq1 = window.matchMedia('@media all and (orientation: portrait)');
var mq2 = window.matchMedia('@media screen and (min-resolution: 2dppx)');
//!! 2dppx resolution