@gyenno/nutui-taro
Version:
京东风格的轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)
49 lines (48 loc) • 1.16 kB
JavaScript
import Taro from "@tarojs/taro";
import { unref } from "vue";
function isWindow(val) {
return val === window;
}
const useTaroRect = (elementRef, taro) => {
let element = unref(elementRef);
return new Promise((resolve) => {
if (Taro.getEnv() === "WEB") {
if (element && element.$el) {
element = element.$el;
}
if (isWindow(element)) {
const width = element.innerWidth;
const height = element.innerHeight;
resolve({
top: 0,
left: 0,
right: width,
bottom: height,
width,
height
});
}
if (element && element.getBoundingClientRect) {
resolve(element.getBoundingClientRect());
}
resolve({
top: 0,
left: 0,
right: 0,
bottom: 0,
width: 0,
height: 0
});
} else {
const query = Taro.createSelectorQuery();
let el = element.id ? element.id : element;
query.select(`#${el}`) && query.select(`#${el}`).boundingClientRect();
query.exec(function(res) {
resolve(res[0]);
});
}
});
};
export {
useTaroRect as u
};