移动端font-size自适应

直接上代码:
(function(win, doc){
function setFontSize() {
// 获取window 宽度
// zepto实现 $(window).width()就是这么干的
// var winWidth = window.innerWidth;
console.log(document);

                var winWidth = document.body.offsetWidth
                
                var designWidth = 750
                doc.documentElement.style.fontSize = (winWidth / designWidth) * 100 + 'px'
                console.log((winWidth / designWidth) * 100);
            }
            var timer = null

            win.addEventListener(
                'resize',
                function() {
                    clearTimeout(timer)

                    timer = setTimeout(setFontSize, 300)
                },
                false
            )

            win.addEventListener(
                'pageshow',
                function(e) {
                    if (e.persisted) {
                    clearTimeout(timer)

                    timer = setTimeout(setFontSize, 300)
                    }
                },
                false
            )
            setFontSize()
        })(window, document)

你可能感兴趣的:(移动端font-size自适应)