标签:
1.最先接触的方法是根据不同的设备也设置html的font-size
html { font-size: 16px; } @media screen and (min-width: 600px) { html { font-size: 18px; } } @media screen and (min-width: 1000px) { html { font-size: 22px; } }
2.第二种是最近了解到的vw,配合CSS3 calc
计算实现动态字体大小效果。其实本来第二种想写js方法,但是有代码就放最后吧
例如,我们希望浏览器宽度在600px~1000px变化的时候,html根元素的font-size
大小是18px~22px之间对应变化的,则可以:
html { /* iPhone6的375px尺寸作为16px基准,600px正好18px大小 */ font-size: calc(100% + 2 * (100vw - 375px) / 225); } @media screen and (min-width: 600px) { html { /* 600px-1000px每100像素宽字体增加1px(18px-22px) */ font-size: calc(112.5% + 4 * (100vw - 600px) / 400); } } @media screen and (min-width: 1000px) { html { /* 1000px往后是每100像素0.5px增加 */ font-size: calc(137.5% + 5 * (100vw - 1000px) / 1000); } }
3.第三个就是方法:js。依赖淘宝团队出的lib-flexible
<meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
iniital-dpr是把dpr强制设定为给定的值,而maximum-dpr则是给出一个最大的dpr限制,然后对其和系统的dpr做一个比较。
(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 isOS93 = isIPhone && win.navigator.appVersion.match(/OS 9_3/); var devicePixelRatio = win.devicePixelRatio; if (isIPhone && !isOS93) { // 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; var height = docEl.getBoundingClientRect().height; //if (width / dpr > 540) { //width = 540 * dpr; //} var rem = width / 10; if(height < width) { rem = height /10; } 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‘] = {}));
参考:http://www.zhangxinxu.com/wordpress/2016/08/vw-viewport-responsive-layout-typography/
标签:
原文地址:http://www.cnblogs.com/myzy/p/5900175.html