Skip to content

Commit 8e3d67e

Browse files
committed
perf: ⚡ 优化性能
1 parent c56b09f commit 8e3d67e

3 files changed

Lines changed: 23 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ test/__snapshots__
1111
test/xpi
1212
test/cookie.ts
1313
.excalidraw
14+
.kilocode

src/components/Manga/components/ComicImg.module.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,13 @@
220220
height: 100%;
221221

222222
.img {
223-
will-change: transform;
224223
width: 100%;
225224
height: auto;
226225

226+
&[data-show] {
227+
will-change: transform;
228+
}
229+
227230
& > picture {
228231
position: relative;
229232
}

src/components/Manga/components/ComicImg.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
isAbreastMode,
1414
isEnableBg,
1515
} from '../actions';
16-
import { useStyleMemo } from '../hooks/useStyle';
1716
import classes from '../index.module.css';
1817
import { store } from '../store';
1918

@@ -44,33 +43,34 @@ export const ComicImg: Component<_ComicImg & { index: number }> = (img) => {
4443
const renderClone = () =>
4544
!store.gridMode && showState() !== undefined && cloneNum() > 0;
4645

47-
const selector = `.${classes.img}[id^="_${img.index}_"]`;
48-
useStyleMemo(selector, {
49-
'grid-area': () =>
50-
isAbreastMode() && !store.gridMode ? 'none' : `_${img.index}`,
51-
'background-color': () => (isEnableBg() ? img.background : undefined),
52-
});
53-
useStyleMemo(`${selector} > picture`, {
54-
'aspect-ratio': () => `${img.size.width} / ${img.size.height}`,
55-
background: () =>
56-
img.progress &&
57-
`linear-gradient(
58-
to bottom,
59-
var(--secondary-bg) ${img.progress}%,
60-
var(--hover-bg-color,#fff3) ${img.progress}%
61-
)`,
62-
});
46+
const styles = createMemo(() => ({
47+
img: {
48+
'grid-area': isAbreastMode() && !store.gridMode ? 'none' : `_${img.index}`,
49+
'background-color': isEnableBg() ? img.background : undefined,
50+
},
51+
picture: {
52+
'aspect-ratio': `${img.size.width} / ${img.size.height}`,
53+
background: img.progress
54+
? `linear-gradient(
55+
to bottom,
56+
var(--secondary-bg) ${img.progress}%,
57+
var(--hover-bg-color,#fff3) ${img.progress}%
58+
)`
59+
: undefined,
60+
},
61+
}));
6362

6463
const _ComicImg: Component<{ cloneIndex?: number }> = (props) => (
6564
<div
6665
class={classes.img}
6766
id={`_${img.index}_${props.cloneIndex ?? 0}`}
67+
style={styles().img}
6868
data-show={showState()}
6969
data-type={img.type ?? store.defaultImgType}
7070
data-load-type={img.loadType === 'loaded' ? undefined : img.loadType}
7171
>
7272
{/* 因为 img 无法使用 ::after,所以得用 picture 包一下 */}
73-
<picture>
73+
<picture style={styles().picture}>
7474
<Show when={img.loadType !== 'wait' && src()}>
7575
<img
7676
src={src()}

0 commit comments

Comments
 (0)