gpt4 book ai didi

javascript - 如何创建一个 "show more"按钮并指定最初可以显示多少行文本

转载 作者:技术小花猫 更新时间:2023-10-29 12:29:37 25 4
gpt4 key购买 nike

我正在寻找一种在我的响应式网站上创建滑出式“显示更多”功能的方法,该功能会在段落的两行 之后中断。

我之前在静态网站上实现过这一点,方法是将设置的高度应用到容器并使用 overflow: hidden,然后设置容器高度的动画。

但作为响应式,容器会在不同的浏览器宽度下压缩文本,因此文本可能会占用更多/更少的空间。也可能每次往下推段落上面的内容都不一样。所以设置 height 可能不会正好覆盖两行。

请检查这个 jsFiddle:http://jsfiddle.net/XVAzU/如果您需要演示。

所以我需要在段落的两行之后切断,无论容器的宽度是多少,也不管该段前后的内容。

感谢您的关注!

最佳答案

从您的 fiddle 开始,将内容包装到 <div> 中默认类别为 content ,用于选择和一个名为 hideContent 的类将与 showContent 交换当点击 show more/show less关联。

我还删除了 <p>文本在里面。文本现在在 content-div 中,我们现在也能够应用正确的高度和行高设置。

HTML:

<div class="text-container">
<h1>Title goes here</h1>
<h2>Subtitle</h2>
<div class="content hideContent">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
<p>Some more text</p>
<ul>
<li>Some more text</li>
<li>Some more text</li>
<li>Some more text</li>
</ul>
</div>
<div class="show-more">
<a href="#">Show more</a>
</div>
</div>​

CSS:

.hideContent {
overflow: hidden;
line-height: 1em;
height: 2em;
}

.showContent {
line-height: 1em;
height: auto;
}

我假设设置 line-height将确保它在所有浏览器中都是相同的。不过我不是 100% 确定这一点。

我将点击事件附加到“显示更多”链接,使用 jQueryUI 切换 div 上的类 switchClass() :

$(".show-more a").on("click", function() {
var $this = $(this);
var $content = $this.parent().prev("div.content");
var linkText = $this.text().toUpperCase();

if(linkText === "SHOW MORE"){
linkText = "Show less";
$content.switchClass("hideContent", "showContent", 400);
} else {
linkText = "Show more";
$content.switchClass("showContent", "hideContent", 400);
};

$this.text(linkText);
});​

JsFiddle Demo - 显示更多/显示更少并应用行高和动画

$(".show-more a").on("click", function() {
var $this = $(this);
var $content = $this.parent().prev("div.content");
var linkText = $this.text().toUpperCase();

if (linkText === "SHOW MORE") {
linkText = "Show less";
$content.switchClass("hideContent", "showContent", 400);
} else {
linkText = "Show more";
$content.switchClass("showContent", "hideContent", 400);
};

$this.text(linkText);
});
div.text-container {
margin: 0 auto;
width: 75%;
}

.hideContent {
overflow: hidden;
line-height: 1em;
height: 2em;
}

.showContent {
line-height: 1em;
height: auto;
}

.showContent {
height: auto;
}

h1 {
font-size: 24px;
}

p {
padding: 10px 0;
}

.show-more {
padding: 10px 0;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div class="text-container">
<h1>Title goes here</h1>
<h2>Subtitle</h2>
<div class="content hideContent">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
<p>Some more text</p>
<ul>
<li>Some more text</li>
<li>Some more text</li>
<li>Some more text</li>
</ul>
</div>
<div class="show-more">
<a href="#">Show more</a>
</div>
</div>

上面的代码只是一个例子,但应该让你开始朝着正确的方向前进。

关于javascript - 如何创建一个 "show more"按钮并指定最初可以显示多少行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12307666/

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