gpt4 book ai didi

javascript - 可调整大小的 div 不响应百分比高度

转载 作者:行者123 更新时间:2023-11-28 05:47:35 25 4
gpt4 key购买 nike

我在父级中有 2 个 div,其中一个具有 .ressized,但是我希望它具有以百分比表示的最大高度,我找到了能够调整 DOM 大小的代码,但我无法让它工作我,已经很接近了。

我希望可调整大小的 div 的最大高度为 70%,最小高度为 30%(最初的起点)

Added a picture for reference

fiddle

我有以下代码设置

HTML

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<body>
<div class="wrapper">
<div class="div">
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
<h4>header</h4>
</div>
<div class="resizable" id="resizable">

CSS

  .wrapper {
position: absolute;
width: 100%;
height: 100vh;
}



.div {
position: relative;
width: 100%;
height: 70%;
overflow: scroll;
background-color: #0098ff;
}

.resizable {
position: relative;
border-top: 5px solid green;
width: 100%;
height: 30%;
background-color: red;
}

JS

$("#resizable").resizable({
autoHide: true,
handles: "n",
stop: function(e, ui) {
var parent = ui.element.parent();
ui.element.css({
height: ui.element.height()/parent.height()*100+"%"
});
}
});

最佳答案

基于 @MuhammadAref 的答案来限制高度 -

经过一番尝试后,如果不将所有蓝色内容放入其自己的容器中,您将无法轻松使其工作。基本上,jQuery UI 依赖于相对定位,我们必须将其用于可调整大小的元素(或者事实证明,在某些测试中它无法调整大小)。

我有the desired effect working on this forked fiddle方法如下:

CSS

#wrapper {
position: fixed;
width: 100%;
height: 100vh;
}
.n-resizable-topcontent {
height: 70%;
background-color: #0098ff;
}
.resizable {
width: 100%;
height: 30%;
background-color: red;
}

HTML

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<body>
<div id="wrapper">
<div class="n-resizable-topcontent">
<div> some text here </div>
<div> some text here </div>
<div> some text here </div>
</div>
<div class="resizable">resizable</div>
</div>
</body>

Javascript

$(".resizable").resizable({
autoHide: true,
handles: "n",
resize: function( event, ui ) {
// parent
var parent = ui.element.parent();
var parent_height = parent.height();

// Get the min value of height or 70% of parent height
// Get the max value of height or 30% of parent height
var height = Math.min(ui.size.height, parent_height * 0.7);
height = Math.max(height, parent_height * 0.3);

// Set height for resizable element, and do not change top position.
// Instead the previous element - content container - will be adjusted.
ui.size.height = height;
ui.position.top = 0;

// make the content container's height 100% of parent, less .resizable
ui.element.prev('.n-resizable-topcontent').height( parent_height - height );
}
});

如何?

我已将#wrapper 分为两个元素,一个是您拥有的元素(可调整大小),另一个是用于容纳所有内容的新元素 - 内容容器。我给它一个类 n-ressized-topcontent 所以它很突出。

与初始代码相比的主要变化是更改新容器元素的高度,而不是使用调整大小后的元素的 top 位置。 jQuery UI 将在 n 方向调整大小时,调整 top height - 而不是向上移动元素,我们隐式地缩小了其上方的元素(容器)以及其中的任何内容。

如果您担心元素太少或嵌套元素太多 - 这通常不是问题,最好确定它是如何工作的。使用 CSS 定位并不总是容易或直接,更困难的事情之一是自动高度 - 如果不将每个元素配置为特定高度,很难告诉元素吃掉“其余”垂直空间。

我能提供的最佳建议是:保持简单。如果你需要一个特定的元素来做太多的事情,它很快就会变得不可能,并且使用看起来多余的 HTML、css、js 等来将所有内容按其应有的位置放置也没什么可耻的。

如果您有更多关于此容器中应包含什么内容的信息/您可能有的任何其他限制,我可以更新我的答案以适应。

关于javascript - 可调整大小的 div 不响应百分比高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38365447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com