[英]window.matchMedia for 3 intervals
I need for my web 3 intervals of matchMedia in javascript. 我需要在javascript中将我的Web 3个间隔设置为matchMedia。 I tried to use this:
我试图用这个:
if (matchMedia) {
var m1 = window.matchMedia("(max-width:640px)");
var m2 = window.matchMedia("(max-width:1024)");
m1.addListener(WidthChange);
m2.addListener(WidthChange);
WidthChange(m1,m2);
}
function WidthChange(m1,m2) {
if(m1.matches) {
console.log('ok1')
}else if(m2.matches){
console.log('ok2')
}else{
console.log('ok3')
}
it doesnt work. 它不起作用。 How do I have to modify it?
我该如何修改? Thanks for any help!
谢谢你的帮助!
I think you're just missing the "px" from: 我认为您只是缺少以下方面的“ px”:
var m2 = window.matchMedia("(max-width:1024)");
Also you don't want to declare m1
and m2
as the arguments to WidthChange
, because that will override the m1
and m2
variables that you declared above. 另外,您也不想将
m1
和m2
声明为WidthChange
的参数,因为这将覆盖您在上面声明的m1
和m2
变量。 The argument to WidthChange
will actually be a MediaQueryListEvent
, which you're not using in your callback. WidthChange
的参数实际上是一个MediaQueryListEvent
,您没有在回调中使用它。 So just declare it like this: 所以就这样声明:
function WidthChange() {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.