如何模拟orientationchange来获取css数据?

时间:2016-03-06 16:27:52

标签: jquery media-queries

我有两个媒体查询

@media screen and (orientation : portrait) {
   .rect {width:100px;}
}

@media screen and (orientation : landscape) {
   .rect {width:200px;}
}

当我启动index.php时,我需要立即获得两个变量,例如

var rect_portrait = width //  when orientation is portrait (100 px)
var rect_landscape = width //  when orientation is landscape (200 px)

如何实现?提前谢谢

1 个答案:

答案 0 :(得分:1)

你走了!

landscape_height = $(window).innerHeight();
landscape_width  = $(window).innerWidth();

现在您可以按如下方式决定肖​​像与肖像:

if(landscape_height < landscape_width){
    console.log('landscape');
    //do something
}else if(landscape_height > landscape_width){
    console.log('portrait');
    //do somethihng
}

现在模拟反向检测景观与肖像的条件逻辑