本文共 728 字,大约阅读时间需要 2 分钟。
将宽度固定为750px;
通过获取屏幕宽度与750的比例关系将所有样式宽高按照相对应的比例缩放:
remChange();window.addEventListener('resize', remChange);function remChange() { remove(); let width = window.screen.width; let fixedw = 750; let scale = width / fixedw; //获取到的屏幕宽度比上自定义的750宽度 获得对应比例 let meta = document.createElement("meta"); meta.setAttribute('name', 'viewport'); //将对应比例填入meta标签即可实现宽度自适应 meta.setAttribute('content', `width=device-width, initial-scale=${scale}, maximum-scale=${scale}, minimum-scale=${scale}, user-scalable=no`); //meta将标签添加到文档中即可 document.head.appendChild(meta);}function remove() { let meta = document.querySelector("meta[name='viewport']"); if (meta != null) { document.head.removeChild(meta); }}
转载地址:http://relrb.baihongyu.com/