H5适配方案
一般ui设计,分为750,720,640宽,分别引入以下js
750
;(function(win, lib) {
var doc = win.document;
var docEl = doc.documentElement;
var metaEl = doc.querySelector('meta[name="viewport"]');
var flexibleEl = doc.querySelector('meta[name="flexible"]');
var dpr = 0;
var scale = 0;
var tid;
var flexible = lib.flexible || (lib.flexible = {});
if (metaEl) {
console.warn('½«¸ù¾ÝÒÑÓеÄmeta±êÇ©À´ÉèÖÃËõ·Å±ÈÀý');
var match = metaEl.getAttribute('content').match(/initial\-scale=([\d\.]+)/);
if (match) {
scale = parseFloat(match[1]);
dpr = parseInt(1 / scale);
}
} else if (flexibleEl) {
var content = flexibleEl.getAttribute('content');
if (content) {
var initialDpr = content.match(/initial\-dpr=([\d\.]+)/);
var maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/);
if (initialDpr) {
dpr = parseFloat(initialDpr[1]);
scale = parseFloat((1 / dpr).toFixed(2));
}
if (maximumDpr) {
dpr = parseFloat(maximumDpr[1]);
scale = parseFloat((1 / dpr).toFixed(2));
}
}
}
if (!dpr && !scale) {
var isAndroid = win.navigator.appVersion.match(/android/gi);
var isIPhone = win.navigator.appVersion.match(/iphone/gi);
var devicePixelRatio = win.devicePixelRatio;
if (isIPhone) {
// iOSÏ£¬¶ÔÓÚ2ºÍ3µÄÆÁ£¬ÓÃ2±¶µÄ·½°¸£¬ÆäÓàµÄÓÃ1±¶·½°¸
if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
dpr = 3;
} else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)){
dpr = 2;
} else {
dpr = 1;
}
} else {
// ÆäËûÉ豸Ï£¬ÈÔ¾ÉʹÓÃ1±¶µÄ·½°¸
dpr = 1;
}
scale = 1 / dpr;
}
docEl.setAttribute('data-dpr', dpr);
if (!metaEl) {
metaEl = doc.createElement('meta');
metaEl.setAttribute('name', 'viewport');
metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
if (docEl.firstElementChild) {
docEl.firstElementChild.appendChild(metaEl);
} else {
var wrap = doc.createElement('div');
wrap.appendChild(metaEl);
doc.write(wrap.innerHTML);
}
}
function refreshRem(){
var width = docEl.getBoundingClientRect().width;
if (width / dpr > 750) {
width = 750 * dpr;
}
var rem = width / 7.5;
docEl.style.fontSize = rem + 'px';
var realitySize = parseInt(window.getComputedStyle(document.documentElement).fontSize);
if (rem !== realitySize) {
rem = rem * rem / realitySize;
docEl.style.fontSize = rem + 'px';
}
flexible.rem = win.rem = rem;
}
win.addEventListener('resize', function() {
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}, false);
win.addEventListener('pageshow', function(e) {
if (e.persisted) {
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}
}, false);
if (doc.readyState === 'complete') {
doc.body.style.fontSize = 12 * dpr + 'px';
} else {
doc.addEventListener('DOMContentLoaded', function(e) {
doc.body.style.fontSize = 12 * dpr + 'px';
}, false);
}
refreshRem();
flexible.dpr = win.dpr = dpr;
flexible.refreshRem = refreshRem;
flexible.rem2px = function(d) {
var val = parseFloat(d) * this.rem;
if (typeof d === 'string' && d.match(/rem$/)) {
val += 'px';
}
return val;
}
flexible.px2rem = function(d) {
var val = parseFloat(d) / this.rem;
if (typeof d === 'string' && d.match(/px$/)) {
val += 'rem';
}
return val;
}
})(window, window['lib'] || (window['lib'] = {}));
;(function(win, lib) {
var doc = win.document;
var docEl = doc.documentElement;
var metaEl = doc.querySelector('meta[name="viewport"]');
var flexibleEl = doc.querySelector('meta[name="flexible"]');
var dpr = 0;
var scale = 0;
var tid;
var flexible = lib.flexible || (lib.flexible = {});
if (metaEl) {
console.warn('将根据已有的meta标签来设置缩放比例');
var match = metaEl.getAttribute('content').match(/initial\-scale=([\d\.]+)/);
if (match) {
scale = parseFloat(match[1]);
dpr = parseInt(1 / scale);
}
} else if (flexibleEl) {
var content = flexibleEl.getAttribute('content');
if (content) {
var initialDpr = content.match(/initial\-dpr=([\d\.]+)/);
var maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/);
if (initialDpr) {
dpr = parseFloat(initialDpr[1]);
scale = parseFloat((1 / dpr).toFixed(2));
}
if (maximumDpr) {
dpr = parseFloat(maximumDpr[1]);
scale = parseFloat((1 / dpr).toFixed(2));
}
}
}
if (!dpr && !scale) {
var isAndroid = win.navigator.appVersion.match(/android/gi);
var isIPhone = win.navigator.appVersion.match(/iphone/gi);
var devicePixelRatio = win.devicePixelRatio;
if (isIPhone) {
// iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
dpr = 3;
} else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)){
dpr = 2;
} else {
dpr = 1;
}
} else {
// 其他设备下,仍旧使用1倍的方案
dpr = 1;
}
scale = 1 / dpr;
}
docEl.setAttribute('data-dpr', dpr);
if (!metaEl) {
metaEl = doc.createElement('meta');
metaEl.setAttribute('name', 'viewport');
metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
if (docEl.firstElementChild) {
docEl.firstElementChild.appendChild(metaEl);
} else {
var wrap = doc.createElement('div');
wrap.appendChild(metaEl);
doc.write(wrap.innerHTML);
}
}
function refreshRem(){
var width = docEl.getBoundingClientRect().width;
if (width / dpr > 750) {
width = 750 * dpr;
}
var rem = width / 7.2;
docEl.style.fontSize = rem + 'px';
var realitySize = parseInt(window.getComputedStyle(document.documentElement).fontSize);
if (rem !== realitySize) {
rem = rem * rem / realitySize;
docEl.style.fontSize = rem + 'px';
}
flexible.rem = win.rem = rem;
}
win.addEventListener('resize', function() {
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}, false);
win.addEventListener('pageshow', function(e) {
if (e.persisted) {
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}
}, false);
if (doc.readyState === 'complete') {
doc.body.style.fontSize = 12 * dpr + 'px';
} else {
doc.addEventListener('DOMContentLoaded', function(e) {
doc.body.style.fontSize = 12 * dpr + 'px';
}, false);
}
refreshRem();
flexible.dpr = win.dpr = dpr;
flexible.refreshRem = refreshRem;
flexible.rem2px = function(d) {
var val = parseFloat(d) * this.rem;
if (typeof d === 'string' && d.match(/rem$/)) {
val += 'px';
}
return val;
}
flexible.px2rem = function(d) {
var val = parseFloat(d) / this.rem;
if (typeof d === 'string' && d.match(/px$/)) {
val += 'rem';
}
return val;
}
})(window, window['lib'] || (window['lib'] = {}));
;(function(win, lib) {
var doc = win.document;
var docEl = doc.documentElement;
var metaEl = doc.querySelector('meta[name="viewport"]');
var flexibleEl = doc.querySelector('meta[name="flexible"]');
var dpr = 0;
var scale = 0;
var tid;
var flexible = lib.flexible || (lib.flexible = {});
if (metaEl) {
console.warn('将根据已有的meta标签来设置缩放比例');
var match = metaEl.getAttribute('content').match(/initial\-scale=([\d\.]+)/);
if (match) {
scale = parseFloat(match[1]);
dpr = parseInt(1 / scale);
}
} else if (flexibleEl) {
var content = flexibleEl.getAttribute('content');
if (content) {
var initialDpr = content.match(/initial\-dpr=([\d\.]+)/);
var maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/);
if (initialDpr) {
dpr = parseFloat(initialDpr[1]);
scale = parseFloat((1 / dpr).toFixed(2));
}
if (maximumDpr) {
dpr = parseFloat(maximumDpr[1]);
scale = parseFloat((1 / dpr).toFixed(2));
}
}
}
if (!dpr && !scale) {
var isAndroid = win.navigator.appVersion.match(/android/gi);
var isIPhone = win.navigator.appVersion.match(/iphone/gi);
var devicePixelRatio = win.devicePixelRatio;
if (isIPhone) {
// iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
dpr = 3;
} else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)){
dpr = 2;
} else {
dpr = 1;
}
} else {
// 其他设备下,仍旧使用1倍的方案
dpr = 1;
}
scale = 1 / dpr;
}
docEl.setAttribute('data-dpr', dpr);
if (!metaEl) {
metaEl = doc.createElement('meta');
metaEl.setAttribute('name', 'viewport');
metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no');
if (docEl.firstElementChild) {
docEl.firstElementChild.appendChild(metaEl);
} else {
var wrap = doc.createElement('div');
wrap.appendChild(metaEl);
doc.write(wrap.innerHTML);
}
}
function refreshRem(){
var width = docEl.getBoundingClientRect().width;
if (width / dpr > 750) {
width = 750 * dpr;
}
var rem = width / 6.4;
docEl.style.fontSize = rem + 'px';
var realitySize = parseInt(window.getComputedStyle(document.documentElement).fontSize);
if (rem !== realitySize) {
rem = rem * rem / realitySize;
docEl.style.fontSize = rem + 'px';
}
flexible.rem = win.rem = rem;
}
win.addEventListener('resize', function() {
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}, false);
win.addEventListener('pageshow', function(e) {
if (e.persisted) {
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}
}, false);
if (doc.readyState === 'complete') {
doc.body.style.fontSize = 12 * dpr + 'px';
} else {
doc.addEventListener('DOMContentLoaded', function(e) {
doc.body.style.fontSize = 12 * dpr + 'px';
}, false);
}
refreshRem();
flexible.dpr = win.dpr = dpr;
flexible.refreshRem = refreshRem;
flexible.rem2px = function(d) {
var val = parseFloat(d) * this.rem;
if (typeof d === 'string' && d.match(/rem$/)) {
val += 'px';
}
return val;
}
flexible.px2rem = function(d) {
var val = parseFloat(d) / this.rem;
if (typeof d === 'string' && d.match(/px$/)) {
val += 'rem';
}
return val;
}
})(window, window['lib'] || (window['lib'] = {}));
在css中设置,以rem为单位
ui是100px,在css中使用 1rem
ui是10px,在css中使用 .1rem
ui是1px, 在css中使用 .01rem
字体的设置,需要在css中加下以下样式
[data-dpr="1"] .fz-12 {
font-size: 6PX;
}
[data-dpr="2"] .fz-12 {
font-size: 12PX;
}
[data-dpr="3"] .fz-12 {
font-size: 18PX;
}
[data-dpr="1"] .fz-14 {
font-size: 7PX;
}
[data-dpr="2"] .fz-14 {
font-size: 14PX;
}
[data-dpr="3"] .fz-14 {
font-size: 21PX;
}
[data-dpr="1"] .fz-16{
font-size: 8PX;
}
[data-dpr="2"] .fz-16 {
font-size: 16PX;
}
[data-dpr="3"] .fz-16 {
font-size: 24PX;
}
[data-dpr="1"] .fz-18{
font-size: 9PX;
}
[data-dpr="2"] .fz-18 {
font-size: 18PX;
}
[data-dpr="3"] .fz-18 {
font-size: 27PX;
}
[data-dpr="1"] .fz-20{
font-size: 10PX;
}
[data-dpr="2"] .fz-20 {
font-size: 20PX;
}
[data-dpr="3"] .fz-20 {
font-size: 30PX;
}
[data-dpr="1"] .fz-22{
font-size: 11PX;
}
[data-dpr="2"] .fz-22 {
font-size: 22PX;
}
[data-dpr="3"] .fz-22 {
font-size: 33PX;
}
[data-dpr="1"] .fz-24{
font-size: 12PX;
}
[data-dpr="2"] .fz-24 {
font-size: 24PX;
}
[data-dpr="3"] .fz-24 {
font-size: 36PX;
}
[data-dpr="1"] .fz-26{
font-size: 13PX;
}
[data-dpr="2"] .fz-26 {
font-size: 26PX;
}
[data-dpr="3"] .fz-26 {
font-size: 39PX;
}
[data-dpr="1"] .fz-28{
font-size: 14PX;
}
[data-dpr="2"] .fz-28 {
font-size: 28PX;
}
[data-dpr="3"] .fz-28 {
font-size: 42PX;
}
[data-dpr="1"] .fz-30{
font-size: 15PX;
}
[data-dpr="2"] .fz-30 {
font-size: 30PX;
}
[data-dpr="3"] .fz-30 {
font-size: 45PX;
}
[data-dpr="1"] .fz-32{
font-size: 16PX;
}
[data-dpr="2"] .fz-32 {
font-size: 32PX;
}
[data-dpr="3"] .fz-32 {
font-size: 48PX;
}
[data-dpr="1"] .fz-34{
font-size: 17PX;
}
[data-dpr="2"] .fz-34 {
font-size: 34PX;
}
[data-dpr="3"] .fz-34 {
font-size: 51PX;
}
[data-dpr="1"] .fz-36{
font-size: 18PX;
}
[data-dpr="2"] .fz-36 {
font-size: 36PX;
}
[data-dpr="3"] .fz-36 {
font-size: 54PX;
}
[data-dpr="1"] .fz-38{
font-size: 19PX;
}
[data-dpr="2"] .fz-38 {
font-size: 38PX;
}
[data-dpr="3"] .fz-38 {
font-size: 57PX;
}
[data-dpr="1"] .fz-40{
font-size: 20PX;
}
[data-dpr="2"] .fz-40 {
font-size: 40PX;
}
[data-dpr="3"] .fz-40 {
font-size: 60PX;
}
[data-dpr="1"] .fz-42{
font-size: 21PX;
}
[data-dpr="2"] .fz-42 {
font-size: 42PX;
}
[data-dpr="3"] .fz-42{
font-size: 63PX;
}
[data-dpr="1"] .fz-44{
font-size: 22PX;
}
[data-dpr="2"] .fz-44 {
font-size: 44PX;
}
[data-dpr="3"] .fz-44{
font-size: 66PX;
}
[data-dpr="1"] .fz-48{
font-size: 24PX;
}
[data-dpr="2"] .fz-48{
font-size: 48PX;
}
[data-dpr="3"] .fz-48{
font-size: 72PX;
}
[data-dpr="1"] .fz-50{
font-size: 25PX;
}
[data-dpr="2"] .fz-50 {
font-size: 50PX;
}
[data-dpr="3"] .fz-50 {
font-size: 75PX;
}
[data-dpr="1"] .fz-60{
font-size: 30PX;
}
[data-dpr="2"] .fz-60 {
font-size: 60PX;
}
[data-dpr="3"] .fz-60 {
font-size: 90PX;
}
[data-dpr="1"] .fz-80{
font-size: 40PX;
}
[data-dpr="2"] .fz-80 {
font-size: 80PX;
}
[data-dpr="3"] .fz-80 {
font-size: 120PX;
}
使用:ui字体是12px,html标签加上类名 .fz-12
如果要用px,也可以这样去用:
rem.less
.px2px(@name, @px){
@{name}: round(@px / 2) * 1;
[data-dpr="2"] & {
@{name}: @px;
}
[data-dpr="3"] & {
@{name}: round(@px / 2 * 3) * 1;
}
}
————————————————
版权声明:本文为CSDN博主「nbin斌」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/nbin2008/java/article/details/82965391