javascript - 多个可拖动的 iFrame,始终将 iFrame 放在首位

标签 javascript jquery html css iframe

我的页面上有 2 个 iFrame。两者都可以使用 jQuery UI 进行拖动。如果两个 iFrame 都被拖动到同一空间,则第二个加载的 iFrame 始终是主导 iFrame,即。它覆盖了第一个 iFrame。有没有办法将聚焦的 iFrame 设置为主要 iFrame,这样如果我移动第一个 iFrame 并将其放在第二个 iFrame 上,它将覆盖第二个框架,反之亦然?

HTML:

<a class = 'expandorcollapse3' href = '#'>Web Page 1</a>
<br>
<a class = 'expandorcollapse4' href = '#'>Web Page 2</a>

<div id = 'iframetest1' style = 'display: none'>
      <iframe id = 'iframe1' src = 'http://www.wsj.com'></iframe></a></div>
<div id = 'iframetest2' style = 'display: none'>
      <iframe id = 'iframe1' src = 'http://www.wsj.com'></iframe></a></div>

CSS:

#iframe1 {width: 600px; height: 500px; border; 1px solid black; zoom: 1.00; -moz-transform: scale(0.65); -moz-transform-orgin: 0 0;
         -o-transform: scale(0.65); -o-transform-origin: 0 0; -webkit-transform: scale(0.65); -webkit-transform-origin: 0 0;}

#iframetest1 {width: 390px; height: 325px; margin: 10px; border-style: solid; border-width: 10px;}    

#iframetest2 {width: 390px; height: 325px; margin: 10 px; border-style: solid; border-width: 10px;}

Javascript:

$(document).ready(function(){
    $(".expandorcollapse3").click(function(){
        if($("#iframetest1").is(":hidden")){
            $("#iframetest1").show("slow");
            }
        else{
            $("#iframetest1").hide("slow");
            }
        });
    });

$(document).ready(function(){
    $(".expandorcollapse4").click(function(){
        if($("#iframetest2").is(":hidden")){
            $("#iframetest2").show("slow");
            }
        else{
            $("#iframetest2").hide("slow");
            }
        });
    });

$("#iframetest1").draggable({containment: "document"});
$("#iframetest2").draggable({containment: "document"});

参见 jsFiddle here

最佳答案

您可以指定 stack draggable 中的选项,以便当前拖动的元素位于最前面

$("#iframetest1,#iframetest2").draggable({
    containment: "document",
    stack: 'div'
});

您还可以使用切换功能而不是 if/else 语句来缩短代码

$(document).ready(function() {
    $(".expandorcollapse3").click(function() {
        $("#iframetest1").toggle('slow');
    });
    $(".expandorcollapse4").click(function() {
        $("#iframetest2").toggle('slow');
    });
});

http://jsfiddle.net/nwu4Q/

关于javascript - 多个可拖动的 iFrame,始终将 iFrame 放在首位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960879/

相关文章:

javascript - 如何在 Spring Boot 中将参数从 Ajax 传递到 RestController

javascript - 单击时如何删除按钮中的蓝色框阴影边框

javascript - 没有 JS 的 CSS 默认选择

html - 如何确保 `select` 输入的格式适用于 Mac OS

javascript - 如何让表单中的input+label一一出现?

html - 如何将文本居中放置在图像上?

javascript - 类型 'XXX' 上不存在属性 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>'

javascript - 为什么我不能使用点语法而不是关联数组来访问表单元素 id 属性

jquery - 检索所有 id

jquery - 如果单击,如何删除输入中的蓝色框和 Bootstrap 中的按钮