gpt4 book ai didi

javascript - 一旦 div 高度达到最大高度,动态扩展 div 的宽度到内容

转载 作者:太空宇宙 更新时间:2023-11-04 11:05:27 24 4
gpt4 key购买 nike

我有一个 div 类,我需要动态更改其宽度,以防 div 超出 height max-height120px。它需要动态增长的原因是我正在append将文本内容添加到div,有时文本太多无法容纳。

现在,div 显示如下:HTML

<div class="arrow_box"></div>

CSS

.arrow_box{
width:320px;
max-height:120px;
}

JS

var timelineSentences = ["OjVIQgtdei NFrqcgRKpy orPfulDVcs eisVloPqXC wfSFifDQqK ghPZyeNKJE wryCLNvlQp pOErunxEtg FyZlcZxkhl xfNSVRuymx HxKxiRTYza AnyUKcgKNO gsYuTscCvx VjIphTkFSc LxmDDASiuI HbWLNJpsVC tTtmBMJsHd asXCGlFqxS vBGpsVkgtN gCPTSKWzog dsAZwIwPbC pWZanKGAFZ dielrXJNMb LZqwCdRtIT viqdgYHGfp PUxhrwKfkG MaiiJUYFOo cfAXxiHTNB TxOqTtxvmg gzmKznNrJF"]

$('.arrow_box').append('<p style="margin-bottom:0;padding:10px;">'+timelineSentences[0]+'</p>');

我看到了一些相关的帖子 A B但在多次尝试调整 scrollHeight

后无法从他们那里得到答案

最佳答案

请看下面的代码。它基于 scrollHeight 工作.

JavaScript解决方案

sample <div>

<div class="arrow_box">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

Approach 1 - If scrollHeight is greater than maxHeight, make <div> 100%

<script>
var
arrowBox = document.querySelector('.arrow_box'), // Get element
scrollHeight = arrowBox.scrollHeight, // Get scrollHeight
maxHeight = 120;

if (scrollHeight > maxHeight) {
// Make width as 100%
arrowBox.style.width = '100%';
}
</script>

Approach 2 - Increase width equivalent to the exceeded height in pixels

<script>
var
arrowBox = document.querySelector('.arrow_box'), // Get element
scrollHeight = arrowBox.scrollHeight, // Get scrollHeight
maxHeight = 120,
constantFactor = 3; // To make sure after readjusting the width, content does not overflow again

if (scrollHeight > maxHeight) {
// Increase width equivalent to the exceeded height in pixels
var extraPx = (scrollHeight - maxHeight) * constantFactor;
arrowBox.style.width = (arrowBox.offsetWidth + extraPx) + 'px';
}
</script>

jQuery两种方法的解决方案!

(document).ready(function() {
var timelineSentences = ["OjVIQgtdei NFrqcgRKpy orPfulDVcs eisVloPqXC wfSFifDQqK ghPZyeNKJE wryCLNvlQp pOErunxEtg FyZlcZxkhl xfNSVRuymx HxKxiRTYza AnyUKcgKNO gsYuTscCvx VjIphTkFSc LxmDDASiuI HbWLNJpsVC tTtmBMJsHd asXCGlFqxS vBGpsVkgtN gCPTSKWzog dsAZwIwPbC pWZanKGAFZ dielrXJNMb LZqwCdRtIT viqdgYHGfp PUxhrwKfkG MaiiJUYFOo cfAXxiHTNB TxOqTtxvmg gzmKznNrJF"]

$('.arrow_box').append('<p style="margin-bottom: 0; padding: 10px;">' + timelineSentences[0] + '</p>');

// ...
// CODE BELOW
// ...

Approach 1 - If scrollHeight is greater than maxHeight, make <div> 100%

  var
arrowBox = $('.arrow_box'), // Get element
scrollHeight = arrowBox.prop('scrollHeight'), // Get scrollHeight
maxHeight = 120;

if (scrollHeight > maxHeight) {
// Make width as 100%
arrowBox.width('100%');
}
}); // Close ready() function

Approach 2 - Increase width equivalent to the exceeded height in pixels

  var
arrowBox = $('.arrow_box'), // Get element
scrollHeight = arrowBox.prop('scrollHeight'), // Get scrollHeight
maxHeight = 120,
constantFactor = 3; // To make sure after readjusting the width, content does not overflow again

if (scrollHeight > maxHeight) {
// Increase width equivalent to the exceeded height in pixels
var extraPx = (scrollHeight - maxHeight) * constantFactor;
arrowBox.width((arrowBox.width() + extraPx) + 'px');
}
}); // Close ready() function

希望对您有所帮助!

关于javascript - 一旦 div 高度达到最大高度,动态扩展 div 的宽度到内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006453/

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