最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

前端實(shí)現(xiàn)文本超出指定行數(shù)顯示"展開"和"收起"效果詳細(xì)步驟

 更新時(shí)間:2024年10月12日 10:29:14   作者:Nyingchi-X  
本文介紹如何使用JavaScript原生代碼實(shí)現(xiàn)文本折疊展開效果,并提供方法指導(dǎo)如何在Vue或React等框架中修改實(shí)現(xiàn),詳細(xì)介紹了創(chuàng)建整體框架、設(shè)置樣式及利用JS控制元素的步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

效果演示

本文方法是利用js原生進(jìn)行實(shí)現(xiàn)的,可根據(jù)相關(guān)vue或react語法進(jìn)行相關(guān)的改寫,并實(shí)現(xiàn)效果

步驟一:實(shí)現(xiàn)整體框架

 <div class='export-info-bar'>
    <span class='ellipsis-text export-info-text'>
      測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試
    </span>
    <span class='expand-button export-btn-group'>
      展開
    </span>
  </div>
  <div class='export-info-bar'>
    <span class='ellipsis-text export-info-text'>
      測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試
    </span>
    <span class='expand-button export-btn-group'>
      展開
    </span>
  </div>
  <div class='export-info-bar'>
    <span class='ellipsis-text export-info-text'>
      測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試</span>
    <span class='expand-button export-btn-group'>
      展開
    </span>
  </div>

步驟二:實(shí)現(xiàn)樣式

1、底部容器設(shè)置定位,按鈕和文本容器利用z-index設(shè)計(jì)容器層級,并使按鈕定位在右下角;

2、設(shè)置 高度=指定隱藏行數(shù)*一行文本的高度,此處一行高度為21px,指定兩行隱藏,最大高度就為42px;

3、設(shè)置按鈕背景為白色,覆蓋在文本上方

    /* 底部容器 */
    .export-info-bar {
      position: relative;
      /* overflow: hidden; */
      margin-top: 10px;
    }

    /* 文本 */
    .ellipsis-text {
      position: relative;
      /* float: left; */
      /* (***)設(shè)置最大高度為兩行的高度 */
      max-height: 42px; 
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      word-break: break-all;
      word-wrap: break-word;
      /* 設(shè)置文本在按鈕下方 */
      z-index: 0; 
    }

    /* 按鈕前方省略號的樣式 */
    .pseudo-element {
      color: #000;
      margin-right: 5px;
    }

    /* 按鈕 */
    .expand-button {
      /* (***) 設(shè)置定位,使按鈕與最后一行平行并覆蓋在最后一行上方 */
      position: absolute;
      /* 設(shè)置按鈕浮動(dòng)在右方 */
      /* float: right; */
      /* 默認(rèn)隱藏按鈕 */
      display: none;
      /* 將按鈕向上移動(dòng)一行 */
      right: 0px;
      bottom: -3px;
      color: skyblue;
      /* 此處背景一定要設(shè)置, */
      background: #fff;
    }

步驟三:js實(shí)現(xiàn)元素控制

    // 元素前方插入文本(因?yàn)閖s沒有無法設(shè)置::before所以,實(shí)現(xiàn)了一個(gè)元素前方插入文本的方法)
    const addContentBefore = (element, content) => {
      // 創(chuàng)建一個(gè)新的偽元素
      const pseudoElement = document.createElement('span');
      pseudoElement.classList.add('pseudo-element');
      pseudoElement.textContent = content;

      // 將偽元素插入到目標(biāo)元素的前面
      if (element.firstChild) {
        element.insertBefore(pseudoElement, element.firstChild);
      } else {
        element.appendChild(pseudoElement);
      }
    };
	
	
    const getMoreText = () => {
        // 獲取文本容器
        const textContents = document.querySelectorAll('.export-info-text');
        // 獲取按鈕容器
        const expandButtons = document.querySelectorAll('.expand-button');

        // 檢查文本是否有超出兩行的文本,并顯示展開按鈕
        textContents.forEach((content, index) => {
          // 輸出查看當(dāng)前高度是否超過兩行高度(根據(jù)當(dāng)前字體大小進(jìn)行適配)
          console.log(content.scrollHeight, content.clientHeight)
          if (content.scrollHeight > content.clientHeight) {
            expandButtons[index].style.display = 'block'; // 顯示展開按鈕
            addContentBefore(expandButtons[index], '...'); // 按鈕前方模擬省略號
          }
        });

        // 為所有按鈕綁定點(diǎn)擊事件
        expandButtons.forEach((button, index) => {
          button.addEventListener('click', () => {
            // 切換文本的max-height屬性,實(shí)現(xiàn)展開效果
            const content = textContents[index];
            if (content.style.maxHeight !== 'none') {
               // 如果未展開,展開所有文本
              content.style.maxHeight = 'none';
              button.textContent = '收起'; // 改變按鈕文本
              addContentBefore(button, '');
            } else {
              // 如果已經(jīng)展開,恢復(fù)到兩行
              content.style.maxHeight = '42px'; // 恢復(fù)到兩行的高度
              button.textContent = '展開'; // 恢復(fù)按鈕文本
              addContentBefore(button, '...');
            }
          });
        });
      };

    getMoreText();

完整代碼

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 文本超出兩行顯示省略號,并設(shè)置“展開”,“收起”效果-->
  <style>
    /* 底部容器 */
    .export-info-bar {
      position: relative;
      /* overflow: hidden; */
      margin-top: 10px;
    }

    /* 文本 */
    .ellipsis-text {
      position: relative;
      /* float: left; */
      /* (***)設(shè)置最大高度為兩行的高度 */
      max-height: 42px; 
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      word-break: break-all;
      word-wrap: break-word;
      /* 設(shè)置文本在按鈕下方 */
      z-index: 0; 
    }

    /* 按鈕前方省略號的樣式 */
    .pseudo-element {
      color: #000;
      margin-right: 5px;
    }

    /* 按鈕 */
    .expand-button {
      /* (***) 設(shè)置定位,使按鈕與最后一行平行并覆蓋在最后一行上方 */
      position: absolute;
      /* 設(shè)置按鈕浮動(dòng)在右方 */
      /* float: right; */
      /* 默認(rèn)隱藏按鈕 */
      display: none;
      /* 將按鈕向上移動(dòng)一行 */
      right: 0px;
      bottom: -3px;
      color: skyblue;
      /* 此處背景一定要設(shè)置, */
      background: #fff;
    }
  </style>
</head>

<body>
  <div class='export-info-bar'>
    <span class='ellipsis-text export-info-text'>
      測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試
    </span>
    <span class='expand-button export-btn-group'>
      展開
    </span>
  </div>
  <div class='export-info-bar'>
    <span class='ellipsis-text export-info-text'>
      測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試
    </span>
    <span class='expand-button export-btn-group'>
      展開
    </span>
  </div>
  <div class='export-info-bar'>
    <span class='ellipsis-text export-info-text'>
      測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試</span>
    <span class='expand-button export-btn-group'>
      展開
    </span>
  </div>
  <script type="text/javascript">
    // 元素前方插入文本
    const addContentBefore = (element, content) => {
      // 創(chuàng)建一個(gè)新的偽元素
      const pseudoElement = document.createElement('span');
      pseudoElement.classList.add('pseudo-element');
      pseudoElement.textContent = content;

      // 將偽元素插入到目標(biāo)元素的前面
      if (element.firstChild) {
        element.insertBefore(pseudoElement, element.firstChild);
      } else {
        element.appendChild(pseudoElement);
      }
    };

    const getMoreText = () => {
        // 獲取文本容器
        const textContents = document.querySelectorAll('.export-info-text');
        // 獲取按鈕容器
        const expandButtons = document.querySelectorAll('.expand-button');

        // 檢查文本是否有超出兩行的文本,并顯示展開按鈕
        textContents.forEach((content, index) => {
          // 輸出查看當(dāng)前高度是否超過兩行高度(根據(jù)當(dāng)前字體大小進(jìn)行適配)
          console.log(content.scrollHeight, content.clientHeight)
          if (content.scrollHeight > content.clientHeight) {
            expandButtons[index].style.display = 'block'; // 顯示展開按鈕
            addContentBefore(expandButtons[index], '...'); // 按鈕前方模擬省略號
          }
        });

        // 為所有按鈕綁定點(diǎn)擊事件
        expandButtons.forEach((button, index) => {
          button.addEventListener('click', () => {
            // 切換文本的max-height屬性,實(shí)現(xiàn)展開效果
            const content = textContents[index];
            if (content.style.maxHeight !== 'none') {
               // 如果未展開,展開所有文本
              content.style.maxHeight = 'none';
              button.textContent = '收起'; // 改變按鈕文本
              addContentBefore(button, '');
            } else {
              // 如果已經(jīng)展開,恢復(fù)到兩行
              content.style.maxHeight = '42px'; // 恢復(fù)到兩行的高度
              button.textContent = '展開'; // 恢復(fù)按鈕文本
              addContentBefore(button, '...');
            }
          });
        });
      };

    getMoreText();
  </script>
</body>

</html>

總結(jié) 

到此這篇關(guān)于前端實(shí)現(xiàn)文本超出指定行數(shù)顯示”展開”和”收起”效果的文章就介紹到這了,更多相關(guān)前端文本超出指定行數(shù)展開收起內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript事件流之事件處理和傳播機(jī)制深入理解

    JavaScript事件流之事件處理和傳播機(jī)制深入理解

    本文將詳細(xì)介紹JavaScript事件流的發(fā)展流程、屬性以及應(yīng)用場景,并提供一些代碼示例和引用資料,幫助讀者深入理解并應(yīng)用這一重要的前端技術(shù),希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • js中浮點(diǎn)型運(yùn)算BUG的解決方法說明

    js中浮點(diǎn)型運(yùn)算BUG的解決方法說明

    本篇文章主要是對js中浮點(diǎn)型運(yùn)算BUG的解決方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • JavaScript稀疏數(shù)組的成因、坑點(diǎn)與解決方案

    JavaScript稀疏數(shù)組的成因、坑點(diǎn)與解決方案

    在?JavaScript?開發(fā)中,稀疏數(shù)組是一個(gè)極易被忽視但高頻踩坑的知識點(diǎn),本文將從本質(zhì)、成因、實(shí)戰(zhàn)坑點(diǎn)、解決方案四個(gè)維度,來理解這個(gè)稀疏數(shù)組,需要的朋友可以參考下
    2026-03-03
  • 將Datatable轉(zhuǎn)化成json發(fā)送前臺(tái)實(shí)現(xiàn)思路

    將Datatable轉(zhuǎn)化成json發(fā)送前臺(tái)實(shí)現(xiàn)思路

    將Datatable轉(zhuǎn)化成json可以將dt序列化成json,放到前臺(tái)的隱藏控件hidBoundary中,具體的實(shí)現(xiàn)如下,有類似需求的朋有可以參考下
    2013-09-09
  • JavaScript實(shí)現(xiàn)更換背景圖片

    JavaScript實(shí)現(xiàn)更換背景圖片

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)更換背景圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • JavaScript瀏覽器內(nèi)置的四大Web Worker的使用詳解

    JavaScript瀏覽器內(nèi)置的四大Web Worker的使用詳解

    webwork是通過js的方式喚起瀏覽器的內(nèi)置api使用,輔助前端計(jì)算的一種方式,就像fetch、ajaix那樣喚起瀏覽器的接口查詢一樣,下面小編就為大家簡單介紹一下吧
    2025-11-11
  • JavaScript 中的運(yùn)算符和表達(dá)式介紹

    JavaScript 中的運(yùn)算符和表達(dá)式介紹

    這篇文章主要介紹了JavaScript 中的運(yùn)算符和表達(dá)式介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • JS中的事件委托實(shí)例淺析

    JS中的事件委托實(shí)例淺析

    這篇文章主要介紹了JS中的事件委托,結(jié)合實(shí)例形式簡單分析了javascript事件委托的概念、功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2018-03-03
  • SwfUpload在IE10上不出現(xiàn)上傳按鈕的解決方法

    SwfUpload在IE10上不出現(xiàn)上傳按鈕的解決方法

    在測試中發(fā)現(xiàn)使用了SwfUpload實(shí)現(xiàn)的無刷新上傳功能,在IE10上竟然無法使用了,難道SwfUpload不支持嗎?下面與大家分享下通過修改SwfUplad.JS文件讓其支持ie10
    2013-06-06
  • 小程序如何寫動(dòng)態(tài)標(biāo)簽的實(shí)現(xiàn)方法

    小程序如何寫動(dòng)態(tài)標(biāo)簽的實(shí)現(xiàn)方法

    這篇文章主要介紹了小程序如何寫動(dòng)態(tài)標(biāo)簽的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評論

镇江市| 永安市| 汤原县| 页游| 镇巴县| 阳东县| 京山县| 孝义市| 仁化县| 琼中| 烟台市| 万年县| 观塘区| 南京市| 高青县| 绿春县| 滦平县| 鱼台县| 延安市| 安图县| 富阳市| 松潘县| 昭平县| 上思县| 宁乡县| 北流市| 六安市| 双峰县| 东安县| 会理县| 上饶市| 常德市| 罗山县| 石台县| 南靖县| 平舆县| 诸暨市| 郎溪县| 维西| 峨眉山市| 锡林浩特市|