美文网首页
自适应布局

自适应布局

作者: Mark同学 | 来源:发表于2019-12-03 15:26 被阅读0次

    flexible.js

    (function () {
        'use strict';
    
        // dpr->scale = 1 / dpr
        var docEl = document.documentElement,
            viewportEl = document.querySelector('meta[name="viewport"]'),
            dpr = window.devicePixelRatio || 1,
            maxWidth = 540,
            minWidth = 320;
    
        dpr = dpr >= 3 ? 3 : (dpr >= 2 ? 2 : 1);
    
        docEl.setAttribute('data-dpr', dpr);
        docEl.setAttribute('max-width', maxWidth);
        docEl.setAttribute('min-width', minWidth);
    
    
        var scale = 1 / dpr,
            content = 'width=device-width, initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no';
    
        if (viewportEl) {
            viewportEl.setAttribute('content', content);
        } else {
            viewportEl = document.createElement('meta');
            viewportEl.setAttribute('name', 'viewport');
            viewportEl.setAttribute('content', content);
            document.head.appendChild(viewportEl);
        }
    
        setRemUnit();
    
        window.addEventListener('resize', setRemUnit);
    
        function setRemUnit() {
            var ratio = 10;
            var viewWidth = docEl.getBoundingClientRect().width || window.innerWidth;
    
            // console.log(viewWidth);
    
            if (maxWidth && (viewWidth / dpr > maxWidth)) {
                viewWidth = maxWidth * dpr;
            } else if (minWidth && (viewWidth / dpr < minWidth)) {
                viewWidth = minWidth * dpr;
            }
    
            docEl.style.fontSize = viewWidth / ratio + 'px';
        }
    })();
    
    
    @function px2rem($px){
      $rem : 75px; 
      @return ($px/$rem) + rem;
    }
    

    相关文章

      网友评论

          本文标题:自适应布局

          本文链接:https://www.haomeiwen.com/subject/hricgctx.html