再会!
在网络上找到了一个判断屏幕大小的脚本,但是想不通如何正确设置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
}
但我认为上面的脚本更通用。
PS 能不能多吃点正确定义屏幕大小的方法?
谢谢