Last active
October 14, 2019 16:23
-
-
Save ccwq/b0f8155ac97ef8c15f271d4aff52a1e8 to your computer and use it in GitHub Desktop.
自己的rem计算js
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
/** | |
* @param designWidth 设计稿宽度 | |
* @param scale 缩放比例(rem最小值不能小于12,如果小于12会被认为是12) | |
* @param maxWidth 大于此宽度停止计算rem | |
*/ | |
(function (designWidth, scale, maxWidth) { | |
const doc = document; | |
const win = window; | |
var docEl = doc.documentElement, | |
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', | |
recalc = function () { | |
var clientWidth = docEl.clientWidth; | |
if (!clientWidth) return; | |
if(clientWidth>=maxWidth){ | |
docEl.style.fontSize = scale* maxWidth/designWidth + 'px'; | |
}else{ | |
docEl.style.fontSize = 100 * clientWidth / designWidth + 'px'; | |
} | |
}; | |
if (!doc.addEventListener) return; | |
win.addEventListener(resizeEvt, recalc, false); | |
doc.addEventListener('DOMContentLoaded', recalc, false); | |
})( 640, 100, 640); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment