1.锁死分辨率的方式
class DevicePixelRatio {
constructor() {
// this.flag = false;
}
// 获取系统类型
_getSystem() {
let flag = false;
var agent = navigator.userAgent.toLowerCase();
// var isMac = /macintosh|mac os x/i.test(navigator.userAgent);
// if(isMac) {
// return false;
// }
// 现只针对windows处理,其它系统暂无该情况,如有,继续在此添加
if (agent.indexOf('windows') >= 0) {
return true;
}
}
// 获取页面缩放比例
// _getDevicePixelRatio() {
// let t = this;
// }
// 监听方法兼容写法
_addHandler(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + type, handler);
} else {
element['on' + type] = handler;
}
}
// 校正浏览器缩放比例
_correct() {
let t = this;
// 页面devicePixelRatio(设备像素比例)变化后,计算页面body标签zoom修改其大小,来抵消devicePixelRatio带来的变化。
document.getElementsByTagName('body')[0].style.zoom = 1 / window.devicePixelRatio;
//防止canvas变形
document.styleSheets[document.styleSheets.length - 1].insertRule('canvas {zoom: ' + 1 / (1 / window.devicePixelRatio) + '}');
document.styleSheets[document.styleSheets.length - 1].insertRule('canvas {transform: scale(' + 1 / window.devicePixelRatio + ')}');
document.styleSheets[document.styleSheets.length - 1].insertRule('canvas {transform-origin: 0 0}');
}
// 监听页面缩放
_watch() {
let t = this;
t._addHandler(window, 'resize', function() { // 注意这个方法是解决全局有两个window.resize
// 重新校正
t._correct()
})
}
// 初始化页面比例
init() {
let t = this;
if (t._getSystem()) { // 判断设备,目前只在windows系统下校正浏览器缩放比例
// 初始化页面校正浏览器缩放比例
t._correct();
// 开启监听页面缩放
t._watch();
}
}
}
export default DevicePixelRatio;
使用:
created() {
new DevicePixelRatio().init()
},
2.修改缩放比方式
export const scaleMixin = {
data() {
return {
isScree: false,
}
},
methods: {
getRatio() {
var ratio = 0,
screen = window.screen,
ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
}
else if (~ua.indexOf('msie')) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
}
else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
ratio = window.outerWidth / window.innerWidth;
}
if (ratio) {
ratio = Math.round(ratio * 100);
}
console.log(ratio)
return ratio;
},
// 计算缩放比例
getScaleRatio() {
const baseWidth = 1920; // 基准宽度
const baseHeight = 1080; // 基准高度
const screenWidth = window.innerWidth; // 屏幕宽度
const screenHeight = window.innerHeight; // 屏幕高度
console.log(screenWidth)
const ratioX = screenWidth / baseWidth;
const ratioY = screenHeight / baseHeight;
return Math.min(ratioX, ratioY); // 取最小比例作为缩放比例
},
onresize(event) {
// 利用屏幕分辨率和window对象的内高度来判断兼容IE
let winFlag = window.innerHeight === window.screen.height;
// 利用window全屏标识来判断 -- IE无效
let isFull = window.fullScreen || document.webkitIsFullScreen;
if (isFull === undefined) {
this.isScree = winFlag;
} else {
this.isScree = winFlag || isFull;
}
// true全屏 false不是全屏
if (winFlag == true) {
const scaleRatio = this.getScaleRatio();
if(this.$refs.wrapper){
this.$refs.wrapper.style.transform = `scale(${scaleRatio})`;
}
}
if (winFlag == false) {
if(this.$refs.wrapper){
this.$refs.wrapper.style.transform = 'inherit';
}
}
},
},
beforeDestroy() {
// 取消监听事件
window.removeEventListener("resize", this.onresize);
},
mounted() {
this.getRatio();
window.addEventListener("resize", this.onresize);
this.onresize()
},
};
使用:
mixins: [scaleMixin],