webcito/iframe-resizer
Composer 安装命令:
composer require webcito/iframe-resizer
包简介
A JavaScript library for dynamic iframe resizing and communication.
README 文档
README
A lightweight JavaScript library for seamless iframe communication and automatic resizing.
Features
- Automatic iframe height adjustment in the parent page
- Optional iframe width adjustment
- Two-way communication between parent and child frames
- Scroll position synchronization
- Custom event handling
- Ready-state detection
- Origin validation for parent and child messages
- Configurable logging
- Singleton pattern in a child frame
Installation
Add both scripts to your project:
<!-- In parent page --> <script src="/dist/iframe-resizer-parent.min.js"></script> <!-- In child page --> <script src="/dist/iframe-resizer-child.min.js"></script>
Demo
Open demo/index.html in a browser or serve the project directory locally:
php -S localhost:8000
Then visit http://localhost:8000/demo/.
Usage
Parent Page
Basic setup:
const resizer = new IFrameResizer('#myIframe', { targetOrigin: 'https://child-domain.com', log: true, onResize: (width, height) => { console.log(`Iframe resized to ${width}x${height}`); } }).onReady(() => { console.log('Iframe is ready!'); });
By default, the parent script applies the reported child height to the iframe element.
Use autoResize: false if you only want to receive resize events without changing iframe styles.
Resize width as well as height:
const resizer = new IFrameResizer('#myIframe', { targetOrigin: 'https://child-domain.com', resizeWidth: true });
Advanced usage with custom messages:
const resizer = new IFrameResizer('#myIframe', { targetOrigin: 'https://child-domain.com', log: true }); // Handle custom messages resizer.onMessage('customEvent', (payload) => { console.log('Custom event received:', payload); }); // Send message to child resizer.sendMessage('updateContent', {data: 'Hello Child!'});
Child Page
Basic setup:
window.IFrameResizer.create({ targetOrigin: 'https://parent-domain.com', log: true });
The child script waits until document.body is available, registers listeners, sends an initial forced resize event, and then sends a ready event.
Advanced usage with custom messages:
const resizer = window.IFrameResizer.create({ targetOrigin: 'https://parent-domain.com', log: true }); // Handle custom messages resizer.onMessage('updateContent', (payload) => { console.log('Update received:', payload); }); // Send message to parent resizer.sendMessage('customEvent', {data: 'Hello Parent!'});
Configuration Options
Parent Options
| Option | Type | Default | Description |
|---|---|---|---|
| targetOrigin | string | '*' | Allowed child origin for received and sent messages |
| autoResize | boolean | true | Automatically apply child height to the iframe |
| resizeWidth | boolean | false | Also apply child width to the iframe |
| log | boolean | false | Enable console logging |
| onResize | function | null | Callback for resize events |
| onScroll | function | null | Callback for scroll events |
Child Options
| Option | Type | Default | Description |
|---|---|---|---|
| targetOrigin | string | '*' | Allowed parent origin for received and sent messages |
| log | boolean | false | Enable console logging |
| resize | boolean | true | Enable resize tracking and resize messages |
| scroll | boolean | true | Enable scroll tracking |
API Reference
Parent Methods
onReady(callback): Register callback for iframe ready stateonMessage(type, callback): Register custom message handlersendMessage(type, data): Send a message to child iframedestroy(): Clean up event listeners
Child Methods
window.IFrameResizer.hasParent(): Check if the script is running inside an iframeonMessage(type, callback): Register custom message handlersendMessage(type, data): Send a message to parentdestroy(): Clean up instance and listeners
Events
Built-in Events
ready: Sent when child iframe is initializedresize: Triggered on size changes and once during child initializationscroll: Triggered on scroll position changes
Custom Events
You can define and handle custom events using onMessage() and sendMessage().
Security
For production use, set targetOrigin on both parent and child instead of using the default '*'.
Messages from other origins are ignored when a concrete origin is configured.
Browser Support
- All modern browsers (Chrome, Firefox, Safari, Edge)
- Requires
ResizeObserversupport for automatic resize tracking (or polyfill) - Requires
postMessagesupport
License
MIT License
Contributing
Feel free to submit issues and pull requests.
webcito/iframe-resizer 适用场景与选型建议
webcito/iframe-resizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「library」 「communication」 「iframe」 「Resizer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webcito/iframe-resizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webcito/iframe-resizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webcito/iframe-resizer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
Dispatcher to handle e-mail and instant notifications.
Package to publish/consume domain events messages from Laravel apps
Inbox pattern process implementation for your Laravel Applications
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-16