简体   繁体   English

文字溢出

[英]Overflowing text

I am rendering documents in the following function and I need to do this. 我正在使用以下功能渲染文档,而我需要这样做。 I need to count how much of the text is overflowing and that overflowing part needs to cut and replaced with ... . 我需要计算多少文本溢出,该溢出部分需要剪切并替换为...。 So if title is Aaaaaaaaaa and the last three letters will be overflowing - then output should be Aaaa... My function works, so I can say whether the element is overflown or not, but I dont know, how to calculate, how many letters I need to cut. 所以,如果标题是Aaaaaaaaaa,最后三个字母将溢出-那么输出应该是Aaaa ...我的函数起作用了,所以我可以说元素是否溢出,但是我不知道如何计算,多少个字母我要剪

export const fillDocuments = (data) => {

    for(var i = 0; i < data.length; i++){

        const state = data[i].documentState.id;
        const documentsForUsers = data[i].documentsForUsers;
        const approval = documentsForUsers[0].approval;

        var img = "";

        if(state === 2) 
        { // approval
            img="img/decision.svg";
        } else if(state === 1){
            // approved
            img = "img/agree.svg";
        } else if(state === 1 && approval === 2){
            img = "img/unread.svg";
        } 
        else {
            // disapproved
            img ="img/disagree.svg";
        }
        var email = $("#user_email").text();
        var object = data[i].documentsForUsers.find(el => el.user.email==email);
        var sharingType = object.sharingType.id;

        var time = sharingType === 1 ? data[i].uploadDatetime : data[i].activeStartTime;

      const markup = `<li class="side-nav__item msg-item" id="msg_item" data-id="${data[i].id}">
      <a href="#" class="side-nav__link" id="msg_link">
          <img src="${img}" class="side-nav__icon" id="msg_icon">
          </img>
          <div class="msg-data" id="msg_data">
          <span class="msg-title" id="msg_title">${data[i].title}</span>
          <span class="msg-date" id="msg_date">${formatTimeForMessages(time)}</span>
          </div>
      </a>
  </li>`;

      elements.side_msgs.insertAdjacentHTML("beforeend", markup);

      var element = document.body.querySelector(`.msg-item[data-id="${data[i].id}"]`);

      if(isOverflown(element)){
        // IF IS OVERFLOWN, THEN ADD CUT THE OVERFLOWING PART AND ADD ... instead
      }
    }
};

function isOverflown(element) {
    return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
}

you can just use following css: 您可以只使用以下CSS:

{
    white-space: nowrap;
    overflow-x: hidden;
    text-overflow: ellipsis;
}

More detailed below: https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/ 详细信息如下: https : //css-tricks.com/snippets/css/truncate-string-with-ellipsis/

This is not easy to do in Javascript, because the size of the text is affected by the way the font is rendered. 这在Javascript中不容易实现,因为文本的大小受字体呈现方式的影响。 That can change from client to client, especially if they use font overrides for accessibility. 客户端之间的情况可能会有所不同,尤其是如果他们使用字体替代来实现可访问性时。

Instead, think about using CSS for those elements: 相反,请考虑对这些元素使用CSS:

// for titles that should be truncated with ellipsis
.ellipsify {
    text-overflow: ellipsis;
    white-space: nowrap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM