美文网首页
vue less rem自适应

vue less rem自适应

作者: 懒懒猫 | 来源:发表于2022-08-08 18:14 被阅读0次

    如果用不了可能是版本问题,实在不行建议尝试下,以下版本
    "less": "^3.0.4", "less-loader": "^5.0.0"
    less-loader安装失败的话,后面添加--legacy-peer-deps
    npm install less-loader@5.0.0 --save --legacy-peer-deps
    flexible.js

    (function flexible (window, document) {
        var docEl = document.documentElement
        var dpr = window.devicePixelRatio || 1
    
        // adjust body font size
        function setBodyFontSize () {
            if (document.body) {
                document.body.style.fontSize = 12 * dpr + 'px'
            } else {
                document.addEventListener('DOMContentLoaded', setBodyFontSize)
            }
        }
        setBodyFontSize()
    
        // set 1rem = viewWidth / 10
        function setRemUnit () {
            // 这里默认是10等份,手动改为24,此时1rem=1920/24px即80px。(设计稿是1920px的)
            var rem = docEl.clientWidth / 24
            docEl.style.fontSize = rem + 'px'
        }
    
        setRemUnit()
    
        // reset rem unit on page resize
        window.addEventListener('resize', setRemUnit)
        window.addEventListener('pageshow', function (e) {
            if (e.persisted) {
                setRemUnit()
            }
        })
    
        // detect 0.5px supports
        if (dpr >= 2) {
            var fakeBody = document.createElement('body')
            var testElement = document.createElement('div')
            testElement.style.border = '.5px solid transparent'
            fakeBody.appendChild(testElement)
            docEl.appendChild(fakeBody)
            if (testElement.offsetHeight === 1) {
                docEl.classList.add('hairlines')
            }
            docEl.removeChild(fakeBody)
        }
    })(window, document)
    
    

    home.js

    <script>
    import '@/utils/flexible'
    ...
    </script>
    <style lang="less" scoped>
    @r: 80rem;
    ...
      height: 688 /@r; //688px
    ...
    </style>
    

    相关文章

      网友评论

          本文标题:vue less rem自适应

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