dom.js 1.3 KB
Newer Older
李星剑's avatar
李星剑 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
export default {
    data(){
        return{
            headerHeight: 0,
            footerHeight: 0
        }
    },
    methods:{
        getHeaderHeight(){
            this.$nextTick(() =>{
                this.uGetRect("#header").then(res =>{
                    this.headerHeight = res.height;
                })
            })
        },
        getFooterHeight(){
            this.$nextTick(() =>{
                this.uGetRect("#footer").then(res =>{
                    this.footerHeight = res.height;
                })
            })
        },
        uGetRect(selector, all) {
            return new Promise((resolve) => {
                uni.createSelectorQuery()
                    .in(this)[all ? 'selectAll' : 'select'](selector)
                    .boundingClientRect((rect) => {
                        if (all && Array.isArray(rect) && rect.length) {
                            resolve(rect)
                        }
                        if (!all && rect) {
                            resolve(rect)
                        }
                        if(rect === null){
                            resolve({})
                        }
                    })
                    .exec()
            })
        },
    },
    mounted() {
        this.getHeaderHeight();
        this.getFooterHeight();
    },
}