简体   繁体   English

如何使父级DIV的子级DIV充当父级DIV

[英]How to make Child DIV of Parent DIV act as a Parent DIV

I have this huge problem I need a fix for. 我有这个巨大的问题需要解决。

So I have this window (parent Div) and the caption inside (child Div). 所以我有这个窗口(父Div)和其中的标题(子Div)。
And I'm using jQuery and jQuery UI to accomplish a draggable window. 我正在使用jQuery和jQuery UI完成可拖动的窗口。 But, the only way to make the entire window move is to do $('#parent').draggable(); 但是,使整个窗口移动的唯一方法是执行$('#parent').draggable(); and trying to do this on #child will break the window. 并尝试在#child上执行此操作将打破窗口。 The only draggable element then would be the caption (child div), and the window (parent div) will stay in place. 然后,唯一可拖动的元素将是标题(子div),并且窗口(父div)将保留在原位。

Showcase ( The Window Is Supposed To Be Draggable Only By The Caption Bar ): This is not supposed to be happening but it does, because function acts on parent div and not child div Showcase( 假定窗口只能由标题栏拖动 ): 这不应该发生,但是会发生,因为函数作用于父div而不作用于子div

index.php : index.php

<?php CreateWindowFn('Log into system', '<button>Log in</button>', 'sys_login_container', 'login_caption', 320, 500); ?>
// Returns: <div class="window" onload="$('#sys_login_container').draggable();" id="sys_login_container" style="width:320px !important;height:500px !important"><div class="caption" id="login_caption">Log into system</div><div class="text"><button>Log in</button></div></div>

CreateWindowFn.php : CreateWindowFn.php

function CreateWindowFn($caption, $function, $windowid, $captionid, $width, $height) {
    if (empty(null)) {
        $text = $function;
        echo "<div class=\"window\" onload=\"$('#$windowid').draggable();\" id=\"$windowid\" style=\"";
        if (!empty($width)){echo "width:$width" . "px !important;";}else{;;}
        if (!empty($height)){echo "height:$height" . "px !important";}else{;;}
        echo "\"><div class=\"caption\" id=\"$captionid\">$caption</div><div class=\"text\">$text</div></div>";
        return (bool) 1;
    } else {
        return (bool) 0;
    }
}

interface.css [ .window ]: interface.css [ .window ]:

.window {
    padding:0px;
    margin:0px;
    width:auto;
    height:auto;
    background:#fff;
    border:1px solid #000;
    color:#000;
    box-sizing:border-box;
}

.window .caption {
    background:#000;
    height:16px;
    color:#fff;
    width:100%;
    border:1px solid #fff;
    text-align:center;
    font-size:16px;
    box-sizing:border-box;
    cursor:default;
}

.window .caption::selection {
    background:none;
}

.window .text {
    background:none;
    background-color:none;
    border-top:1px solid #000;
    width:auto;
    height:auto;
    padding:-1px 2px 2px 2px;
    box-sizing:border-box;
}

I need a way to make the child div actually drag the window and itself, only holding the cursor on caption should make the window moveable. 我需要一种使子div实际拖动窗口及其本身的方法,只有将光标置于标题上才能使窗口可移动。

Thanks in advance. 提前致谢。

You need to use the jQuery UI handle option to specify what elements are allowed to drag. 您需要使用jQuery UI handle选项来指定允许拖动哪些元素。 The jQuery UI website has a page on this. jQuery UI网站上有一个页面

When you set an element to be draggable you would pass { handle: "selector" } as an argument to draggable() . 将元素设置为可拖动时,您可以将{ handle: "selector" }作为参数传递给draggable() In your case you would pass { handle: ".caption" } . 在您的情况下,您可以传递{ handle: ".caption" }

To create your window in PHP, the relevant line in your CreateWindowFn function would be changed to: 要在PHP中创建窗口,CreateWindowFn函数中的相关行将更改为:

echo "<div class=\"window\" onload=\"$('#$windowid').draggable({ handle: '.caption' });\" id=\"$windowid\" style=\"";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM