finance-sh / adaptive Goto Github PK
View Code? Open in Web Editor NEWThis project forked from zhangjingpu/adaptive
javascript H5自适应方案
This project forked from zhangjingpu/adaptive
javascript H5自适应方案
使用next.js等框架时,使用服务端渲染特性。项目是移动端网页。按照之前其他的react项目经验,我们会选用adaptive.js和postcss-px2rem去做高清适配,以直接按照设计稿的px值在css中还原。
由于服务端无法直接获取window(即设备的width,dpr,of course),服务端渲染出来的html与客户端渲染出来的样式,root font-size将有很大出入,导致界面跳动剧烈。
已经尝试过而失败的思路:
在node server端通过user-agent取得设备dpr: 找不到合适的方式 适配所有的物理设备型号 =》 dpr,width
使
用http-client-hints: 草案未被广泛支持
Node server like koa2 || express can't get client device info(sure), it will cause rendered dom different from client side, make html flash.
tried:
get client info by user-agent: there's no lib for all device mapping
http-client-hints: just a draft.
import adaptive from 'adaptive'
export default () => {
adaptive.desinWidth = 375
adaptive.baseFont = 14
adaptive.init()
}
Cannot read property 'newBase' of undefined
function checkRem() {
if (/android/ig.test(window.navigator.appVersion)) {
var hideDiv = document.createElement('p');
hideDiv.style.height = '1px';
hideDiv.style.width = '2.5rem';
hideDiv.style.visibility = 'hidden';
document.body.appendChild(hideDiv);
var now = hideDiv.offsetWidth;
var right = window.adaptive.newBase * 2.5; // 这一行从全局上面去读取adaptive,而通过webpack打包时全局是没有adaptive的,所以报错了,应该直接改成adaptive.newBase * 2.5;就可以吧
if (Math.abs(right / now - 1) > 0.05) {
lib.errDpr = right / now;
setRem();
}
document.body.removeChild(hideDiv);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.