gpt4 book ai didi

css - 如何使 PRE 扩展到列大小而不增加新数据

转载 作者:行者123 更新时间:2023-11-27 23:19:33 25 4
gpt4 key购买 nike

我有一张卡片 (1),其中包含另外两张使用 Bootstrap 列的卡片(2 和 3)。卡号 2 有一些选择和其他元素,并根据屏幕改变其高度。卡号 3 有一个 pre 元素和一些按钮。我想要实现的是使两列的高度相同,每列包含一张卡片。 pre 元素将动态接收新数据,所以我希望它有滚动条以保持数据可读。我能够通过为卡片指定高度来做到这一点:

height: 10rem; overflow: auto; display: flex; flex-direction: column-reverse;

但是我需要它根据卡片 1 的大小进行扩展,如果我将高度设置为 100%,它就可以工作。所以我希望它像高度设置为 100% 时那样显示,但像设置高度时那样工作到一个特定的值。这可能吗?

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<div class="mt-1" id="comcard">

<div class="card">
<div class="card-header" id="headingOne">
<button class="btn" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<i class="fas fa-exchange-alt"></i> CARD
</button>
</div>

<div id="collapseOne" class="collapse hide" aria-labelledby="headingOne" data-parent="#comcard">
<div class="card-body">
<div class="row">
<div class="col-sm col-md-5 col-lg-4 col-xl-3">
<div class="card h-100">
<div class="card-header">
CARD 2
</div>
<div class="card-body">

<form>
<div class="form-group row">
<label for="Select1" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select1">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

<form>
<div class="form-group row">
<label for="Select2" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select2">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

<form>
<div class="form-group row">
<label for="Select3" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select3">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

<form>
<div class="form-group row">
<label for="Select4" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select4">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

</div>
</div>
</div>

<div class="col-sm col-md-7 col-lg-8 col-xl-9">
<div class="card h-100">
<div class="card-header"> CARD 3 </div>
<div class="card-body d-flex flex-column">
<div class="card" style="height: 100%; overflow: auto; display: flex; flex-direction: column-reverse;">
<div class="card-body" id="terminal_div">
<pre id="terminal_pre"></pre>
</div>
</div>

<fieldset id="group_io">
<div class="input-group mt-2">
<select class="custom-select col-sm-2" id="io_select">
<option value="0" selected>1</option>
<option value="1">2</option>
</select>
<input type="text" class="form-control">
<div class="input-group-append">
<button class="btn btn-outline-success" type="button">Send</button>
</div>

<button type="button" class="btn btn-secondary ml-2">Clear</button>
</div>
</fieldset>
</div>
</div>
</div>

</div>

</div>
</div>
</div>

</div>

高度设置为 100% 的 Pre(随着新数据不断扩展):

https://jsfiddle.net/Fperola/kL17ewpv/17/

Pre 高度设置为一个值(例如 10rem)(根据需要添加滚动条但不响应不同的屏幕):

https://jsfiddle.net/Fperola/kL17ewpv/16/

最佳答案

我认为纯 CSS 是不可能的,必须放入 jQuery...我做了一些事情:

  • 分配 id card2 & card3 和一个类 ourClass 让事情变得简单
  • 将内联样式更改为 overflow-y: scroll;显示: flex ; flex 方向:列反转;
  • 添加了一些 jQuery 来读取 #card2 的结果并相应地设置 #card3
下面的

工作代码片段(请在整个页面中运行它以查看效果):

setInterval(function() {
$('#terminal_pre').append("123");
$('#terminal_pre').append("&#10;");
}, 1000);

function setRelevantHeight() {
var newHeight = $("#card2").height();
$('#heightHere').text($("#card2").height());
$("#card3").height(newHeight);
var newAdjustedHeight = newHeight - 175;
//console.log("newAdjustedHeight:",newAdjustedHeight);
$(".ourClass").height(newAdjustedHeight);

}

$(document).ready(function() {
setRelevantHeight();
})

$(window).resize(function() {
setRelevantHeight()
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<div class="mt-1" id="comcard">

<div class="card">
<div class="card-header" id="headingOne">
<button class="btn" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<i class="fas fa-exchange-alt"></i> CARD
</button>
</div>

<div id="collapseOne" class="collapse hide" aria-labelledby="headingOne" data-parent="#comcard">
<div class="card-body">
<div class="row">
<div id='card2' class="col-sm col-md-5 col-lg-4 col-xl-3">
<div class="card h-100">
<div class="card-header">
CARD 2
</div>
<div class="card-body">

<form>
<div class="form-group row">
<label for="Select1" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select1">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

<form>
<div class="form-group row">
<label for="Select2" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select2">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

<form>
<div class="form-group row">
<label for="Select3" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select3">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

<form>
<div class="form-group row">
<label for="Select4" class="col-sm-5 col-form-label"> ABCDEFGHIJKL: </label>
<div class="col-sm-7">
<select class="form-control" id="Select4">
<option selected value="0">1</option>
<option value="1">2</option>
</select>
</div>
</div>
</form>

</div>
</div>
</div>

<div id='card3' class="col-sm col-md-7 col-lg-8 col-xl-9">
<div class="card h-100">
<div class="card-header"> CARD 3 </div>
<div class="card-body d-flex flex-column">
<div class="card ourClass" style=" overflow-y: scroll; display: flex; flex-direction: column-reverse;">
<div class="card-body" id="terminal_div">
<pre id="terminal_pre"></pre>
</div>
</div>

<fieldset id="group_io">
<div class="input-group mt-2">
<select class="custom-select col-sm-2" id="io_select">
<option value="0" selected>1</option>
<option value="1">2</option>
</select>
<input type="text" class="form-control">
<div class="input-group-append">
<button class="btn btn-outline-success" type="button">Send</button>
</div>

<button type="button" class="btn btn-secondary ml-2">Clear</button>
</div>
</fieldset>
</div>
</div>
</div>

</div>

</div>
</div>
</div>
<p id='heightHere'> this is </p>
</div>

关于css - 如何使 PRE 扩展到列大小而不增加新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089025/

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