新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
從0到1搭建一款頁面自適應(yīng)組件(Vue.js)
組件將根據(jù)屏幕比例及當(dāng)前瀏覽器窗口大小,自動(dòng)進(jìn)行縮放處理。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括寶豐網(wǎng)站建設(shè)、寶豐網(wǎng)站制作、寶豐網(wǎng)頁制作以及寶豐網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,寶豐網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到寶豐省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
- 建議在組件內(nèi)使用百分比搭配flex進(jìn)行布局,以便于在不同的分辨率下得到較為一致的展示效果。
- 使用前請(qǐng)注意將body的margin設(shè)為0,否則會(huì)引起計(jì)算誤差。
fullScreenContainer.vue
- #full-screen-container {
- position: fixed;
- top: 0px;
- left: 0px;
- overflow: hidden;
- transform-origin: left top;
- z-index: 999;
- }
autoResize.js
- export default {
- data() {
- return {
- dom: '',
- width: 0,
- height: 0,
- debounceInitWHFun: '',
- domObserver: ''
- };
- },
- methods: {
- debounce(delay, callback) {
- let lastTime;
- return function() {
- clearTimeout(lastTime);
- const [that, args] = [this, arguments];
- lastTime = setTimeout(() => {
- callback.apply(that, args);
- }, delay);
- };
- },
- observerDomResize(dom, callback) {
- const MutationObserver =
- window.MutationObserver ||
- window.WebKitMutationObserver ||
- window.MozMutationObserver;
- const observer = new MutationObserver(callback);
- observer.observe(dom, {
- attributes: true,
- attributeFilter: ['style'],
- attributeOldValue: true
- });
- return observer;
- },
- async autoResizeMixinInit() {
- const {
- initWH,
- getDebounceInitWHFun,
- bindDomResizeCallback,
- afterAutoResizeMixinInit
- } = this;
- await initWH(false);
- getDebounceInitWHFun();
- bindDomResizeCallback();
- if (typeof afterAutoResizeMixinInit === 'function')
- afterAutoResizeMixinInit();
- },
- initWH(resize = true) {
- const { $nextTick, $refs, ref, onResize } = this;
- return new Promise(resolve => {
- $nextTick(() => {
- const dom = (this.dom = $refs[ref]);
- this.width = dom ? dom.clientWidth : 0;
- this.height = dom ? dom.clientHeight : 0;
- if (!dom) {
- console.warn(
- 'DataV: Failed to get dom node, component rendering may be abnormal!'
- );
- } else if (!this.width || !this.height) {
- console.warn(
- 'DataV: Component width or height is 0px, rendering abnormality may occur!'
- );
- }
- if (typeof onResize === 'function' && resize) onResize();
- resolve();
- });
- });
- },
- getDebounceInitWHFun() {
- const { initWH } = this;
- this.debounceInitWHFun = this.debounce(100, initWH);
- },
- bindDomResizeCallback() {
- const { dom, debounceInitWHFun } = this;
- this.domObserver = this.observerDomResize(dom, debounceInitWHFun);
- window.addEventListener('resize', debounceInitWHFun);
- },
- unbindDomResizeCallback() {
- let { domObserver, debounceInitWHFun } = this;
- if (!domObserver) return;
- domObserver.disconnect();
- domObserver.takeRecords();
- domObserver = null;
- window.removeEventListener('resize', debounceInitWHFun);
- }
- },
- mounted() {
- const { autoResizeMixinInit } = this;
- autoResizeMixinInit();
- },
- beforeDestroy() {
- const { unbindDomResizeCallback } = this;
- unbindDomResizeCallback();
- }
- };
這樣,一個(gè)頁面自適應(yīng)組件就這樣搭建完成了,下面,我們將引入組件看一下效果。
- #app {
- font-family: Avenir, Helvetica, Arial, sans-serif;
- -webkit-font-smoothing: antialiased;
- -moz-osx-font-smoothing: grayscale;
- text-align: center;
- color: #2c3e50;
- margin-top: 60px;
- }
效果很好,這樣對(duì)于一些開發(fā)自適應(yīng)頁面非常容易。
新聞名稱:從0到1搭建一款頁面自適應(yīng)組件(Vue.js)
新聞來源:http://www.dlmjj.cn/article/djhjshs.html


咨詢
建站咨詢
