Skip to content

Commit

Permalink
fix: fix isLazy card
Browse files Browse the repository at this point in the history
  • Loading branch information
imagine10255 committed Sep 15, 2023
1 parent e2912f0 commit e36d663
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const examples: Record<EExampleCode, () => JSX.Element> = {
};

function App() {
const [exampleKey, setExampleItem] = useState<EExampleCode>(EExampleCode.lazyImage);
const [exampleKey, setExampleItem] = useState<EExampleCode>(EExampleCode.lazyCard);
const Comp = examples[exampleKey];


Expand Down
2 changes: 1 addition & 1 deletion src/BearSlideCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const BearSlideCard = ({
data-lazy-src={slide.isLazy && status !== ELoadStatus.done ? bgUrl: undefined}
style={{
...style,
backgroundImage: (!slide.isLazy && imageUrl) ? `url(${imageUrl})` :undefined,
backgroundImage: imageUrl ? `url(${imageUrl})` :undefined,
backgroundSize: bgSize,
}}
>
Expand Down
7 changes: 5 additions & 2 deletions src/hook/useLazyLoadBg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ const useLazyLoadBg = ({isLazy, imageUrl}: IUseLazyLoadProps) => {
if (entry.isIntersecting) {
const el = entry.target as HTMLDivElement;

const img = new Image();
if(status === ELoadStatus.ready){
const img = new Image();
setStatus(ELoadStatus.loading);

img.src = el.dataset.lazySrc;
img.onload = () => setStatus(ELoadStatus.done);
img.onload = () => {
setStatus(ELoadStatus.done);
setDoneImageUrl(img.src);
};
img.onerror = () => setStatus(ELoadStatus.fail);
}
observer.unobserve(el);
Expand Down
2 changes: 1 addition & 1 deletion src/hook/useLazyLoadImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const useLazyLoadImage = ({isLazy, imageUrl}: IUseLazyLoadProps) => {
for (let entry of entries) {
if (entry.isIntersecting) {
// 監視目標進入畫面
const img = entry.target as HTMLImageElement;

const img = entry.target as HTMLImageElement;
if(status === ELoadStatus.ready){
setStatus(ELoadStatus.loading);

Expand Down

0 comments on commit e36d663

Please sign in to comment.