【问题标题】:how to show JQuery-UI Dialog window beneath the radio button如何在单选按钮下方显示 JQuery-UI 对话框窗口
【发布时间】:2011-09-22 19:48:50
【问题描述】:

我有两个单选按钮: 单选按钮1 和单选按钮2,当我将鼠标悬停在它上面时,我显示了jquery-ui 对话框窗口......目前它显示的对话太靠右了......我想在单选按钮下方显示对话框窗口。如第二张图片所示。

   $(document).ready(function () {

            $('#div_').dialog({
                autoOpen: false,
            });  

           $(".radiobutton1").hover(
                function (){
                   $('#div_').dialog({title: "Iamge Left)"});               
                   $('#div_').removeClass("radiobutton1_1 radiobutton2").dialog('open'); 
                    var target = $(this);     
                    $("#div_").dialog("wid").position({        
                        my: 'center top',        
                        at: 'center bottom'//,        
                        //of: target     
                    });
                },
                function (){
                   $('#div_').dialog('close');
            });

            $(".radiobutton2").hover(
                function (){
                   $('#div_').dialog({title: "Images Right"});               
                   $('#div_').removeClass("radiobutton1_1 radiobutton2").addClass("radiobutton2").dialog('open'); 
                   $("#div_").dialog("wid").position({        
                        my: 'center top',        
                        at: 'center bottom'//,        
                        //of: target     
                    });
                },
                function (){
                   $('#div_').dialog('close');
            });  
    });

【问题讨论】:

标签: jquery jquery-ui


【解决方案1】:

我提琴了:http://jsfiddle.net/jensbits/dhp3d/1/

我在定位对话框中添加了这个:http://www.jensbits.com/2011/06/19/position-jquery-ui-dialog-relative-to-link/

试一试。您可能需要为窗口滚动和调整大小编写代码,以便解决问题。我只在 jsfiddle 中测试了这个。您必须在页面上运行它以查看在调整页面大小或滚动页面时定位是否正常工作。

如果你不关心resize或者scrolling,那么你可以去掉所有的window scroll和resize函数。

HTML:

<input type="radio" name="radioImage" id="radio1" class="opendialog" />Images Right
<input type="radio" name="radioImage" id="radio2" class="opendialog" />Images Left


<div id="dialog1" class="dialog">
    <p> My positioned dialog 1</p>
</div>  
<div id="dialog2" class="dialog">
    <p> My positioned dialog 2</p>
</div>  

jQuery:

function getNum(element, attrPrefix) {
    //set prefix, get number
    var prefix = attrPrefix;
    var num = element.attr("id").substring((prefix.length));
    return num;
}

 function positionDialog(base, dialog) {
    linkOffset = base.position();
    linkWidth = base.width();
    linkHeight = base.height();
    scrolltop = $(window).scrollTop();
    dialog.dialog("option", "position", [(linkOffset.left) + linkWidth / 2, linkOffset.top + linkHeight - scrolltop]);
}

$(function() {
$(".dialog").dialog({
    autoOpen: false,
    show: 'fade',
    hide: 'fade',
    modal: false,
    width: 320,
    minHeight: 180,
    buttons: {
        "Close": function() {
            $(this).dialog("close");
        }
    }
});

$(".opendialog").change(function() {
    $(".dialog").dialog("close");
    if ($(this).is(":checked")) {
        var num = getNum($(this), "radio");
        positionDialog($("#radio1"), $("#dialog" + num));
        $("#dialog" + num).dialog("open");
    }
    return false;
});

//resize and scroll function are optional - you may or may not need them
$(window).resize(function() {
    var openDialog = $(".dialog" + getNum($(".opendialog:checked")), "radio");
    positionDialog($("#radio1"), openDialog);
});

$(window).scroll(function() {
    var openDialog = $(".dialog" + getNum($(".opendialog:checked")), "radio");
    positionDialog($("#radio1"), openDialog);
});

});

【讨论】:

  • 感谢 JK 的努力,一个小的改变,即使我点击 image left 我仍然希望它一直向左对齐,就像它对 image right 所做的那样
  • 我需要哪一行代码才能让我的代码像你的一样工作(显示下面的窗口...)
  • @AbuHamzah 如果您需要知道哪些行已更改,请单击“已编辑”旁边的 SO 中的链接。这将显示答案的所有变化。
  • 感谢 +1,但你的例子有点复杂 William Niu 解决方案是我想要的,只需稍加调整即可。感谢您的回复。
【解决方案2】:

我认为你所拥有的几乎是正确的。您只需要在位置方法中使用my: 'left top',而不是my: 'center top'。另外,将.dialog("wid") 更改为.dialog("widget")。我猜后者是你这边的一个错字。

var target = $(this);
$("#div_").dialog("widget").position({
    my: 'left top',
    at: 'center bottom',        
    of: target     
});

查看实际操作:http://jsfiddle.net/william/dVZex/

【讨论】:

    【解决方案3】:

    您可以将希望对话框打开的位置的 x,y 坐标作为数组传递,即

    $( ".selector" ).dialog({ position: [300,300] });
    
    $('#div_').dialog({
         autoOpen: false,
         position: [300,300]
    });
    

    【讨论】:

    • 查看我编辑的答案,您必须尝试使用​​ x,y 坐标才能将其精确到您想要的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多