web前端学习之从0到1搭建一款页面自适应组件(Vue.js)
小职 2021-02-23 来源 :前端历劫之路 阅读 1570 评论 0

摘要:本文主要介绍了web前端学习之从0到1搭建一款页面自适应组件(Vue.js),通过具体的内容向大家展现,希望对大家对Web前端的学习有所帮助。

本文主要介绍了web前端学习之从0到1搭建一款页面自适应组件(Vue.js),通过具体的内容向大家展现,希望对大家对Web前端的学习有所帮助。

web前端学习之从0到1搭建一款页面自适应组件(Vue.js)

组件将根据屏幕比例及当前浏览器窗口大小,自动进行缩放处理。

 

建议在组件内使用百分比搭配flex进行布局,以便于在不同的分辨率下得到较为一致的展示效果。

使用前请注意将body的margin设为0,否则会引起计算误差。

fullScreenContainer.vue

 

<template>

  <div id="full-screen-container" :ref="ref">

    <template v-if="ready">

      <slot></slot>

    </template>

  </div>

</template>

 

<script>

import autoResize from './autoResize.js'

export default {

  name: 'DvFullScreenContainer',

  mixins: [autoResize],

  data () {

    return {

      ref: 'full-screen-container',

      allWidth: 0,

      scale: 0,

      datavRoot: '',

      ready: false

    }

  },

  methods: {

    afterAutoResizeMixinInit () {

      const { initConfig, setAppScale } = this

      initConfig()

      setAppScale()

      this.ready = true

    },

    initConfig () {

      const { dom } = this

      const { width, height } = screen

      this.allWidth = width

      dom.style.width = `${width}px`

      dom.style.height = `${height}px`

    },

    setAppScale () {

      const { allWidth, dom } = this

      const currentWidth = document.body.clientWidth

      dom.style.transform = `scale(${currentWidth / allWidth})`

    },

    onResize () {

      const { setAppScale } = this

      setAppScale()

    }

  }

}

</script>

 

<style>

#full-screen-container {

  position: fixed;

  top: 0px;

  left: 0px;

  overflow: hidden;

  transform-origin: left top;

  z-index: 999;

}

</style>

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();

    }

};

这样,一个页面自适应组件就这样搭建完成了,下面,我们将引入组件看一下效果。

 

<template>

  <div id="app">

    <fullScreenContainer>

      <img alt="Vue logo" src="./assets/logo.png" />

      <HelloWorld msg="Welcome to Your Vue.js App" />

    </fullScreenContainer>

  </div>

</template>

 

<script>

import HelloWorld from "./components/HelloWorld.vue";

import fullScreenContainer from "./components/fullScreenContainer/fullScreenContainer.vue";

export default {

  name: "App",

  components: {

    HelloWorld,

    fullScreenContainer,

  },

};

</script>

 

<style>

#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;

}

</style>

 web前端学习之从0到1搭建一款页面自适应组件(Vue.js)

 

效果很好,这样对于一些开发自适应页面非常容易。


我是小职,记得找我

✅ 解锁高薪工作

✅ 免费获取最新技术干货教程资源

web前端学习之从0到1搭建一款页面自适应组件(Vue.js)

本文由 @小职 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程