【问题标题】:Making DIV in an IFRAME scrollable使 IFRAME 中的 DIV 可滚动
【发布时间】:2018-02-28 21:01:00
【问题描述】:

页面 A 有一个iframe(加载页面 B)。该页面 B 有一个div#OutputDiv。我的目标是使diviframe 中可滚动。

解决方案(感谢史蒂夫!):

  1. 包括overflow: auto 用于div。但是,您也必须指定 height。只需给出任何固定值。例如height: 0
  2. 使用 javascript 函数使 div 的 height 始终与窗口相同,即使在窗口调整大小后也是如此。 height 现在不固定。

代码:

#outputDiv {
    font-size: 12px;
    font-family: Arial;
    margin-right: 1em;
    overflow: auto;
    overflow-x: hidden; (optional)
    -webkit-overflow-scrolling: touch; (enable smooth scrolling on mobile)
    height: 0; (omit-able)
}

$(window).resize(function(){
    $("#outputDiv").css("height",0).css("height",$(this).height());
});
$(window).trigger("resize");

TL;DR 全文

Page A.html - 有一个iframe 来加载Page B。在页面 A 上时,div#OutputDiv 中的 iframe 必须是可滚动的。在 PC 上运行良好,但在 iPad/Android 上无法滚动。页面结构:

页面 B.php - 左半部分 div#OutputDiv,右半部分 div#map-canvas 包含 Google 地图。

(旁注:我认为#map-canvas CSS 是不可更改的,例如更改某些内容可能会导致地图的高度超出浏览器高度,这不是我想要的。)

页面 A.html

<style type="text/css">
    #title-banner {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
    }

    #real-time-alert {
        margin-top: 155px;
        margin-left: 10px;
    }

    .tab-content {
        border-left: 1px solid #ddd;
        padding: 10px;
        height: 100%;
    }

    #map {
        height: 100%;
    }

    .nav-tabs {
        margin-bottom: 0;
    }

    #panel {
        position: fixed;
        top: 120px;
        right: 10px;
        bottom: 10px;
        left: 350px;
    }

    iframe {
        width: 100%;
        height: 100%;
    }
</style>

<body>
<div id="title-banner" class="well"><h1>Real-time incident updates</h1></div>
<div id="real-time-alert">
    DEMO:<br>
    <a id="demolink" style="cursor: pointer; font-weight: bold;">22/11/2013, 0.32.18AM: 3.128268, 101.650656<br></a>
</div>

<div id="panel">
    <ul class="nav nav-tabs" id="myTab">
        <li class="active"><a data-toggle="tab" href="#map">Map</a></li>
        <li><a data-toggle="tab" href="#message">Messages</a></li>
    </ul>

    <div class="tab-content">
        <div class="tab-pane active" id="map"><iframe seamless name="map-report"></iframe></div>
        <div class="tab-pane" id="message"></div>
    </div>
</div>
</body>

页面 B.php *对于 div#map-canvas,我必须执行下面的代码,否则当我将鼠标悬停在页面上时,div#OutputDiv 将消失。这可能并不重要。

$("*").hover(function(){
     $("#map-canvas").css("position","fixed"); });
<style>
html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }

        #map-canvas {
            height: 100%;
            width: 50%;
        }
        #content-pane {
            float:left;
            width:48%;
            padding-left: 2%;
        }
        #outputDiv {
            font-size: 12px;
            font-family: Arial;
            margin-right: 1em;
        }
</style>

<body>
    <div id="content-pane">
        <div class='well well-small' id="inputs" style="margin: 1em 1em 0 0">
            <b>TESTING ONLY</b> <br>
            <label for="originLat">Incident Site: </label><input type="text" id="originLat" style="width:6em;" />
            <input type="text" id="originLng" style="width:6em;" />
            <button type="button">Calculate distances</button>
            </br>eg. 3.126547,101.657825
        </div>
        <div id="outputDiv"></div>
    </div>
    <div id="map-canvas" style="position: fixed; right: 1px;"></div>
</body>

【问题讨论】:

    标签: ipad html mobile iframe scrollable


    【解决方案1】:

    我看不到 CSS 中指定的任何溢出控件(如果我错过了它们,我们深表歉意。

    你试过了吗:

    div#OutputDiv { overflow: auto; height: 200px; }
    

    高度仅用于测试目的 - 但您可以使用 Javascript 获取实际高度并使用原始 javascript 或 jQuery 应用它。

    可以在以下位置找到一个很好的示例(包括如何在设备从纵向变为横向或类似情况时检测方向变化):

    How do I get the new dimensions of an element *after* it resizes due to a screen orientation change?

    【讨论】:

    • 我也试过这个,但如果我没记错的话,它会延长页面 B,所以所有内容都适合而不是在不改变 iframe 大小的情况下使 iframe 内的内容可滚动......而且它使页面很长因为谷歌地图很大:
    • 对不起,我错过了一个关键点,除了溢出:自动你需要指定一个固定的高度。你可以从 Javascript 中得到它最终成为 iframe 的可用高度,但目前如果你指定一个高度为 200px - 它是否使 div 可滚动?目前正在我的手机上回复 - 稍后我回到我的办公桌时会尝试自己......
    • 史蒂夫有效!您可以发布一个新答案,以便我给您打勾吗?并且您可以包含每次浏览器更改时获取高度的javascript代码吗?因为可能会调整窗口大小或旋转移动设备?谢谢史蒂夫,我期待解决!
    • 太棒了 - 很高兴它成功了 - 我已经用完整的解决方案更新了我上面的答案,以及指向另一个 Stack Overflow 线程的链接,该线程涵盖了随着设备方向的变化等获取高度。希望这一切非常适合您。
    • 谢谢史蒂夫! Jasper 的解决方案对我来说效果不佳,因为我的情况有点不同。不知何故,由于那里的谷歌地图,如果 CSS="some value" 中的初始高度,当屏幕(尤其是移动设备)调整为 some value 时,滚动时无法到达底部的某些区域。所以(如果我说的很难理解),总是在调整到所需值之前重置 height=0,如下所示:$(window).resize(function(){ $("#outputDiv").css("height",0).css("height",$(this).height()); }); //or use .height(); $(window).trigger("resize"); //when content first loaded;
    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2018-01-18
    相关资源
    最近更新 更多