在 div 中保留 jQuery 对话框

发布于 2024-11-14 21:09:16 字数 515 浏览 3 评论 0原文

我正在尝试创建许多 jQuery 对话框,但我想将它们的位置限制在父 div 内。我使用以下代码来创建它们(顺便说一句,不透明度选项也不起作用......):

var d= $('<div title="Title goes here"></div>').dialog({
            autoOpen: true,
            closeOnEscape: false,
            draggable: true,
            resizable: false,
            width: dx,
            height: dy
        });

        d.draggable('option', 'containment', 'parent');
        d.draggable('option', 'opacity', 0.45);

        $('#controlContent').append(d.parent());

I am trying to create a number of jQuery dialogs but I would like to constrain their positions to inside a parent div. I am using the following code to create them (on a side note the oppacity option is not working either...):

var d= $('<div title="Title goes here"></div>').dialog({
            autoOpen: true,
            closeOnEscape: false,
            draggable: true,
            resizable: false,
            width: dx,
            height: dy
        });

        d.draggable('option', 'containment', 'parent');
        d.draggable('option', 'opacity', 0.45);

        $('#controlContent').append(d.parent());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2024-11-21 21:09:16

上述解决方案的更有用和完整的版本。

它甚至还限制了 div 外部的大小调整!

JavaScript 有完整的注释。

// Public Domain
// Feel free to use any of the JavaScript, HTML, and CSS for any commercial or private projects. No attribution required.
// I'm not responsible if you blow anything up with this code (or anything else you could possibly do with this code).

jQuery(function($) 
{ 
    // When the document is ready run the code inside the brackets.
    $("button").button(); // Makes the button fancy (ie. jquery-ui styled)
    $("#dialog").dialog(
    { 
        // Set the settings for the jquery-ui dialog here.
        autoOpen: false, // Don't open the dialog instantly. Let an event such as a button press open it. Optional.
        position: { my: "center", at: "center", of: "#constrained_div" } // Set the position to center of the div.
    }).parent().resizable(
    { 
        // Settings that will execute when resized.
        containment: "#constrained_div" // Constrains the resizing to the div.
    }).draggable(
    { 
        // Settings that execute when the dialog is dragged. If parent isn't used the text content will have dragging enabled.
        containment: "#constrained_div", // The element the dialog is constrained to.
        opacity: 0.70 // Fancy opacity. Optional.
    });

    $("#button").click(function() 
    { 
        // When our fancy button is pressed the stuff inside the brackets will be executed.
        $("#dialog").dialog("open"); // Opens the dialog.
    });
});

http://jsfiddle.net/emilhem/rymEh/33/

A bit more helpful and complete version of above solution.

It even limits the resizing outside of the div too!

And the JavaScript is fully commented.

// Public Domain
// Feel free to use any of the JavaScript, HTML, and CSS for any commercial or private projects. No attribution required.
// I'm not responsible if you blow anything up with this code (or anything else you could possibly do with this code).

jQuery(function($) 
{ 
    // When the document is ready run the code inside the brackets.
    $("button").button(); // Makes the button fancy (ie. jquery-ui styled)
    $("#dialog").dialog(
    { 
        // Set the settings for the jquery-ui dialog here.
        autoOpen: false, // Don't open the dialog instantly. Let an event such as a button press open it. Optional.
        position: { my: "center", at: "center", of: "#constrained_div" } // Set the position to center of the div.
    }).parent().resizable(
    { 
        // Settings that will execute when resized.
        containment: "#constrained_div" // Constrains the resizing to the div.
    }).draggable(
    { 
        // Settings that execute when the dialog is dragged. If parent isn't used the text content will have dragging enabled.
        containment: "#constrained_div", // The element the dialog is constrained to.
        opacity: 0.70 // Fancy opacity. Optional.
    });

    $("#button").click(function() 
    { 
        // When our fancy button is pressed the stuff inside the brackets will be executed.
        $("#dialog").dialog("open"); // Opens the dialog.
    });
});

http://jsfiddle.net/emilhem/rymEh/33/

知足的幸福 2024-11-21 21:09:16

我找到了一种方法来做到这一点。现在这是我创建对话框的方法:

    var d = $('<div title="Title"></div>').dialog({
        autoOpen: true,
        closeOnEscape: false,
        resizable: false,
        width: 100,
        height: 100
    });

    d.parent().find('a').find('span').attr('class', 'ui-icon ui-icon-minus');
    d.parent().draggable({
        containment: '#controlContent',
        opacity: 0.70
    });

    $('#controlContent').append(d.parent());

I have found a way to do it. This is now my method for creating a dialog:

    var d = $('<div title="Title"></div>').dialog({
        autoOpen: true,
        closeOnEscape: false,
        resizable: false,
        width: 100,
        height: 100
    });

    d.parent().find('a').find('span').attr('class', 'ui-icon ui-icon-minus');
    d.parent().draggable({
        containment: '#controlContent',
        opacity: 0.70
    });

    $('#controlContent').append(d.parent());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文