【问题标题】:How to create a "show more" button and specify how many lines of text can be initially shown如何创建“显示更多”按钮并指定最初可以显示多少行文本
【发布时间】:2012-08-31 17:18:24
【问题描述】:

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

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

但是响应式,容器会在不同的浏览器宽度下挤压文本,因此文本可能会占用更多/更少的空间。每次下推,段落上方也可能有不同的内容。所以设置height 可能不会完全覆盖两行。

如果需要演示,请查看这个 jsFiddle:http://jsfiddle.net/XVAzU/

所以我需要在段落的两行之后切断,无论容器的宽度是多少,或者该段落之前或之后是什么。

感谢收看!

【问题讨论】:

  • 使用 CSS 你可以应用 line-height: 1emheight: 2em 应该总是显示两行。我不知道跨浏览器兼容性,但我附在答案中的 DEMO 只显示了 Chrome、FireFox、IE9 和 IE8 中预期的两行文本。

标签: javascript jquery css html


【解决方案1】:

从你的小提琴开始,将内容包装到一个<div> 中,默认类为content,用于选择和一个名为hideContent 的类,当单击show more/show less 链接时,该类将与showContent 交换.

我还删除了文本所在的 <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 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 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>

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

【讨论】:

  • 我正在做类似的事情,但我想我更喜欢你的解决方案。作为替代jsfiddle.net/XVAzU/4。我会将初始类设置为 showContent,并在加载文档时将其替换为 js 中的 hideContent 类,仅适用于禁用 js 的人。
  • @owen:如果这就是您要找的东西,或者您是否需要任何其他帮助,请告诉我。
  • @François Wahl,看看你的 jsFiddle,它看起来很完美!感谢您为此付出的时间。我还没有时间在实际站点上对其进行测试,出现了一些紧急情况,但我会将答案标记为正确,因为 jsFiddle 正是我正在寻找的。​​span>
  • @Owen:不用担心,如果您需要帮助解决问题,请告诉我。
  • 很好的例子@FrançoisWahl,完美地满足了我的需要
【解决方案2】:

如果您正在寻找仅使用 css 的解决方案,请查看:

HTML

 <div class="show-hide-text">
  <a  id="show-more" class="show-less" href="#show-less">Show less</a>
  <a  id="show-less" class="show-more" href="#show-more">Show more</a>
  <p>
    Lorem Ipsum is simply dummy text of  the printing and typesetting industry...
  </p>
</div>

// CSS

.show-hide-text {
  display: flex;
  flex-wrap: wrap;
}

.show-hide-text a {
  order: 2;
}

.show-hide-text p {
  position: relative;
  overflow: hidden;
  max-height: 60px; // The Height of 3 rows
}

.show-hide-text p {
  display: -webkit-box;
  -webkit-line-clamp: 3; // 3 Rows of text
  -webkit-box-orient: vertical;
}

.show-less {
  display: none;
}

.show-less:target {
  display: block;
}

.show-less:target ~ p {
  display: block;
  max-height: 100%;
}

.show-less:target + a {
  display: none;
}

一个例子:https://codepen.io/srekoble/pen/WGBzZa?editors=1100

【讨论】:

    【解决方案3】:

    我的解决问题的建议

    $("#slider_desc_toogler").on( "click", function() { 
      $('#slider_desc_toogler > i').toggleClass('fa-arrow-circle-down')
      $('#slider_desc_toogler > i').toggleClass('fa-arrow-circle-up')
      if ($("#slider_desc_toogler > i").hasClass( "fa-arrow-circle-down" )) {
        $(".slider_desc").css("max-height","38px");
      } else $(".slider_desc").css("max-height","500px");
    });
    .slider_desc {
      margin: 16px;
      margin-top: 0px;
      color: #333333;
      font-family: Arial;
      font-size: 14px;
      line-height: 18px;
      text-align: justify;
      overflow: hidden;
      transition: all 0.5s ease 0s;
      max-height: 38px;
    }
    
    #slider_desc_toogler{
      border-top: silver 1px dotted;
      margin-bottom: 30px;
      margin-top: 20px;
      width: 70%;
      margin-left: auto;
      margin-right: auto;
    }
    
    #slider_desc_toogler i {
          position: absolute;
          text-align: center;
          color: silver;
          font-size: 25px;
          font-family: fontawesome;
          left: calc(50% - 10px);
          margin-top: -13px;
          background: #fff;
      }
    <link href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <div class="slider_desc">
                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    
    
                </div>
                <div id="slider_desc_toogler"><i class="fas fa-arrow-circle-down"></i></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2018-05-03
      相关资源
      最近更新 更多