MutationObserver 实现 iframe 自适应高度

话不多说,直接上代码。

父页面 parent.tsx

tsx 复制代码
import { useRef, useEffect } from 'react'
import authResizeIframeHeight from './authResizeIframeHeight.ts'

const Parent = () => {
  const iframeRef = useRef()

  useEffect(() => {
    authResizeIframeHeight({ iframe: iframeRef.current })
  }, [])

  return (
    <div>
      <iframe
        src='child.tsx'
        ref={iframeRef}
        width='100%'
        height='100%'
        frameBorder={0}
      />
    </div>
  )
}

export default Parent

其中 authResizeIframeHeight.ts 代码如下:

ts 复制代码
/**
 * 自适应 iframe 高度
 * @param iframe 元素
 * @returns
 */
export authResizeIframeHeight = ({ iframe }) => {
  const MIN_HEIGHT = 800
  const MAX_HEIGHT = 3000

  const handleMessage = (event) => {
    const { source, origin, data } = event
    if (!data || typeof data !== 'object' || !origin || !source) {
      return
    }

    const { type, height } = data
    const originValid = `${origin}/` === 'http://dev.jd.com'
    const sourceValid = source === iframe?.contentWindow
    const typeValid = type === 'resize-height'
    const heightValid = typeof height === 'number'
    if (originValid && sourceValid && typeValid && heightValid) {
      let finalHeight = height
      if (height < MIN_HEIGHT) {
        finalHeight = MIN_HEIGHT
      } else if (height > MAX_HEIGHT) {
        finalHeight = MAX_HEIGHT
      }
      iframe.style.height = `${finalHeight}px`
    }
  }

  window.addEventListener('message', handleMessage)

  return () => {
    window.removeEventListener('message', handleMessage) // 防止内存泄漏
  }
}

子页面 child.tsx

tsx 复制代码
/**
 * 自适应 iframe 高度
 * @returns
 */
const postMessageResize = () => {
	const targetOright = 'http://dev.jd.com';
	let lastHeight = 0;
	const mo = new MutationObserver(() => {
		const currentHeight = document.body.scrollHeight;
		if (currentHeight === lastHeight) {
			return;
		}
		lastHeight = currentHeight;
		parent.postMessage(
			{
				type: 'resize-height',
				height: currentHeight
			},
			targetOright
		);
	});
	// 开始监控 body 元素的修改:
	mo.observe(document.body, {
		attributes: false,
		childList: true,
		subtree: true
	});
};

postMessageResize();

再看看 mutationObserver 的兼容性。

IE11 都支持。

PS:某些交互还需要父页面和子页面共同实现,比如从高度大的页面跳转到高度小的页面时,需要返回顶部。

相关推荐
coderYYY10 分钟前
element树结构el-tree,默认选中当前setCurrentKey无效
前端·javascript·vue.js
GISer_Jing41 分钟前
[总结篇]个人网站
前端·javascript
疯狂的沙粒1 小时前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
小妖6661 小时前
html 滚动条滚动过快会留下边框线
前端·html
heroboyluck1 小时前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_1 小时前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
琹箐2 小时前
ant-design4.xx实现数字输入框; 某些输入法数字需要连续输入两次才显示
前端·javascript·anti-design-vue
程序员-小李2 小时前
VuePress完美整合Toast消息提示
前端·javascript·vue.js
Uyker2 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder6 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架