解决屏幕缩放大小让元素不变动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test div</title>
</head>
<style>
body,
html {
padding: 0;
margin: 0;
}
#app,
body,
html {
width: 100%;
height: 100%;
}
#app {
width: 100%;
height: 100%;
}
#data-view {
width: 100%;
height: 100%;
background-color: #030409;
color: #fff;
}
#div1 {
position: fixed;
top: 0;
left: 0;
overflow: hidden;
-webkit-transform-origin: left top;
transform-origin: left top;
z-index: 999;
background-image: url('http://datav.jiaminghi.com/demo/construction-data/img/bg.837e99ea.png');
background-size: 100% 100%;
flex-direction: column;
display: flex;
color: #fff;
}
</style>
<body>
<div id="app">
<div id="data-view">
<div id="div1">
<div style="width: 200px;height: 200px;border: 1px solid red;">
12121
</div>
</div>
</div>
</div>
</body>
</html>
<script>
console.log('window.screen',window.screen)
class myResize {
constructor(domName) {
this.dom = document.querySelector(domName);
this.allWidth = 0;
this.initConfig();
this.onResize();
}
initConfig = () => {
const { dom } = this;
const { width, height } = window.screen;
this.allWidth = width;
dom.style.width = `${width}px`;
dom.style.height = `${height}px`;
this.setAppScale();
}
setAppScale = () => {
const { allWidth, dom } = this;
const currentWidth = document.body.clientWidth;
dom.style.transform = `scale(${currentWidth / allWidth})`;
}
onResize = () => {
const {
initConfig
} = this;
window.addEventListener('resize', initConfig);
}
}
new myResize('#div1');
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现