主要是公司对前端要求不高,能解决问题就行了,前端不太熟,用js这种处理起来方便,在此记录下。
在src中创建一个api目录,新建custom.js
export const getDivHeightByClass = (className) => {let divElements = document.getElementsByClassName(className);for (let i = 0; i < divElements.length; i++) {let height = divElements[i].offsetHeight;return height;}return 0;
};export const getDivWidthByClass = (className) => {let divElements = document.getElementsByClassName(className);for (let i = 0; i < divElements.length; i++) {let height = divElements[i].offsetWidth;return height;}return 0;
};
在对应的vue中导入,然后就可以直接调用了
<template>......
</template><script>
import {getDivHeightByClass, getDivWidthByClass} from '../../api/custom.js'
export default {............mounted() {let height = getDivHeightByClass("xxx");let width = getDivWidthByClass("yyy");}
}
</script>