The strings edited by the rich text on the PC end are adapted on the mobile end

This method depends on lib-flexible, just replace the string in it directly

		 transformHtmlStr(str) {
                let vm = this;
                const ZPXRegExp = /(\d+)px/;
                let pxGlobalRegExp = new RegExp(ZPXRegExp.source, 'ig')
                if (pxGlobalRegExp.test(str)) {
                    // px转换vw,核心部分
                    let dataDpr = document.documentElement.getAttribute("data-dpr");
                    let rootSize = parseFloat(document.documentElement.style.fontSize) ;
                    let $_source = str.replace(pxGlobalRegExp, vm.createPxReplace(dataDpr,rootSize))
                    // 转换之后替换回source中,返回函数值
                    return str.replace(str, $_source)
                } else {
                    //没有就不转,直接返回
                    return source
                }
            },
            createPxReplace (dataDpr,rootSize) {
                return function ($0, $1) {
                    if (!$1) return
                    var pixels = parseFloat($1)
                    return (pixels*dataDpr/rootSize).toFixed(2)+'rem';
                }
            },

Guess you like

Origin blog.csdn.net/cuiyuchen111/article/details/98338701