插件窝 干货文章 如何用CSS和JavaScript优雅地实现三行文本省略及“详情”按钮布局?

如何用CSS和JavaScript优雅地实现三行文本省略及“详情”按钮布局?

三行 文本 显示 按钮 839    来源:    2025-03-21

要实现三行文本省略及“详情”按钮的布局,可以使用CSS的text-overflowline-clamp属性来控制文本的显示,并结合JavaScript来动态控制“详情”按钮的显示与隐藏。以下是实现步骤:

1. HTML结构

首先,创建一个包含文本和“详情”按钮的HTML结构。

<div class="text-container">
  <p class="text-content">
    这是一段很长的文本内容,可能会超出三行的显示范围。这是一段很长的文本内容,可能会超出三行的显示范围。这是一段很长的文本内容,可能会超出三行的显示范围。
  </p>
  <button class="details-button">详情</button>
</div>

2. CSS样式

使用CSS来控制文本的显示和省略,并设置“详情”按钮的样式。

.text-container {
  width: 300px; /* 设置容器的宽度 */
  position: relative;
}

.text-content {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3; /* 限制显示的行数 */
  overflow: hidden;
  text-overflow: ellipsis; /* 超出部分显示省略号 */
  line-height: 1.5; /* 设置行高 */
  margin: 0;
}

.details-button {
  position: absolute;
  right: 0;
  bottom: 0;
  background-color: #007bff;
  color: white;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
  display: none; /* 默认隐藏按钮 */
}

3. JavaScript逻辑

使用JavaScript来检测文本是否被省略,并根据情况显示或隐藏“详情”按钮。

document.addEventListener("DOMContentLoaded", function() {
  const textContent = document.querySelector('.text-content');
  const detailsButton = document.querySelector('.details-button');

  // 检测文本是否被省略
  if (textContent.scrollHeight > textContent.clientHeight) {
    detailsButton.style.display = 'block'; // 显示按钮
  }

  // 点击按钮显示完整文本
  detailsButton.addEventListener('click', function() {
    textContent.style.webkitLineClamp = 'unset'; // 取消行数限制
    detailsButton.style.display = 'none'; // 隐藏按钮
  });
});

4. 完整代码

将上述代码整合到一起,完整的实现如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>三行文本省略及详情按钮</title>
  <style>
    .text-container {
      width: 300px;
      position: relative;
    }

    .text-content {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 1.5;
      margin: 0;
    }

    .details-button {
      position: absolute;
      right: 0;
      bottom: 0;
      background-color: #007bff;
      color: white;
      border: none;
      padding: 5px 10px;
      cursor: pointer;
      display: none;
    }
  </style>
</head>
<body>
  <div class="text-container">
    <p class="text-content">
      这是一段很长的文本内容,可能会超出三行的显示范围。这是一段很长的文本内容,可能会超出三行的显示范围。这是一段很长的文本内容,可能会超出三行的显示范围。
    </p>
    <button class="details-button">详情</button>
  </div>

  <script>
    document.addEventListener("DOMContentLoaded", function() {
      const textContent = document.querySelector('.text-content');
      const detailsButton = document.querySelector('.details-button');

      if (textContent.scrollHeight > textContent.clientHeight) {
        detailsButton.style.display = 'block';
      }

      detailsButton.addEventListener('click', function() {
        textContent.style.webkitLineClamp = 'unset';
        detailsButton.style.display = 'none';
      });
    });
  </script>
</body>
</html>

5. 解释

  • CSS部分:使用-webkit-line-clamp属性限制文本显示为三行,超出部分显示省略号。details-button按钮默认隐藏,只有在文本被省略时才显示。
  • JavaScript部分:通过比较scrollHeightclientHeight来判断文本是否被省略。如果被省略,则显示“详情”按钮。点击按钮后,取消行数限制并隐藏按钮。

6. 兼容性

  • -webkit-line-clamp属性在现代浏览器中支持良好,但在某些旧版浏览器中可能不支持。如果需要兼容性更好的解决方案,可以考虑使用JavaScript动态计算文本高度并手动截断文本。

通过这种方式,你可以优雅地实现三行文本省略及“详情”按钮的布局。