ResizeObserver是什么?

简介: 新来的产品经理,想做一个和qq或者微信聊天一样的,上下拖动动态改变文本内容框和编辑器布局的需求。

image.png其实一开始是一头雾水的,但是通过万能的mdn,以及充满智慧的我,最终还是完成了这个需求。


其中最核心的还是ResizeObserver这个第一次用的类,所以会在这里做一些记录。

  • ResizeObserver初识
  • ResizeObserver实战


ResizeObserver初识


  • ResizeObserver interface可以报告元素content或者border box,或者svg元素box大小的变化
  • ResizeObserver.disconnect() 取消观察某个observer的所有observed目标元素。
  • ResizeObserver.observe() 初始化观察一个指定元素。
  • ResizeObserver.observe() 取消观察一个指定元素。
  • new ResizeObserver(callback) callback的入参包括entries和observer。


entries是一个数组,它由所有的ResizeObserverEntry object组成。通过for (let entry of entries) {}的方式,entry代表一个ResizeObserver object,一个entry由contentRect和target组成。


在resize相关实践中,entry的contentRect对象是最最重要的。


{target: div, contentRect: DOMRectReadOnly}
contentRect: DOMRectReadOnly
bottom: 312.3125
height: 292.3125
left: 20
right: 626
top: 20
width: 606
x: 20
y: 20
__proto__: DOMRectReadOnly
target: div
__proto__: ResizeObserverEntry


ResizeObserver实战


Make element resizable

  • 元素应用resize css属性。
  • 元素ResizeObserver化。


<div class="main" :style="{minHeight: dynamicMainHeight}">
      <chatView></chatView>
</div>


.main {
    resize: vertical;
    overflow: auto;
}


 observeChatView() {
    if (window.ResizeObserver) {
      const viewElem = document.querySelector('.main');
      const resizeObserver = new ResizeObserver((entries) => {
        for (const entry of entries) {
          if (!this.initialHeight) {
            this.initialHeight = entry.contentRect.height;
          }
          if (this.initialHeight) {
            const deltaHeight = this.initialHeight - entry.contentRect.height;
            this.$bus.$emit('rerenderViewAndEditor', deltaHeight);
          }
        }
      });
      resizeObserver.observe(viewElem);
    } else {
      this.$Message.warning('不支持ResizeObserver');
    }
  },
},


动态计算的editor组件

<div
  class="rich-text-editor"
  contenteditable
  data-placeholder="按下Enter发送消息,按下Shift+Enter换行"
  :style="{height: dynamicHeight}"
></div>
computed: {
  dynamicHeight() {
    return `${defaultEditorHeight + this.deltaHeight}px`;
  },
},
this.$bus.$on('rerenderViewAndEditor', (deltaHeight) => {
    this.deltaHeight = deltaHeight;
});


动态计算的view组件


自动跳到最新一条消息的chatView组件需要减去deltaHeight,从而增大scrollHeight的高度。


this.$bus.$on('rerenderViewAndEditor', (deltaHeight) => {
  this.visiableHeight = document.body.clientHeight - deltaHeight;
  this.deltaHeight = deltaHeight;
});
scrollToBottom() {
  this.$nextTick(() => {
    this.scrollTop = this.scrollHeight - this.deltaHeight;
  });
},


最终效果

image.png


参考资料


https://developer.mozilla.org...

https://github.com/mdn/dom-ex...



相关文章
|
9月前
vite环境引入web worker方法
在?vite?环境中使用?web?worker?时,如果遇到生产环境中?worker.js?文件的?MIME?类型被识别为?text/html,导致报错无法运行的情况时,可以参考以下两种方法,原理都是避免编译时产出单独的?worker.js?文件。方法一worker文件不需要包装,引入时后缀增加??worker&inline,使用时直接?new?ImportedWorker();self.
487 0
element整理<el-calendar>日历组件-假期(整理)
element整理<el-calendar>日历组件-假期(整理)
|
前端开发
|
9月前
|
API
window resize和scroll事件性能优化
window resize和scroll事件性能优化
161 0
|
12月前
|
移动开发 JavaScript 小程序
微信小程序:uni-app页面Page和组件Component生命周期执行的先后顺序
微信小程序:uni-app页面Page和组件Component生命周期执行的先后顺序
331 0
|
11月前
|
JavaScript 开发者
Vite 在运行过程中是如何发现新增依赖的?
Vite 在运行过程中是如何发现新增依赖的?
183 0
|
11月前
|
存储 前端开发 JavaScript
前端封装库/工具库的编辑器之Slate.js
随着互联网时代的到来,Web应用程序的内容创作和编辑变得越来越重要。而为了更高效地进行内容编辑和管理,前端封装库/工具库的出现成为了一个非常好的解决方案。其中一个备受关注的编辑器就是Slate.js。
446 0
|
10月前
|
小程序 前端开发 JavaScript
uni-app框架看这五款组件库就够了
uni-app框架看这五款组件库就够了
2697 0
|
10月前
|
XML 前端开发 JavaScript
Webpack5 系列(二):静态资源的处理2
Webpack5 系列(二):静态资源的处理2
245 0
|
10月前
|
虚拟化
VMware Workstation安装教程(上)
VMware Workstation安装教程
159 0
http://www.vxiaotou.com