【问题标题】:Truncate text in scalable content截断可缩放内容中的文本
【发布时间】:2021-03-30 14:29:42
【问题描述】:

我将页面分为两部分,我使用 'w-50' bootstrap(4) css 类完成了它。

所有页面项目(组件)都已创建和设计,它们按预期工作,除了应根据可用空间截断的文本(Lorem impsum dolor sit amet)。

我已经列出了上传图片中标记为橙色的组件类型。 select -> span -> img(本例中为星号) -> div(固定大小)

当所有其他元素都选择时,img(star), div(fixed) 取它们各自的宽度,我们必须截断我们的 span(lorem ipsum)。

您能否提供任何解决方案或建议一个应调查的主题以存档预期结果?

JSFiddle: https://jsfiddle.net/AEMLoviji/umf6nzL3/6/

【问题讨论】:

  • 您好...您所说的截断是什么意思。你也可以在 jsfiddle 上分享你的代码吗?想看看那里:)
  • 嗨@ImranRafiqRather。截断文本意味着使用 CSS 的 text-overflow 属性等进行文本截断。如果您查看上传的图像(即完全 lorem ipsum 文本),那么您可以直观地理解文本截断的含义。我想在 jsfiddle 上共享当前代码需要一些时间。所以我会在完成时分享它。
  • 没问题。我明白你来自哪里。一旦你在 codepen 或 jsfiddle 上有你的代码。随时给我留言,我可以在那里实际看到发生了什么。
  • @ImranRafiqRather 我创建了 JSFiddle,其中包含具有相似组件的相似页面。 jsfiddle.net/AEMLoviji/umf6nzL3/6
  • 太棒了!让我看看:)

标签: css twitter-bootstrap bootstrap-4


【解决方案1】:

更新:创建了我的自定义方案来解决问题。除了添加一些额外的 CSS 之外,还使用了一些 Javascript 来满足所需的条件。

工作代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Account Dashboard</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
    />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

    <style>
      select {
        width: 100px;
      }
      .fixed-right-aligned {
        flex-basis: 330px;
      }

      .left-actions {
        background-color: #fa4567;
      }

      .right-actions {
        background-color: #13a352;
      }

      .left-content {
        background-color: #4667fa;
      }

      .right-content {
        background-color: #67fa46;
      }

      /* --- Added CSS --- */
      .wrapper-aem {
        height: 20px;
        position: relative;
        overflow: hidden;
        padding-right: 5px;
      }

      .three-dots {
        display: none;
        position: absolute;
        top: 0;
        right: 0;
      }
    </style>
  </head>
  <body>
    <div class="h-100 w-100 p-0">
      <div class="d-flex w-100">
        <div class="d-flex w-100">
          <div class="w-50 left-actions">
            <div class="d-flex">
              <div
                class="d-flex align-items-center left-actions-content flex-grow-1"
              >
                <select>
                  <option>1</option>
                  <option>2</option>
                  <option>3</option>
                </select>

                <div class="d-flex align-items-center flex-grow-1">
                  <div class="wrapper-aem flex-grow-1">
                    <span class="lorem-content">
                      Lorem ipsum There are no accidents Lorem ipsum dolor sit,
                      Lorem ipsum dolor sit amet.
                    </span>
                    <span class="three-dots">...</span>
                  </div>
                  <div class="block d-flex align-items-center">
                    <button class="btn btn-icon info-icon" type="button">
                      <svg
                        height="16pt"
                        viewBox="0 -10 511.98685 511"
                        width="16pt"
                        xmlns="http://www.w3.org/2000/svg"
                      >
                        <path
                          d="m510.652344 185.902344c-3.351563-10.367188-12.546875-17.730469-23.425782-18.710938l-147.773437-13.417968-58.433594-136.769532c-4.308593-10.023437-14.121093-16.511718-25.023437-16.511718s-20.714844 6.488281-25.023438 16.535156l-58.433594 136.746094-147.796874 13.417968c-10.859376 1.003906-20.03125 8.34375-23.402344 18.710938-3.371094 10.367187-.257813 21.738281 7.957031 28.90625l111.699219 97.960937-32.9375 145.089844c-2.410156 10.667969 1.730468 21.695313 10.582031 28.09375 4.757813 3.4375 10.324219 5.1875 15.9375 5.1875 4.839844 0 9.640625-1.304687 13.949219-3.882813l127.46875-76.183593 127.421875 76.183593c9.324219 5.609376 21.078125 5.097657 29.910156-1.304687 8.855469-6.417969 12.992187-17.449219 10.582031-28.09375l-32.9375-145.089844 111.699219-97.941406c8.214844-7.1875 11.351563-18.539063 7.980469-28.925781zm0 0"
                          fill="#ffc107"
                        />
                      </svg>
                    </button>
                  </div>
                </div>
              </div>

              <div
                class="d-flex p-1 align-items-center justify-content-end fixed-right-aligned"
              >
                Fixed sized toolbar
              </div>
            </div>
          </div>
          <div class="w-50 right-actions">
            <div class="d-flex">
              <div class="d-flex align-items-center">
                <p>Any content goes here</p>
              </div>

              <div
                class="d-flex p-1 align-items-center justify-content-end fixed-right-aligned"
              >
                <span>Fixed sized toolbar</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="d-flex">
        <div class="w-50 left-content">
          <span> Left content </span>
        </div>
        <div class="w-50 right-content">
          <span> Right content </span>
        </div>
      </div>
    </div>

    <script>
      document.addEventListener("DOMContentLoaded", (e) => {
        const loremWrapper = document.querySelector(".wrapper-aem");

        // I have used the resize handler for testing purposes, It may or may not be needed (decide as per your requirement)
        window.addEventListener("resize", () => {
          handleContentBehaviour();
        });

        const handleContentBehaviour = () => {
          if (window.innerWidth > 1366) {
            document.querySelector(".three-dots").style.display =
              "inline-block";
          } else {
            document.querySelector(".three-dots").style.display = "none";
          }
        };
        handleContentBehaviour();
      });
    </script>
  </body>
</html>

【讨论】:

  • 我已经为你定义了一个类,但不幸的是我不知道如何正确/轻松地为每个特定的屏幕尺寸定义宽度。据我了解,我需要为每个屏幕尺寸定义不同的宽度(即 1366px、1367px、1368px 等),这很难维护。
  • 嘿兄弟,我有一个问题,如果我得到你的回答,我想你的问题就解决了。您只希望文本(Lorem impsum dolor sit amet)被截断,但基于可用空间而不触及任何其他代码。我说得对吗?
  • 绝对是的。这正是我的问题
  • 请稍等。 :)
  • 因此,目前您在较小的屏幕上会遇到问题,因为浏览器窗口会缩小。如果我错了,请纠正我:)
猜你喜欢
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 2015-03-27
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多