so basically I am trying to make it so if the dynamically generated description has over 5 lines it will use line-clamp and only show 5 lines and have a see more/see less thing
所以基本上,我试图使它,如果动态生成的描述有5行以上,它将使用线钳,只显示5行,并有一个多看/少看的东西
<div class="description-container">
<div class="description">
<p>{@html data.description}</p>
</div>
<span class="showMoreLess">Show More</span>
</div>
.description-container {
display: flex;
justify-content: center;
align-items: center;
margin-top: 1rem;
}
.description {
margin-top: 1rem;
padding: 1rem;
width: 70rem;
background-color: #333;
border-radius: 0.5rem;
}
.description p {
margin: 0;
font-size: 1rem;
line-height: 1.5;
}
that is my code as of right now I know the solution might be simple but I'm a semi new developer so anything helps
这是我目前的代码我知道解决方案可能很简单,但我是一个半新的开发人员,所以任何事情都有帮助
I haven't tried anything cus I'm too dumb lol
我什么都没试过,因为我太笨了哈哈
更多回答
Do a little research: fortwin.hashnode.dev/…. If you get stuck somewhere, we will help you. But it's useless to give you a full solution.
做一点研究:fortwin.hashnode.dev/……如果你在某个地方遇到困难,我们会帮助你。但给你一个完整的解决方案是没有用的。
If you use @html
, make sure it is sanitized in case it contains unknown/user input, otherwise that can be dangerous.
如果您使用@html,请确保它经过消毒,以防它包含未知/用户输入,否则可能会很危险。
Also if the description can contain any HTML, it is pretty much impossible to always determine the size of 5 lines.
此外,如果描述可以包含任何HTML,那么几乎不可能总是确定5行的大小。
I agree with @Wimanicesir !!
我同意@Wimanicesir!!
Is the data.description
in html?
data.description是html格式的吗?
优秀答案推荐
I put this solution together, and will post it here with this disclaimer: I did not test it with arbitrary content. I have only tested it with a paragraph.
我把这个解决方案放在一起,并将在这里发布免责声明:我没有用任意内容测试它。我只用一段话来测试它。
Also, this is probably a tricky thing to set up correctly. Many CSS inherited styling might come into play that can skew the result in one way or another. This requires thorough testing.
此外,这可能是一件很难正确设置的事情。许多CSS继承的样式可能会以某种方式扭曲结果。这需要进行彻底的测试。
Solution
Code first. This can be seen working in this Svelte REPL.
代码优先。这可以在Svelte REPL中看到。
<script>
let hContents, hReference;
$: exceeds = hContents > hReference;
</script>
<div class="content" bind:clientHeight={hContents}>
<div class="reference" bind:clientHeight={hReference}>
{#each { length: 5 } as _, i}
<span>{i}</span>
{/each}
</div>
<p>Lorem ipsum dolor sit amet, consectetur pulvinar. At lectus urna duis convallis convallis tellus. Aliquet enim tortor at auctor urna nunc id cursus metus. Sem viverra aliquet eget sit amet tellus cras. Pulvinar sapien et ligula ullamcorper malesuada proin libero nunc. Integer eget aliquet nibh praesent tristique magna sit. Pellentesque eu tincidunt tortor aliquam nulla. Vitae tortor condimentum lacinia quis. Eget duis at tellus at urna condimentum mattis. Bibendum enim facilisis gravida neque convallis. Mi tempus imperdiet nulla malesuada pellentesque.</p>
</div>
{#if exceeds}
<h2>Exceeds!</h2>
{/if}
<style>
div.content {
font-size: 14pt;
line-height: 1.2;
position: relative;
}
div.reference {
position: absolute;
left: 0;
top: 0;
pointer-events: none;
width: 100%;
visibility: hidden;
}
div.reference > span {
display: block;
}
</style>
Explanation
We create a reference DIV as a child of the container we want to measure to hopefully inherit important styles like font-size
and line-height
. We make this child invisible and absolutely-positioned so it doesn't interfere with the document's flow.
我们创建了一个引用DIV,作为我们想要测量的容器的子级,以继承字体大小和行高等重要样式。我们使这个子对象不可见并且处于绝对位置,这样它就不会干扰文档的流动。
We then fill our invisible container with the maximum number of lines we want to test for.
然后,我们用要测试的最大行数填充我们的不可见容器。
Finally, we use Svelte's dimension-binding feature to measure the height of our reference container and the subject container. If the height of the subject container exceeds the height of our reference container, the subject container has more lines.
最后,我们使用Svelte的维度绑定特性来测量我们的参考容器和主题容器的高度。如果主题容器的高度超过我们的引用容器的高度,那么主题容器就有更多的行。
更多回答
我是一名优秀的程序员,十分优秀!