init
This commit is contained in:
28
miniprogram/utils/screen.ts
Normal file
28
miniprogram/utils/screen.ts
Normal file
@ -0,0 +1,28 @@
|
||||
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("元素不在屏幕内");
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user