Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
京东前端
nutui-react
提交
6c5cef67
N
nutui-react
项目概览
京东前端
/
nutui-react
通知
0
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
nutui-react
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
6c5cef67
编写于
2月 03, 2023
作者:
O
oasis-cloud
提交者:
GitHub
2月 03, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: image 组件更新后无法移除 loading 状态 (#625)
上级
7cb1087a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
13 addition
and
12 deletion
+13
-12
src/packages/image/image.tsx
src/packages/image/image.tsx
+13
-12
未找到文件。
src/packages/image/image.tsx
浏览文件 @
6c5cef67
import
React
,
{
FunctionComponent
,
useState
,
useEffect
}
from
'
react
'
import
React
,
{
FunctionComponent
,
useState
,
useEffect
,
useRef
}
from
'
react
'
import
{
useConfig
}
from
'
@/packages/configprovider
'
import
Icon
from
'
@/packages/icon
'
...
...
@@ -82,9 +82,11 @@ export const Image: FunctionComponent<
}
=
{
...
defaultProps
,
...
props
}
const
[
loading
,
setLoading
]
=
useState
(
true
)
const
[
isError
,
setIsError
]
=
useState
(
false
)
const
[
_src
,
setSrc
]
=
useState
(
''
)
useEffect
(()
=>
{
if
(
src
)
{
setSrc
(
src
)
setIsError
(
false
)
setLoading
(
true
)
}
...
...
@@ -120,7 +122,7 @@ export const Image: FunctionComponent<
}
// 图片懒加载
let
observer
:
any
=
null
// 监听保存
const
observer
:
any
=
useRef
(
null
)
const
initObserver
=
()
=>
{
const
imgs
=
document
.
querySelectorAll
(
'
.nut-img.lazyload
'
)
const
options
=
{
...
...
@@ -128,7 +130,7 @@ export const Image: FunctionComponent<
rootMargin
:
'
0px
'
,
// 对视口进行收缩和扩张
}
// 监听dom是否在视口内
observer
=
new
IntersectionObserver
((
entires
,
self
)
=>
{
observer
.
current
=
new
IntersectionObserver
((
entires
,
self
)
=>
{
// entires为监听的节点数组对象
entires
.
forEach
((
item
)
=>
{
// isIntersecting是当前监听元素交叉区域是否在可视区域指定的阈值内返回的是一个布尔值
...
...
@@ -140,27 +142,26 @@ export const Image: FunctionComponent<
img
.
removeAttribute
(
'
data-src
'
)
}
// 资源加载后停止监听
observer
.
unobserve
(
item
.
target
)
observer
.
current
.
unobserve
(
item
.
target
)
},
300
)
}
})
},
options
)
imgs
.
forEach
((
item
)
=>
{
observer
.
observe
(
item
)
observer
.
current
.
observe
(
item
)
})
}
// 使用disconnect将取消的Observer实例中的所有监听
const
resetObserver
=
()
=>
{
observer
.
disconnect
&&
observer
.
disconnect
()
observer
.
current
.
disconnect
&&
observer
.
current
.
disconnect
()
}
useEffect
(()
=>
{
if
(
isLazy
)
{
initObserver
()
}
isLazy
&&
initObserver
()
return
()
=>
{
resetObserver
()
isLazy
&&
resetObserver
()
}
},
[
isLazy
])
...
...
@@ -189,7 +190,7 @@ export const Image: FunctionComponent<
<
img
className
=
"nut-img lazyload"
style
=
{
styles
}
data
-
src
=
{
src
}
data
-
src
=
{
_
src
}
alt
=
{
alt
}
loading
=
"lazy"
onLoad
=
{
load
}
...
...
@@ -199,7 +200,7 @@ export const Image: FunctionComponent<
<
img
className
=
"nut-img"
style
=
{
styles
}
src
=
{
src
}
src
=
{
_
src
}
alt
=
{
alt
}
onLoad
=
{
load
}
onError
=
{
error
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录