-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support hd with config.hd, Close #19
- Loading branch information
Showing
8 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function() { | ||
return { | ||
name: 'hd', | ||
onChange() { | ||
this.restart(/* why */ 'Config hd Changed'); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export default function(baseFontSize, fontscale) { | ||
const _baseFontSize = baseFontSize || 100; | ||
const _fontscale = fontscale || 1; | ||
|
||
const win = window; | ||
const doc = win.document; | ||
const ua = navigator.userAgent; | ||
const matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i); | ||
const UCversion = ua.match(/U3\/((\d+|\.){5,})/i); | ||
const isUCHd = | ||
UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80; | ||
const isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi); | ||
let dpr = win.devicePixelRatio || 1; | ||
if (!isIos && !(matches && matches[1] > 534) && !isUCHd) { | ||
// 如果非iOS, 非Android4.3以上, 非UC内核, 就不执行高清, dpr设为1; | ||
dpr = 1; | ||
} | ||
const scale = 1 / dpr; | ||
|
||
let metaEl = doc.querySelector('meta[name="viewport"]'); | ||
if (!metaEl) { | ||
metaEl = doc.createElement('meta'); | ||
metaEl.setAttribute('name', 'viewport'); | ||
doc.head.appendChild(metaEl); | ||
} | ||
metaEl.setAttribute( | ||
'content', | ||
'width=device-width,user-scalable=no,initial-scale=' + | ||
scale + | ||
',maximum-scale=' + | ||
scale + | ||
',minimum-scale=' + | ||
scale, | ||
); | ||
doc.documentElement.style.fontSize = | ||
_baseFontSize / 2 * dpr * _fontscale + 'px'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import vw from './vw.js'; | ||
import flex from './flex.js'; | ||
|
||
if (document.documentElement.clientWidth >= 750) { | ||
vw(100, 750); | ||
} else { | ||
flex(); | ||
} | ||
|
||
// hd solution for antd-mobile@2 | ||
// ref: https://mobile.ant.design/docs/react/upgrade-notes-cn#%E9%AB%98%E6%B8%85%E6%96%B9%E6%A1%88 | ||
document.documentElement.setAttribute('data-scale', true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export default function(baseFontSize, psdWidth) { | ||
const _baseFontSize = baseFontSize || 100; | ||
const _psdWidth = psdWidth || 750; | ||
|
||
const win = window; | ||
const doc = win.document; | ||
const ua = navigator.userAgent; | ||
const matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i); | ||
const UCversion = ua.match(/U3\/((\d+|\.){5,})/i); | ||
const isUCHd = | ||
UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80; | ||
const isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi); | ||
const dpr = win.devicePixelRatio || 1; | ||
const docEl = doc.documentElement; | ||
// 为了消除安卓dpr乱标的比例 | ||
let rate = 1; | ||
let scale = 1 / dpr; | ||
if (isIos) { | ||
// iOS下不用做什么 | ||
} else if ((matches && matches[1] > 534) || isUCHd) { | ||
// 有些兼容环境下, fontSize为100px的时候, 结果1rem=86px; 需要纠正viewport; | ||
docEl.style.fontSize = _baseFontSize + 'px'; | ||
const div = doc.createElement('div'); | ||
div.setAttribute('style', 'width: 1rem;display:none'); | ||
docEl.appendChild(div); | ||
const trueWidth = win.getComputedStyle(div).width; | ||
docEl.removeChild(div); | ||
// 如果1rem的真实px跟html.fontSize不符. 那么就要加一个rate缩放了; | ||
if (trueWidth !== docEl.style.fontSize) { | ||
const trueWidthVal = parseInt(trueWidth, 10); | ||
rate = _baseFontSize / trueWidthVal; | ||
scale = scale * rate; | ||
} | ||
} else { | ||
// 如果是在PC或者安卓4.3(会闪屏)以下, 则正常展现. | ||
scale = 1; | ||
} | ||
|
||
let metaEl = doc.querySelector('meta[name="viewport"]'); | ||
if (!metaEl) { | ||
metaEl = doc.createElement('meta'); | ||
metaEl.setAttribute('name', 'viewport'); | ||
doc.head.appendChild(metaEl); | ||
} | ||
metaEl.setAttribute( | ||
'content', | ||
'width=device-width,user-scalable=no,initial-scale=' + | ||
scale + | ||
',maximum-scale=' + | ||
scale + | ||
',minimum-scale=' + | ||
scale, | ||
); | ||
|
||
// width/750*100, 为了统一rem为0.01rem = 1px | ||
function setFontSize() { | ||
docEl.style.fontSize = | ||
_baseFontSize / _psdWidth * docEl.clientWidth * rate + 'px'; | ||
} | ||
setFontSize(); | ||
win.addEventListener('resize', setFontSize); | ||
} |