nuke-dimensions
Version:
屏幕参数
105 lines (91 loc) • 3.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _nukeEnv = require('nuke-env');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var FULL_WIDTH = 750;
var NAVBAR_HEIGHT = 64;
var DEFAULT_SCALE = 2;
function getDimensions() {
var landscape = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var dimensions = {};
if (_nukeEnv.isWeex) {
var weexEnv = typeof WXEnvironment !== 'undefined' ? WXEnvironment : {};
weexEnv.deviceHeight = parseInt(weexEnv.deviceHeight, 10);
weexEnv.deviceWidth = parseInt(weexEnv.deviceWidth, 10);
var scale = parseFloat(weexEnv.scale, 10) || DEFAULT_SCALE;
if (landscape) {
dimensions = {
window: {
width: (weexEnv.deviceHeight * 750 / weexEnv.deviceWidth).toFixed(),
scale: scale,
fontScale: 1
},
screen: {
width: weexEnv.deviceHeight,
height: weexEnv.deviceWidth
}
};
var navbarHeight = 52;
if (scale > 2) {
navbarHeight = 64;
}
dimensions.window.height = (FULL_WIDTH - navbarHeight * scale * FULL_WIDTH / weexEnv.deviceWidth).toFixed();
} else {
dimensions = {
window: {
width: FULL_WIDTH,
height: ((weexEnv.deviceHeight - NAVBAR_HEIGHT * scale) * FULL_WIDTH / weexEnv.deviceWidth).toFixed(),
scale: scale,
fontScale: 1
},
screen: {
width: weexEnv.deviceWidth,
height: weexEnv.deviceHeight
}
};
}
} else if (_nukeEnv.isWeb) {
var documentElement = document.documentElement;
dimensions = {
window: {
width: FULL_WIDTH,
height: (documentElement.clientHeight * FULL_WIDTH / documentElement.clientWidth).toFixed(),
scale: window.devicePixelRatio || DEFAULT_SCALE,
fontScale: 1
}
};
dimensions.screen = _extends({}, window.screen);
dimensions.screen.width = window.screen.width * window.devicePixelRatio;
dimensions.screen.height = window.screen.height * window.devicePixelRatio;
}
return dimensions;
}
var Dimensions = function () {
function Dimensions() {
_classCallCheck(this, Dimensions);
}
_createClass(Dimensions, null, [{
key: 'get',
value: function get(dim) {
return getDimensions()[dim];
}
}, {
key: 'getWindowInfo',
value: function getWindowInfo(landscape) {
return getDimensions(landscape)['window'];
}
}, {
key: 'getScreenInfo',
value: function getScreenInfo(landscape) {
return getDimensions(landscape)['screen'];
}
}]);
return Dimensions;
}();
exports.default = Dimensions;
module.exports = exports['default'];
;