在平板电脑上检测屏幕方向的最佳全局方法是什么?
What is the best global way to detect screen orientation on tablets
我正在寻找一个全球性的解决方案,将检测方向的平板电脑(不只是iPad或Android设备,但越多越好)。
一个解决方案:
device.portrait = function() {
return (window.innerHeight / window.innerWidth) > 1;
};
device.landscape = function() {
return (window.innerHeight / window.innerWidth) < 1;
};
我的观点是,以上这些并没有很好地发挥作用。
我需要很好的解决方案加载专用图像在旋转木马上的肖像视图和专用图像在景观视图当方向改变。
欢呼。
使用媒体查询
风景@media screen and (min-width: 700px) and (orientation: landscape) {
/*css for a image*/
}
肖像@media screen and (min-width: 700px) and (orientation: portrait) {
/*css for different image*/
}
我不知道为什么你的代码不能正常工作。
但是我可以建议你使用下面的代码
<span id="orientation">orientation</span>
$(document).ready(checkOrientation);
$(window).resize(checkOrientation);
function checkOrientation() {
var orientation = "Portrait";
var screenWidth = $(window).width();
var screenHeight = $(window).height();
if (screenWidth > screenHeight) {
orientation = "Landscape";
}
$("#orientation").html(orientation);
}
你可以在这里看到demo !
相关文章:
- 在javascript函数的循环中设置全局布尔变量的最佳语法
- 在jquery中设置多个全局变量的最佳方式
- 保存全局变量以供指令监视的最佳方法
- 将全局配置变量传递到手把模板的最佳方式是什么
- 避免节点中全局变量的最佳实践
- 通知全局事件组件的最佳方式
- 使用 Express 4 在 Node.js 中全局处理异常的最佳方法
- 在主干中使用全局变量.js .最佳实践
- Node.js & Express - 应用程序结构的全局模块和最佳实践
- JavaScript 中“全局”变量的最佳实践
- 全局命名空间中事件的最佳实践
- 最佳实践:全局对象与模块
- YUI库-保持对对象的全局引用的最佳方式
- 测试全局npm模块的最佳方式
- Angular过滤器|全局设置格式化(最佳实践)
- Javascript的最佳实践是全局使用一个模块实例
- 在Backbone中保存对全局集合的引用的最佳模式
- 在平板电脑上检测屏幕方向的最佳全局方法是什么?
- Angular JS -如何使表单验证成为全局服务?最佳实践
- 在jsp中加载全局可用库的最佳方式