【问题标题】:How to Auto-Expand an IFrame in a DIV element when user clicks inside it?当用户在其中单击时,如何在 DIV 元素中自动展开 IFrame?
【发布时间】:2017-09-27 18:19:45
【问题描述】:

我在 DIV 中嵌入了一个 IFrame 源(JQuery-UI Datepicker)。当用户在 iframe 内单击以选择日期时,iframe 的宽度和高度应增加以适应 datepicker-calender 的高度。日期选择完成后,IFrame 应该恢复到其原始大小。

JSFiddle : https://jsfiddle.net/Jersey_Guy/yoc9jewv/

$(document).ready(function() {
    //debugger;
    var $w = $(window),
      $dateframe = $("iframe[src*='jqueryui']");

    $dateframe.blur(function() {
      console.log("Called Resize");
      $(this).width = 100;
      $(this).height = 100;
    }).resize();

    $dateframe.click(function() {
      console.log("Called Resize");
      $(this).width(200);
      $(this).height(400);
    }).resize();

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-zone tab-widget tabZoneSelected tabSuppressVizTooltipsAndOverlays tabZone-web fade-bg" id="tabZoneId1" style="z-index: 17; background-color: rgba(255, 255, 255, 0); width: 474px; height: 127px; top: 5px; left: 3px;">
  Outer Div
  <div class="tab-web tab-widget fade-in">
    Inner Div
    <div style="position: absolute;">
      IFrame Floater Div
      <iframe name="frame_1" src="https://jqueryui.com/resources/demos/datepicker/default.html" style="border: 0px none; width: 200px; height: 80px; top: 0px; left: 0px;">

      </iframe>
      End IFrame Div
    </div>
    End Inner Div
  </div>
  End Outer Div
</div>

我需要设置 div 大小和 IFrame 大小吗? 我哪里错了?

【问题讨论】:

  • 是 iFrame 跨域吗?
  • 实际情况并非如此,但该示例确实使用了不同来源的html作为示例。

标签: javascript jquery html iframe jquery-ui-datepicker


【解决方案1】:

我认为您可以通过处理 div 而不是 iframe 来做到这一点。 我在这里修改了你的小提琴:https://jsfiddle.net/bkelley13/faLeucpp/1/

 var dateframe = $("#myframediv");

  $(document).click(function() {            
    dateframe.width(100);
    dateframe.height(50);
    dateframe.resize();
});

dateframe.click(function(ev) {    
  $(this).width(400);
  $(this).height(200);
  ev.stopPropagation();
}).resize();

    <div id="myframediv" style="background-color:yellow">
  IFrame Floater Div
  <iframe name="frame_1" src="https://jqueryui.com/resources/demos/datepicker/default.html">
  </iframe>

因此,如果您在 iframe div 中单击,它会调整大小,如果您在其外部单击,它会调整大小。 (我不知道目标 jqueryui 页面,datepicker 似乎没有冒泡点击事件。)

【讨论】:

  • 好主意!你让我想到了正确的道路。玩了一下之后,我意识到点击和模糊功能不会触发 div 或 iframe - 使用 mouseleave 和 mouseenter 和 jquery animate 来解决它。请参阅我更新的答案。我不必触摸 div。谢谢! – Jersey_Guy 7 分钟前编辑
  • 酷。我注意到模糊在 iframe 或 div 上不起作用,尽管我确实看到 click 在 div 上起作用。
【解决方案2】:

解决了!未触发的点击和模糊功能是问题所在。必须使用 mouseenter 和 mouseleave 以及 jquery animate 函数来使 iframe 扩展和收缩。

这是我的代码:https://jsfiddle.net/Jersey_Guy/4zja94j4/

$(document).ready(function() {
  //debugger;
  var $w = $(window),
    $dateIframe = $("iframe[src*='jqueryui']"),
    $dateInnerDiv = $("#myframediv");

  $dateIframe.mouseleave(function() {
    console.log("Called Iframe Reduce");
    $(this).animate({
      width: "190px",
      height: "90px"
    }, 500);
    //Optional: slow down the exit to 500 ms in case user changes their mind
  });

  $dateIframe.mouseenter(function() {
    console.log("Called Iframe increase");
    $(this).animate({
      width: "390px",
      height: "290px"
    }, 0);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-zone tab-widget tabZoneSelected tabSuppressVizTooltipsAndOverlays tabZone-web fade-bg" id="tabZoneId1" style="z-index: 17; background-color:white; width: 500px; height: 300px; top: 5px; left: 3px;">
  Outer Div
  <div class="tab-web tab-widget fade-in" style="background-color:gray;">
    Inner Div
    <div id="myframediv" style="position: absolute; background-color:yellow;">
      IFrame Floater Div <a href="before">before</a>
      <iframe name="frame_1" src="https://jqueryui.com/resources/demos/datepicker/default.html" style="border: 0px none; width: 190px; height: 90px; top: 0px; left: 0px;  background-color:orange;">

      </iframe> End IFrame Div <a href="after">after</a>
    </div>
    End Inner Div
  </div>
  End Outer Div
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多