2011-06-11 62 views
4

我想創建一些jQuery對話框,但我想限制它們的位置在父div內。我使用下面的代碼來創建它們(在一個側面說明了oppacity選項不工作要麼...):在div中保留一個jQuery對話框

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()); 
+0

被#controlContent具有位置:相對/絕對? – tbleckert 2011-06-11 10:02:58

+0

我還沒有指定,我剛剛嘗試將其設置爲相對,沒有任何更改,並將其設置爲絕對使對話框的內容窗格消失,只是保持jDialog的標題欄... – ExtremeCoder 2011-06-11 10:07:49

+0

原因設置父級位置:親戚使得孩子的上/左相對於其父,而不是文檔。 – tbleckert 2011-06-11 10:56:33

回答

0

我已經找到一種方法來做到這一點。這是現在我創建對話框的方法:

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()); 
8

上述解決方案更有幫助和完整的版本。

它甚至限制了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/

+0

一直在尋找這樣的事情幾天吧!謝謝! – andrewktmeikle 2015-01-21 15:26:01