栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何创建“显示更多”按钮并指定最初可以显示多少行文本

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何创建“显示更多”按钮并指定最初可以显示多少行文本

从小提琴开始,将内容包装到中

<div>
,默认类为
content
,用于选择,单击链接时
hideContent
将替换为的类。
showContent``show more/show less

我还删除了

<p>
文本所在的位置。该文本现在位于content-div中,并且我们现在还能够应用正确的高度和行高设置。

HTML:

<div >    <h1>Title goes here</h1>    <h2>Subtitle</h2>    <div >        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 >        <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%。

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

$(".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演示-显示更多/显示更少,并应用行高和动画

$(".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://pre.jquery.com/ui/1.12.1/jquery-ui.min.js"></script><div >  <h1>Title goes here</h1>  <h2>Subtitle</h2>  <div >    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 >    <a href="#">Show more</a>  </div></div>

上面的代码仅是示例,但应该使您入门正确。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647652.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号