Skip to content

Instantly share code, notes, and snippets.

@yumitsu
Created April 27, 2015 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumitsu/426fd370a830179bb73e to your computer and use it in GitHub Desktop.
Save yumitsu/426fd370a830179bb73e to your computer and use it in GitHub Desktop.
// Tests: http://jsfiddle.net/yumitsu/k18fkfgt/
var MobileDevice = {
init: function() {
var j, device, _this = this;
device = {
'mobile': _this.isMobileDevice(),
'mobileDimensions': _this.isDimsMatching(),
'webkit': _this.isWebKitDevice(),
'mobileSafari': _this.isMobileSafari(),
'touch': _this.isTouchDevice(),
'ipad': _this.isIpadFamily()
};
j = window.jQuery || $;
if (j != null) {
if (!j.isReady) {
j(document).ready(function() {
window.device = device;
});
} else {
window.device = device;
}
}
},
viewport: (function() {
var zoom = (this.isLandscape() ? window.screen.height : window.screen.width) / window.innerWidth;
return {
'w': window.innerWidth,
'h': window.innerHeight,
'x': window.scrollX / (window.devicePixelRatio || 1),
'y': window.scrollY / (window.devicePixelRatio || 1),
'zoom': zoom,
'dpxPerSpx': zoom * (window.devicePixelRatio || 1),
};
}),
originalDims: (function() {
return {
'width': (window.document.width != null ? window.document.width : (jQuery != null ? jQuery(window).width() : window.screen.width)),
'height': (window.document.height != null ? window.document.height : (jQuery != null ? jQuery(window).height() : window.screen.height)),
'scrollX': window.scrollX,
'scrollY': window.scrollY,
};
}),
offsets: (function() {
return {
'x': window.pageXOffset,
'y': window.pageYOffset,
};
}),
dims: function() {
return {
'width': (this.isLandscape() ? window.screen.height : window.screen.width),
'height': (this.isLandscape() ? window.screen.width : window.screen.height)
};
},
calculateOffset: function(wrapWidth) {
var rez = this.viewport().x + 20;
return (((Math.max(rez, (rez + ((this.viewport().w - wrapWidth) * 0.5))))/this.viewport().zoom)/2)-20;
},
isLandscape: function() {
return (this.isWebKitDevice() && window.orientation != null && Math.abs(window.orientation) == 90);
},
isPortrait: function() {
return (this.isWebKitDevice() && window.orientation != null && Math.abs(window.orientation) == 0);
},
isZoomed: function() {
var values = [];
if (this.isIpadFamily()) {
values = [0.8, 1.1];
} else {
values = [0.33, 0.573];
}
return (this.isLandscape() ? (this.viewport().zoom > values[1]) : (this.viewport().zoom > values[0]));
},
isMobileDevice: function() {
return (/(iphone|ipad|ipod|android)/i.test(navigator.userAgent) || this.isDimsMatching());
},
isDimsMatching: function() {
return (window.screen.width >= 320 && window.screen.width <= 1024 && window.screen.height >= 480 && window.screen.height <= 1024);
},
isWebKitDevice: function() {
return (window.devicePixelRatio != null && (window.devicePixelRatio > 1 || (window.devicePixelRatio == 1 && this.isDimsMatching())));
},
isMobileSafari: function() {
return (this.isWebKitDevice() && /.*?(iphone|ipad|ipod)\;.*?applewebkit\/.*?mobile\/.*?safari\//i.test(navigator.userAgent));
},
isTouchDevice: function() {
return (window.Modernizr != null ? !!window.Modernizr.touch : ('ontouchstart' in window));
},
isIpadFamily: function() {
return (this.isDimsMatching() && window.screen.height >= 768);
}
};
MobileDevice.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment