Skip to content

Commit

Permalink
fix: 修复 Image 组件开启 lazy 后在 fixed 定位容器下检查交叉异常的问题 (#820)
Browse files Browse the repository at this point in the history
* fix: 修复 `Image` 组件开启 `lazy` 后在 fixed 定位容器下检查交叉异常的问题

---------

Co-authored-by: 颜宇浩 <[email protected]>
  • Loading branch information
KMS-Bismarck and 颜宇浩 authored Nov 27, 2024
1 parent 702b1ff commit 3206bd7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sheinx",
"private": true,
"version": "3.5.2-beta.9",
"version": "3.5.2-beta.10",
"description": "A react library developed with sheinx",
"module": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
33 changes: 30 additions & 3 deletions packages/hooks/src/utils/dom/element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ export function getClosestScrollContainer(element: HTMLElement | null): HTMLElem
const style = window.getComputedStyle(el);
const overflowX = style.overflowX;
const overflowY = style.overflowY;
return (overflowX === 'auto' || overflowX === 'scroll' ||
overflowY === 'auto' || overflowY === 'scroll') &&
(el.scrollHeight > el.clientHeight || el.scrollWidth > el.clientWidth);
return (
(overflowX === 'auto' ||
overflowX === 'scroll' ||
overflowY === 'auto' ||
overflowY === 'scroll') &&
(el.scrollHeight > el.clientHeight || el.scrollWidth > el.clientWidth)
);
};

// 如果元素本身是可滚动的,直接返回
Expand All @@ -136,6 +140,29 @@ export function getClosestScrollContainer(element: HTMLElement | null): HTMLElem
return (document.scrollingElement || document.documentElement) as HTMLElement;
}

export function getClosestFixedContainer(element: Element | HTMLElement | null) {
if (!element) {
return null;
}

const isFixable = (el: HTMLElement) => {
const style = window.getComputedStyle(el);
return style.position === 'fixed';
};

// 遍历父元素
let parent = element.parentElement;

while (parent) {
if (isFixable(parent)) {
return parent;
}
parent = parent.parentElement;
}

return null;
}

export function cssSupport(attr: keyof CSSStyleDeclaration, value: string) {
if (isBrowser()) {
const element = document.createElement('div');
Expand Down
6 changes: 4 additions & 2 deletions packages/hooks/src/utils/lazyload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateUUID, docSize } from '../utils';
import { getClosestFixedContainer } from './dom/element';

type Timer = NodeJS.Timeout | null;

Expand Down Expand Up @@ -70,7 +71,8 @@ export function removeStack(id?: string | null, removeListener?: boolean) {
}

function getObserver(obj: LazyConfig, id: string) {
const { container = null, offset, render, offscreen, noRemove } = obj;
const { container = null, element, offset, render, offscreen, noRemove } = obj;
const fixedContainer = getClosestFixedContainer(element);
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((en) => {
Expand All @@ -83,7 +85,7 @@ function getObserver(obj: LazyConfig, id: string) {
});
},
{
root: container,
root: fixedContainer || container,
rootMargin: `${offset}px`,
},
);
Expand Down
7 changes: 7 additions & 0 deletions packages/shineout/src/image/__doc__/changelog.cn.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 3.5.2-beta.10
2024-10-27

### 🐞 BugFix

- 修复 `Image` 组件开启 `lazy` 后在 fixed 定位容器下检查交叉异常的问题 ([#820](https://github.com/sheinsight/shineout-next/pull/820))

## 3.4.5
2024-10-31

Expand Down

0 comments on commit 3206bd7

Please sign in to comment.