【发布时间】:2018-10-21 13:01:34
【问题描述】:
美好的一天!
在网络上找到了一个确定屏幕大小的脚本,但我不知道如何正确设置isMobile、isTablet、isDesktop的定义。
function() {
var i = {
mobile: "mobile",
tablet: "tablet",
desktop: "desktop",
oldHeight: null,
oldWidth: null,
current: function() {
return this.define()
},
isMobile: function() {
return this.define() === this.mobile
},
isTablet: function() {
return this.define() === this.tablet
},
isDesktop: function() {
return this.define() === this.desktop
},
define: function() {
return this.width() < 768 ? this.mobile : this.width() < 1360 ? this.tablet : this.desktop
},
height: function() {
return window.innerHeight
},
width: function() {
return window.innerWidth
},
init: function() {
this.oldHeight = window.innerHeight, this.oldWidth = this.define()
}
};
请告诉我...
现在我用这个方法:
if(
(screen.width <= 640) ||
(window.matchMedia &&
window.matchMedia('only screen and (max-width: 640px)').matches
)
){
// Do the mobile thing
}
但我认为上面的脚本更通用。
附:能吃更正确的定义屏幕大小的方法吗?
谢谢
【问题讨论】:
标签: javascript function screen matchmedia