Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
2ac68d90
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6060
Star
93
Fork
166
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
2ac68d90
编写于
4月 15, 2024
作者:
shutao-dc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增 resize-observer 监听元素大小变化示例
上级
13d71508
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
124 addition
and
1 deletion
+124
-1
pages.json
pages.json
+10
-1
pages/API/resize-observer/resize-observer.test.js
pages/API/resize-observer/resize-observer.test.js
+20
-0
pages/API/resize-observer/resize-observer.uvue
pages/API/resize-observer/resize-observer.uvue
+88
-0
pages/tabBar/API.uvue
pages/tabBar/API.uvue
+6
-0
未找到文件。
pages.json
浏览文件 @
2ac68d90
...
@@ -1279,7 +1279,16 @@
...
@@ -1279,7 +1279,16 @@
"navigationBarTitleText"
:
""
,
"navigationBarTitleText"
:
""
,
"backgroundColorContent"
:
"#fffae8"
"backgroundColorContent"
:
"#fffae8"
}
}
},
//
#endif
//
#ifdef
APP-ANDROID
{
"path"
:
"pages/API/resize-observer/resize-observer"
,
"style"
:
{
"navigationBarTitleText"
:
"resize0bserver"
}
}
},
//
#endif
//
#endif
],
],
"globalStyle"
:
{
"globalStyle"
:
{
...
...
pages/API/resize-observer/resize-observer.test.js
0 → 100644
浏览文件 @
2ac68d90
describe
(
'
api-resize-observer
'
,
()
=>
{
if
(
!
process
.
env
.
uniTestPlatformInfo
.
startsWith
(
'
android
'
))
{
it
(
'
dummyTest
'
,
async
()
=>
{
expect
(
1
).
toBe
(
1
)
})
return
}
let
page
beforeAll
(
async
()
=>
{
page
=
await
program
.
reLaunch
(
'
/pages/API/resize-observer/resize-observer
'
)
await
page
.
waitFor
(
'
button
'
)
})
it
(
'
check_resize-observer
'
,
async
()
=>
{
await
page
.
waitFor
(
600
)
const
image
=
await
program
.
screenshot
();
expect
(
image
).
toSaveImageSnapshot
();
})
})
pages/API/resize-observer/resize-observer.uvue
0 → 100644
浏览文件 @
2ac68d90
<template>
<view>
<text>点击蓝色或红色方块区域会修改元素宽高</text>
<view style="align-items: center; justify-content: center; margin: 10px;">
<view style="width: 140px; height: 140px; background-color: blue; align-items: center; justify-content: center; padding: 5px;" id="outBox" @click="outBoxClick">
<view style="width: 80px; height: 80px; background-color: red; padding: 5px;" id="innerBox" @click="innerBoxClick"></view>
</view>
</view>
<button @click="revertBoxSize">还原修改前元素宽高</button>
<view>
<text class="info-text">蓝色方块元素:</text>
<view class="info-item">
<text class="info-text">{{outBoxSizeInfo}}</text>
</view>
<text class="info-text" style="margin-top: 20px;">红色方块元素:</text>
<view class="info-item">
<text class="info-text">{{innerBoxSizeInfo}}</text>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
outBoxSizeInfo: "",
innerBoxSizeInfo: "",
offset: 2,
outBoxElement: null as UniElement | null,
innerBoxElement: null as UniElement | null,
}
},
onShow() {
const resizeObserver = new UniResizeObserver((entries: Array<UniResizeObserverEntry>) => {
entries.forEach(entry =>{
if(entry.target == this.outBoxElement) {
this.outBoxSizeInfo = this.analysisResizeObserverEntry(entry)
} else if(entry.target == this.innerBoxElement) {
this.innerBoxSizeInfo = this.analysisResizeObserverEntry(entry)
}
})
})
this.outBoxElement = uni.getElementById("outBox")
resizeObserver.observe(this.outBoxElement)
this.innerBoxElement = uni.getElementById("innerBox")
resizeObserver.observe(this.innerBoxElement)
},
methods: {
innerBoxClick() {
this.innerBoxElement!.style.setProperty("width", this.innerBoxElement!.offsetWidth+this.offset)
this.innerBoxElement!.style.setProperty("height", this.innerBoxElement!.offsetWidth+this.offset)
},
outBoxClick() {
this.outBoxElement!.style.setProperty("width", this.outBoxElement!.offsetWidth+this.offset)
this.outBoxElement!.style.setProperty("height", this.outBoxElement!.offsetWidth+this.offset)
},
revertBoxSize() {
if(this.outBoxElement != null) {
this.outBoxElement!.style.setProperty("width", "140px")
this.outBoxElement!.style.setProperty("height", "140px")
}
if(this.innerBoxElement != null) {
this.innerBoxElement!.style.setProperty("width", "80px")
this.innerBoxElement!.style.setProperty("height", "80px")
}
},
analysisResizeObserverEntry(entry: UniResizeObserverEntry): string {
const contentBoxSize = entry.contentBoxSize[0]
const borderBoxSize = entry.borderBoxSize[0]
const devicePixelContentBoxSize = entry.devicePixelContentBoxSize[0]
return "borderBoxSize: \n{blockSize:"+borderBoxSize.blockSize+", inlineSize:"+borderBoxSize.inlineSize+"}\n"+
"contentBoxSize: \n{blockSize:"+contentBoxSize.blockSize+", inlineSize:"+contentBoxSize.inlineSize+"}\n"+
"devicePixelContentBoxSize: \n{blockSize:"+devicePixelContentBoxSize.blockSize+", inlineSize:"+devicePixelContentBoxSize.inlineSize+"}\n"+
"contentRect: \n{x:"+entry.contentRect.x+", y:"+entry.contentRect.y+", width:"+entry.contentRect.width+", height:"+entry.contentRect.height+"}"
}
}
}
</script>
<style>
.info-item {
flex-direction: row;
}
.info-text {
font-size: 14px;
}
</style>
pages/tabBar/API.uvue
浏览文件 @
2ac68d90
...
@@ -160,6 +160,12 @@
...
@@ -160,6 +160,12 @@
api: ["Element.takeSnapshot"]
api: ["Element.takeSnapshot"]
},
},
// #endif
// #endif
// #ifdef APP-ANDROID
{
name: 'element大小变化监听',
url: 'resize-observer'
},
// #endif
{
{
name: 'node节点',
name: 'node节点',
url: 'nodes-info',
url: 'nodes-info',
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录