Django,Bootstrap响应式文本区域

huangapple go评论137阅读模式
英文:

Django, Bootstrap responsive text area

问题

我写了一小段JavaScript代码,用来使文本区域根据旁边的图像大小变得响应式。除了一个我真的不理解的奇怪随机错误外,它运行得相当顺利。页面应该如下所示:

Django,Bootstrap响应式文本区域

这在大约70%的时间里都有效,文本区域加载时高度正确。偶尔会发生以下情况:

Django,Bootstrap响应式文本区域

如果我刷新页面,它会立即采用正确的高度。以下是我的代码:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
    <h1 class="display-3 text-center">Willkommen auf</h1>
    <h1 class="display-6 text-center">Diese Seite befindet sich im Aufbau</h1>
    <div class="row">
        <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3">
            <div id="targetcard" class="card">
                <img id="pic_uc" class="nav-item rounded" src="https://placehold.co/600x400"
                    alt=""></img>
                <div class="card-img-overlay">
                    <a target="_blank" href="http://www.freepik.com"></a>
                </div>
                <div class="card-body">
                    <p class="card-text fs-6 fw-light lh-1">Designed by BiZkettE1 / Freepik</p>
                </div>
            </div>
        </div>
        <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3">
            <div id="textContentAre" class="card border-0 overflow-y-scroll">
                <!-- 此处为您的文本内容 -->
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col my-3 text-center">
            <p class="text-muted">
                Kontakt <a href="#" class="text-reset">Kontakt</a>.
            </p>
        </div>
    </div>
</div>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        let height = document.querySelector("#targetcard").clientHeight;
        document.querySelector('#textContentAre').style.height = height + "px";
        window.addEventListener("resize", function () {
            if (height != document.querySelector("#targetcard").clientHeight) {
                height = document.querySelector("#targetcard").clientHeight;
                document.querySelector('#textContentAre').style.height = height + "px";
            }
        });
        return false;
    })
</script>

如果有人能告诉我为什么文本区域有时加载的高度不正确,我将非常感激。

英文:

I Wrote a little java script to make a text area responsive, depending on the image size next to it. That works pretty fine except of a weired random bug I really don't understand. This is how the page should look like:

Django,Bootstrap响应式文本区域

That works in approx. 70% on the time, that the textarea is loaded with the correct hight. Every once and a while it happens that this happens:

Django,Bootstrap响应式文本区域

If I refresh the page it directly takes the correct hight. Here is my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;h1 class=&quot;display-3 text-center&quot;&gt;Willkommen auf&lt;/h1&gt;
&lt;h1 class=&quot;display-6 text-center&quot;&gt;Diese Seite befindet sich im Aufbau&lt;/h1&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3&quot;&gt;
&lt;div id=&quot;targetcard&quot; class=&quot;card&quot;&gt;
&lt;img id=&quot;pic_uc&quot; class=&quot;nav-item rounded&quot; src=&quot;https://placehold.co/600x400&quot;
alt=&quot;&quot;&gt;&lt;/img&gt;
&lt;div class=&quot;card-img-overlay&quot;&gt;
&lt;a target=&quot;_blank&quot; href=&quot;http://www.freepik.com&quot;&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;p class=&quot;card-text fs-6 fw-light lh-1&quot;&gt;Designed by BiZkettE1 / Freepik&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3&quot;&gt;
&lt;div id=&quot;textContentAre&quot; class=&quot;card border-0 overflow-y-scroll&quot;&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit. Consectetur officiis iusto dolorum aspernatur
itaque sunt placeat veniam velit eius! Itaque molestias laborum repellat dicta doloribus? Unde atque
perferendis eaque architecto. Tempora sapiente accusantium iste libero qui fugiat consectetur laudantium
placeat ab explicabo inventore sunt modi facere a dolor, vitae at ratione! Explicabo nulla quasi
provident facilis repellendus esse deleniti tenetur eaque ratione perferendis sunt rerum amet
accusantium hic
temporibus quidem, laborum nemo blanditiis doloremque! Fugit eligendi atque illo. Molestiae modi ex
magnam id quaerat perferendis laboriosam illo repellat, in reiciendis! Rem voluptate quae fugit
doloremque
molestias unde ab necessitatibus perspiciatis, ipsa autem, voluptas commodi esse ex. Incidunt libero
dolores consequuntur distinctio sequi impedit voluptatibus, eum inventore, deserunt facere quidem error,
minima
sunt labore enim? Recusandae labore dignissimos libero ab asperiores maiores tenetur magnam, rerum
itaque
voluptate ipsa dolorum voluptatem officia in alias omnis! Animi incidunt, ipsa molestias a ut ducimus
consequuntur libero dicta expedita voluptatem fuga nam veniam facere ad pariatur voluptate autem sunt
reiciendis reprehenderit maiores. Nostrum, architecto. Veniam id facere suscipit aliquid vitae
consequuntur eum aspernatur quam magnam libero illo voluptate corporis iste pariatur consequatur, totam
eligendi
eveniet adipisci obcaecati qui architecto incidunt molestias mollitia fugiat. Laborum quod, sunt
perspiciatis
minima totam architecto quibusdam! Eligendi dignissimos eos ut? Maxime distinctio repellat non magnam
cupiditate beatae corrupti laboriosam deserunt quisquam ullam quam assumenda itaque dolores debitis
facere
exercitationem, officiis explicabo doloribus nobis. Error quibusdam ut suscipit in a. Nisi ad doloremque
ipsum sit tempore perferendis reiciendis accusamus tenetur facilis cupiditate molestias est autem ea
vero nostrum ipsa magnam, iste ipsam deserunt rem fugit. Quidem magnam recusandae doloribus dignissimos
ad
modi inventore sint labore saepe! Tenetur, neque hic ducimus quaerat odit maiores officiis nostrum
aliquid,
architecto ut perspiciatis qui adipisci quas. Ab, laudantium ratione. Cupiditate consequatur iure.
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col my-3 text-center&quot;&gt;
&lt;p class=&quot;text-muted&quot;&gt;
Kontakt &lt;a href=&quot;#&quot; class=&quot;text-reset&quot;&gt;Kontakt&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
document.addEventListener(&#39;DOMContentLoaded&#39;, function () {
let height = document.querySelector(&quot;#targetcard&quot;).clientHeight;
document.querySelector(&#39;#textContentAre&#39;).style.height = height + &quot;px&quot;;
window.addEventListener(&quot;resize&quot;, function () {
//console.log(&#39;View Port: &#39; + window.innerWidth)
if (height != document.querySelector(&quot;#targetcard&quot;).clientHeight) {
height = document.querySelector(&quot;#targetcard&quot;).clientHeight;
document.querySelector(&#39;#textContentAre&#39;).style.height = height + &quot;px&quot;;
}
});
return false;
})
&lt;/script&gt;

</div>
<!-- end snippet -->

If someone could tell me why the textare sometimes is loaded with the wrong hight I would be really thankful.

答案1

得分: 1

以下是翻译好的部分:

这可以在没有任何JavaScript的情况下完成。

两个col元素并排显示,已经自动具有相同的高度,这是因为Bootstrap 5使用flexbox作为其网格系统的基础。

因此,我们在这里需要做的是解决这两个列"竞争"哪个是更大的列,从而确定整行的高度。

通过将文本元素置于其列的绝对位置,我们可以实现这一点-文本不再影响整行的高度,这完全取决于图像。

如果我们将文本元素定位在列元素的所有四个"角"上,它将自动占据相应的宽度和高度(inset一次设置topbottomleftright的值)。

列本身获得了position: relative以成为锚点,对于文本元素,我使用了inset: 0 12px来复制列通常应用于其内容的12px左右填充。

您需要在全屏模式下检查此处呈现的代码片段,以使两列布局实际应用;您还需要将其包装在适当的媒体查询中,以便我的更改不会在所选择的两列断点以下应用,因为在那里文本的绝对定位会导致问题,所以不应该从一开始就应用它。

英文:

This can be done without any JavaScript.

Two col elements that display next to each other already automatically have the same height, due to the fact that Bootstrap 5 uses flexbox for its grid system.

So all we need to do here, is resolve the conflict that both of those columns are "competing" about which of them is the larger one, that will determine the height of the whole row.

By positioning the text element inside its column absolute, we can achieve that - the text gets taken out of the layout flow, does not influence how high the whole row gets any more, that is solely down to the image now.

And if we position the text element at all four "corners" of the column element it is in, it will automatically take that width & height. (inset sets the values for top, bottom, left and right all in one go.)

The column itself gets position: relative to be the anchor point, and for the text element I used inset: 0 12px, to replicate the 12px padding-left and -right the column would normally apply to its content.

You will have to check the rendered snippet here in full-screen mode, so that the two-column layout actually applies; and you will also have to wrap this into appropriate media queries, so that my changes don't apply below the chosen two-column breakpoint, because there the absolute positioning of the text will mess things up, so it must not be applied there to begin with.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.scrollTextColumn {
position: relative;
}
#textContentAre {
position: absolute;
inset: 0 12px;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;h1 class=&quot;display-3 text-center&quot;&gt;Willkommen auf&lt;/h1&gt;
&lt;h1 class=&quot;display-6 text-center&quot;&gt;Diese Seite befindet sich im Aufbau&lt;/h1&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3&quot;&gt;
&lt;div id=&quot;targetcard&quot; class=&quot;card&quot;&gt;
&lt;img id=&quot;pic_uc&quot; class=&quot;nav-item rounded&quot; src=&quot;https://placehold.co/600x400&quot; alt=&quot;&quot;&gt;
&lt;div class=&quot;card-img-overlay&quot;&gt;
&lt;a target=&quot;_blank&quot; href=&quot;http://www.freepik.com&quot;&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;p class=&quot;card-text fs-6 fw-light lh-1&quot;&gt;Designed by BiZkettE1 / Freepik&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3 scrollTextColumn&quot;&gt;
&lt;div id=&quot;textContentAre&quot; class=&quot;card border-0 overflow-y-scroll&quot;&gt;
Lorem ipsum dolor sit amet consectetur adipisicing elit. Consectetur officiis iusto dolorum aspernatur itaque sunt placeat veniam velit eius! Itaque molestias laborum repellat dicta doloribus? Unde atque perferendis eaque architecto. Tempora sapiente
accusantium iste libero qui fugiat consectetur laudantium placeat ab explicabo inventore sunt modi facere a dolor, vitae at ratione! Explicabo nulla quasi provident facilis repellendus esse deleniti tenetur eaque ratione perferendis sunt rerum
amet accusantium hic temporibus quidem, laborum nemo blanditiis doloremque! Fugit eligendi atque illo. Molestiae modi ex magnam id quaerat perferendis laboriosam illo repellat, in reiciendis! Rem voluptate quae fugit doloremque molestias unde
ab necessitatibus perspiciatis, ipsa autem, voluptas commodi esse ex. Incidunt libero dolores consequuntur distinctio sequi impedit voluptatibus, eum inventore, deserunt facere quidem error, minima sunt labore enim? Recusandae labore dignissimos
libero ab asperiores maiores tenetur magnam, rerum itaque voluptate ipsa dolorum voluptatem officia in alias omnis! Animi incidunt, ipsa molestias a ut ducimus consequuntur libero dicta expedita voluptatem fuga nam veniam facere ad pariatur voluptate
autem sunt reiciendis reprehenderit maiores. Nostrum, architecto. Veniam id facere suscipit aliquid vitae consequuntur eum aspernatur quam magnam libero illo voluptate corporis iste pariatur consequatur, totam eligendi eveniet adipisci obcaecati
qui architecto incidunt molestias mollitia fugiat. Laborum quod, sunt perspiciatis minima totam architecto quibusdam! Eligendi dignissimos eos ut? Maxime distinctio repellat non magnam cupiditate beatae corrupti laboriosam deserunt quisquam ullam
quam assumenda itaque dolores debitis facere exercitationem, officiis explicabo doloribus nobis. Error quibusdam ut suscipit in a. Nisi ad doloremque ipsum sit tempore perferendis reiciendis accusamus tenetur facilis cupiditate molestias est autem
ea vero nostrum ipsa magnam, iste ipsam deserunt rem fugit. Quidem magnam recusandae doloribus dignissimos ad modi inventore sint labore saepe! Tenetur, neque hic ducimus quaerat odit maiores officiis nostrum aliquid, architecto ut perspiciatis
qui adipisci quas. Ab, laudantium ratione. Cupiditate consequatur iure.
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col my-3 text-center&quot;&gt;
&lt;p class=&quot;text-muted&quot;&gt;
Kontakt &lt;a href=&quot;#&quot; class=&quot;text-reset&quot;&gt;Kontakt&lt;/a&gt;.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 12:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定