29 lines
977 B
TypeScript
29 lines
977 B
TypeScript
![]() |
export const isInView = () => {
|
||
|
// 获取元素信息
|
||
|
const query = wx.createSelectorQuery();
|
||
|
query.select("#targetElement").boundingClientRect();
|
||
|
query.exec((res) => {
|
||
|
const targetElementInfo = res[0]; // 获取元素位置信息
|
||
|
|
||
|
// 获取屏幕可视区域信息
|
||
|
wx.getSystemInfo({
|
||
|
success: (res) => {
|
||
|
const windowHeight = res.windowHeight; // 屏幕高度
|
||
|
const windowWidth = res.windowWidth; // 屏幕宽度
|
||
|
|
||
|
// 判断元素是否在屏幕可视区域内
|
||
|
if (
|
||
|
targetElementInfo.top >= 0 && // 元素顶部在屏幕内
|
||
|
targetElementInfo.bottom <= windowHeight && // 元素底部在屏幕内
|
||
|
targetElementInfo.left >= 0 && // 元素左侧在屏幕内
|
||
|
targetElementInfo.right <= windowWidth // 元素右侧在屏幕内
|
||
|
) {
|
||
|
console.log("元素在屏幕内");
|
||
|
} else {
|
||
|
console.log("元素不在屏幕内");
|
||
|
}
|
||
|
},
|
||
|
});
|
||
|
});
|
||
|
};
|