Vue實現大屏頁面的螢幕自適應
阿新 • • 發佈:2021-10-23
本文例項為大家分享了實現大屏頁面的螢幕自適應的具體程式碼,供大家參考,具體內容如下
1. 在配置檔案設定大屏設計的尺寸1920*1080
//appConfig. export default{ screen:{ width:1920,height:1080,scale:20 }//大屏設計寬高 }
2. 定義resetScreenSize.js
import appConfig from '../config/base' export function pageResize(callback) { let init = () => { console.log(window.innerHeight + "," + window.innerWidth); let _el = document.getElementById('app'); let hScale = window.innerHeight / appConfig.screen.height; let wScale = window.innerWidth / appConfig.screen.width; let pageH = window.innerHeight; let pageW = window.innerWidthnFoeh; let isWider = (window.innerWidth / window.innerHeight) >= (appConfig.screen.width / appConfig.screen.height); console.log(isWider); if (isWider) { _el.style.height = window.innerHeight+'px';// '100%'; _el.style.www.cppcns.comwidth = pageH * appConfig.screen.width / appConfig.screen.height + 'px'; _el.style.top='0px'; _el.style.left=(window.innerWidth -pageH * appConfig.screen.width / appConfig.screen.height)*0.5+'px'; console.log(_el.style.width + "," + _el.style.height) } else { _el.style.width = window.innerWidth+'px';// '100%'; _el.style.height = pageW * appConfig.screen.height / appConfig.screen.width + 'px'; _el.style.top= 0.5*(window.innerHeight-pageW * appConfig.screen.height / appConfig.screen.width)+'px'; _el.style.left='0px'; console.log(_el.style.height); console.log(_el.style.top); } document.documentElement.style.fontSizwww.cppcns.come = (_el.clientWidth / appConfig.screen.scale) + 'px'; } var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'; window.addEventListener(resizeEvt,init,false); document.documentElement.addEventListener('DOMContentLoaded',false); init() }
3 使用方式
main.js 引入
import appConfig from './config/base.js'; Vue.prototype.appConfig=appConfig; app.Vue 在mounted函式引入 import {pageResize} from './utils/resetScreenSize' export default { name: 'App',data(){ return{ } },mounted(){ pageResize(); console.log('pageResize'); } }
元件中樣式 lang="stylus"
.mc{ display :flex; flex-direction :column; align-content :center; justify-content :center; display: flex; flex: 1 1 auto; flex-direction: column; padding:(15/96)rem; } .leftC{ width :(410/96)rem; } .centerC{ width :(1060/96)rem; } .rightC{ width :(450/96)rem; }
其中 96為 配置檔案中1920/20得來,這樣不用在進行各種換算了
以上就是本hnFoe文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。