1. Download dependencies
npm install lib-flexible –save
If you don’t want to download, just go to utils, create a new file flexible.js, and copy the code below
(function(win, lib) { var doc = win.document var docEl = doc.documentElement var metaEl = doc.querySelector('meta[name="viewport"]') var flexibleEl = doc.querySelector('meta[name="flexible"]') vardpr = 0 var scale = 0 var tid var flexible = lib.flexible || (lib.flexible = {}) if (metaEl) { console.warn('The scaling will be set based on the existing meta tag') var match = metaEl.getAttribute('content').match(/initial\-scale=([\d\.] + )/) if (match) { scale = parseFloat(match[1]) dpr = parseInt(1/scale) } } else if (flexibleEl) { var content = flexibleEl.getAttribute('content') if (content) { var initialDpr = content.match(/initial\-dpr=([\d\.] + )/) var maximumDpr = content.match(/maximum\-dpr=([\d\.] + )/) if (initialDpr) { dpr = parseFloat(initialDpr[1]) scale = parseFloat((1 / dpr).toFixed(2)) } if (maximumDpr) { dpr = parseFloat(maximumDpr[1]) scale = parseFloat((1 / dpr).toFixed(2)) } } } if (!dpr & amp; & amp; !scale) { var isAndroid = win.navigator.appVersion.match(/android/gi) var isIPhone = win.navigator.appVersion.match(/iphone/gi) var devicePixelRatio = win.devicePixelRatio if (isIPhone) { // Under iOS, for screens 2 and 3, use the 2x solution, and the rest use the 1x solution if (devicePixelRatio >= 3 & amp; & amp; (!dpr || dpr >= 3)) { dpr=3 } else if (devicePixelRatio >= 2 & amp; & amp; (!dpr || dpr >= 2)) { dpr=2 } else { dpr=1 } } else { // On other devices, still use the 1x solution dpr=1 } scale=1/dpr } docEl.setAttribute('data-dpr', dpr) if (!metaEl) { metaEl = doc.createElement('meta') metaEl.setAttribute('name', 'viewport') metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no') if (docEl.firstElementChild) { docEl.firstElementChild.appendChild(metaEl) } else { var wrap = doc.createElement('div') wrap.appendChild(metaEl) doc.write(wrap.innerHTML) } } function refreshRem() { var width = docEl.getBoundingClientRect().width if (width / dpr > 1920) { width = 1920 * dpr } // Divide the screen width into 24 parts, one part is 80px (that is, 1rem === 80px) var rem = width / 24 docEl.style.fontSize = rem + 'px' flexible.rem = win.rem = rem } win.addEventListener('resize', function() { clearTimeout(tid) tid = setTimeout(refreshRem, 200) }, false) win.addEventListener('pageshow', function(e) { if (e.persisted) { clearTimeout(tid) tid = setTimeout(refreshRem, 200) } }, false) if (doc.readyState === 'complete') { doc.body.style.fontSize = 12 * dpr + 'px' } else { doc.addEventListener('DOMContentLoaded', function(e) { doc.body.style.fontSize = 12 * dpr + 'px' }, false) } refreshRem() flexible.dpr = win.dpr = dpr flexible.refreshRem = refreshRem flexible.rem2px = function(d) { var val = parseFloat(d) * this.rem if (typeof d === 'string' & amp; & amp; d.match(/rem$/)) { val + = 'px' } return val } flexible.px2rem = function(d) { var val = parseFloat(d) / this.rem if (typeof d === 'string' & amp; & amp; d.match(/px$/)) { val + = 'rem' } return val } })(window, window['lib'] || (window['lib'] = {}))
2. Modify the source code
function refreshRem() { var width = docEl.getBoundingClientRect().width if (width / dpr > 1920) { width = 1920 * dpr } // Divide the screen width into 24 parts, one part is 80px (that is, 1rem === 80px) var rem = width / 24 docEl.style.fontSize = rem + 'px' flexible.rem = win.rem = rem }
3. Download the vscode plug-in (calculate equal rem values based on px)
4. Modify the default value of the plug-in
5. Introducing hand shopping tools (2 types)
1. Global adaptation
Introduce it in main.js, or introduce it in index.html
import 'lib-flexible/flexible.js'
2. Adapt some pages
6. rem for development
Adaptation notes:
1. If you use node_modules dependencies, it may not be effective after modifying the source code of the glove tool and deploying it to the formal environment
Because the node_modules package is a file ignored by git, the solution is to find the file and copy it to the utils file
Remember to change the imported files
import '@/utils/flexible'
2. Use echarts in the project to adapt visual components
Provide a simple and crude method, in the page using echarts:
mounted() { window.addEventListener('resize', () => { this.$router.go(0) // location.reload() Both methods can re-render the echarts component }) },
Or call the subcomponent (pie chart)
mounted() { window.addEventListener('resize', (event) => { this.$refs['rightLine'].resizeEcharts() this.$refs['rightPie'].resizeEcharts() this.$refs['leftLine'].resizeEcharts() this.$refs['leftPie'].resizeEcharts() }) },
methods: { resizeEcharts() { setTimeout(() => { this.myChart.resize() }, 300) } }
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Vue entry skill treeHomepageOverview 39073 people are learning the system