让 chatgpt 帮你实现一个可以拖动调节高度的 iframe

背景

需要将一个页面以 iframe 的方式嵌入页面, 而且还需要能够拖动iframe的上边缘动态调整 iframe 的高度。 大概长下面这个样子。

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Draggable Iframe</title>
    <style>
        body, html {
            height: 100%;
            margin: 0;
        }

        .iframe-container {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            width: 100%;
            height: 300px;
        }

        iframe {
            width: 100%;
            height: 100%;
            border: none;
        }

        .drag-handle {
            position: absolute;
            top: -10px;
            left: 0;
            right: 0;
            height: 20px;
            background-color: transparent;
            cursor: ns-resize;
            user-select: none;
        }
    </style>
</head>
<body>
    <div class="iframe-container">
        <iframe src="https://example.com"></iframe>
        <div class="drag-handle"></div>
    </div>
</body>
</html>

js

js 复制代码
const dragHandle = document.querySelector('.drag-handle');
        const iframeContainer = document.querySelector('.iframe-container');
        let isDragging = false;
        let startY = 0;

        dragHandle.addEventListener('mousedown', (event) => {
            isDragging = true;
            startY = event.clientY;
            event.preventDefault();
        });

        document.addEventListener('mousemove', (event) => {
            if (!isDragging) return;
            const deltaY = startY - event.clientY;
            const newHeight = iframeContainer.offsetHeight + deltaY;
            iframeContainer.style.height = `${newHeight}px`;
            startY = event.clientY;
        });

        document.addEventListener('mouseup', () => {
            isDragging = false;
        });

这样实现之后,发现拖动 iframe 的上边缘往上调节高度没有问题, 当往下的时候,发现高度无法调整。 看了下是因为往下的时候, 鼠标已经滑动到 iframe 的区域了。此时上面的监听的 mousemove 事件已经不再触发,业绩UI无法正常调节高度了。

修改下代码, 新增一个 mask div, 用来保证鼠标的 mousemove 事件可以持续触发。

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Draggable Iframe</title>
    <style>
        body, html {
            height: 100%;
            margin: 0;
        }

        .iframe-container {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            width: 100%;
            height: 300px;
        }

        iframe {
            width: 100%;
            height: 100%;
            border: none;
        }

        .drag-handle {
            position: absolute;
            top: -10px;
            left: 0;
            right: 0;
            height: 20px;
            background-color: transparent;
            cursor: ns-resize;
            user-select: none;
        }

        .iframe-mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: transparent;
        }
    </style>
</head>
<body>
    <div class="iframe-container">
        <iframe src="https://example.com"></iframe>
        <div class="drag-handle"></div>
        <div class="iframe-mask"></div>
    </div>

</body>
</html>

js

js 复制代码
    const dragHandle = document.querySelector('.drag-handle');
    const iframeContainer = document.querySelector('.iframe-container');
    const iframeMask = document.querySelector('.iframe-mask');
    let isDragging = false;
    let startY = 0;

    dragHandle.addEventListener('mousedown', (event) => {
        isDragging = true;
        startY = event.clientY;
        iframeMask.style.display = 'block'; // 显示遮罩层
        event.preventDefault();
    });

    document.addEventListener('mousemove', (event) => {
        if (!isDragging) return;
        const deltaY = startY - event.clientY;
        const newHeight = iframeContainer.offsetHeight + deltaY;
        iframeContainer.style.height = `${newHeight}px`;
        startY = event.clientY;
    });

    document.addEventListener('mouseup', () => {
        isDragging = false;
        iframeMask.style.display = 'none'; // 隐藏遮罩层
    });

这样之后, 就实现了一个简单的拖动调整 iframe 的功能。

相关推荐
Kakarotto2 分钟前
Canvas 直线点击事件处理优化
javascript·vue.js·canvas
顺遂2 分钟前
基于Rokid CXR-M SDK的引导式作业辅导系统设计与实现
前端
代码搬运媛2 分钟前
Generator 迭代器协议 & co 库底层原理+实战
前端
前端拿破轮4 分钟前
从0到1搭建个人网站(三):用 Cloudflare R2 + PicGo 搭建高速图床
前端·后端·面试
功能啥都不会7 分钟前
PM2 使用指南 - 踩坑记录
前端
HelloReader9 分钟前
React 中 useState、useEffect、useRef 的区别与使用场景详解,终于有人讲明白了
前端
兆子龙10 分钟前
CSS 里的「if」:@media、@supports 与即将到来的 @when/@else
前端
踩着两条虫10 分钟前
AI 智能体如何重构开发工作流
前端·人工智能·低代码
代码老中医31 分钟前
逃离"Div汤":2026年,当AI写了75%的代码,前端开发者还剩什么?
前端
进击的尘埃32 分钟前
Playwright Component Testing 拆到底:组件怎么挂上去的,快照怎么在 CI 里不翻车
javascript