Vue前端項(xiàng)目自適應(yīng)布局的簡(jiǎn)單方法
一、單位尺寸
- px
- %
- vw、vh: 窗口
- em: 針對(duì)父元素的font-size
- rem:“root em”的縮寫,是一個(gè)相對(duì)長(zhǎng)度單位;rem單位作用于非根元素時(shí),相對(duì)于根元素(html)字體大小,rem單位作用于根元素字體大小時(shí),相對(duì)于其出初始字體大小。
說明:
谷歌瀏覽器,字體的默認(rèn)大小是16px;
如果父元素item1的font-size=50px,那么子元素item2的1em=50px。
如果html的頂層font-size為20px; 那么子元素item2的1rem就為20px。
二、基于rem實(shí)現(xiàn)自適應(yīng)布局
假設(shè)頁面的根元素,font-size的大小沒有設(shè)置,默認(rèn)為16px。
經(jīng)過設(shè)計(jì),我們的頁面主體部分寬為500px; 高為200px。要想轉(zhuǎn)換為自適應(yīng)布局,我們只需要將px轉(zhuǎn)換為rem就可以。
轉(zhuǎn)換后:
寬為:500/16rem
高為:200/16rem
附:html5頁面 的rem 布局適配方法
rem 布局適配方案
主要方法為:
- 按照設(shè)計(jì)稿與設(shè)備寬度的比例,動(dòng)態(tài)計(jì)算并設(shè)置 html 根標(biāo)簽的 font-size 大?。?/li>
- css 中,設(shè)計(jì)稿元素的寬、高、相對(duì)位置等取值,按照同等比例換算為 rem 為單位的值;
- 設(shè)計(jì)稿中的字體使用 px 為單位,通過媒體查詢稍作調(diào)整。
1 動(dòng)態(tài)設(shè)置 html 標(biāo)簽 font-size 大小
精簡(jiǎn)通用版本:
!(function(win, doc){ function setFontSize() { // 獲取window 寬度 // zepto實(shí)現(xiàn) $(window).width()就是這么干的 var winWidth = window.innerWidth; // doc.documentElement.style.fontSize = (winWidth / 640) * 100 + 'px' ; // 640寬度以上進(jìn)行限制 需要css進(jìn)行配合 var size = (winWidth / 640) * 100; doc.documentElement.style.fontSize = (size < 100 ? size : 100) + 'px' ; } var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize'; var timer = null; win.addEventListener(evt, 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));
高配精確版本:
(function(WIN) { var setFontSize = WIN.setFontSize = function (_width) { var docEl = document.documentElement; // 獲取當(dāng)前窗口的寬度 var width = _width || docEl.clientWidth; // docEl.getBoundingClientRect().width; // 大于 1080px 按 1080 if (width > 1080) { width = 1080; } var rem = width / 10; console.log(rem); docEl.style.fontSize = rem + 'px'; // 誤差、兼容性處理 var actualSize = win.getComputedStyle && parseFloat(win.getComputedStyle(docEl)["font-size"]); if (actualSize !== rem && actualSize > 0 && Math.abs(actualSize - rem) > 1) { var remScaled = rem * rem / actualSize; docEl.style.fontSize = remScaled + 'px'; } } var timer; function dbcRefresh() { clearTimeout(timer); timer = setTimeout(setFontSize, 100); } //窗口更新動(dòng)態(tài)改變 font-size WIN.addEventListener('resize', dbcRefresh, false); //頁面顯示時(shí)計(jì)算一次 WIN.addEventListener('pageshow', function(e) { if (e.persisted) { dbcRefresh() } }, false); setFontSize(); })(window) //對(duì)H5活動(dòng)推過頁面,寬高比例有所要求,可適當(dāng)調(diào)整 function adjustWarp(warpId = '#warp') { const $win = $(window); const height = $win.height(); let width = $win.width(); // 考慮導(dǎo)航欄情況 if (width / height < 360 / 600) { return; } width = Math.ceil(height * 360 / 640); $(warpId).css({ height, width, postion: 'relative', top: 0, left: 'auto', margin: '0 auto' }); // 重新計(jì)算 rem window.setFontSize(width); }
2 通過 CSS3媒體查詢?cè)O(shè)置 rem
簡(jiǎn)單易用 缺乏靈活度 請(qǐng)看demo 你懂的
@media screen and ( min-width: 320px){html{font-size:50px}} @media screen and ( min-width: 360px){html{font-size:56.25px}} @media screen and ( min-width: 375px){html{font-size:58.59375px}} @media screen and ( min-width: 384px){html{font-size:60px}} @media screen and ( min-width: 400px){html{font-size:62.5px}} @media screen and ( min-width: 414px){html{font-size:64.6875px}} @media screen and ( min-width: 424px){html{font-size:66.25px}} @media screen and ( min-width: 480px){html{font-size:75px}} @media screen and ( min-width: 540px){html{font-size:84.375px}} @media screen and ( min-width: 640px){html{font-size:100px}}
根據(jù)個(gè)人項(xiàng)目需求和產(chǎn)品設(shè)計(jì)可適當(dāng)修改,以上demo寫法并不唯一固定。
總結(jié)
到此這篇關(guān)于Vue前端項(xiàng)目自適應(yīng)布局的文章就介紹到這了,更多相關(guān)Vue前端自適應(yīng)布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用echarts詞云圖的實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于vue使用echarts詞云圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05vue監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽
本文主要介紹了vue監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決
這篇文章主要介紹了vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08vue自定義指令添加跟隨鼠標(biāo)光標(biāo)提示框v-tooltip方式
這篇文章主要介紹了vue自定義指令添加跟隨鼠標(biāo)光標(biāo)提示框v-tooltip方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié)篇
vue-router 是 Vue.js 官方的路由庫.這篇文章主要介紹了vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié),需要的朋友可以參考下2018-02-02vue+django實(shí)現(xiàn)一對(duì)一聊天功能的實(shí)例代碼
這篇文章主要介紹了vue+django實(shí)現(xiàn)一對(duì)一聊天功能,主要是通過websocket,由于Django不支持websocket,所以我使用了django-channels。,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-07-07