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

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

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

效果演示

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

步驟一:實現(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>

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

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

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è)置按鈕浮動在右方 */
      /* float: right; */
      /* 默認(rèn)隱藏按鈕 */
      display: none;
      /* 將按鈕向上移動一行 */
      right: 0px;
      bottom: -3px;
      color: skyblue;
      /* 此處背景一定要設(shè)置, */
      background: #fff;
    }

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

    // 元素前方插入文本(因為js沒有無法設(shè)置::before所以,實現(xiàn)了一個元素前方插入文本的方法)
    const addContentBefore = (element, content) => {
      // 創(chuàng)建一個新的偽元素
      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], '...'); // 按鈕前方模擬省略號
          }
        });

        // 為所有按鈕綁定點擊事件
        expandButtons.forEach((button, index) => {
          button.addEventListener('click', () => {
            // 切換文本的max-height屬性,實現(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è)置按鈕浮動在右方 */
      /* float: right; */
      /* 默認(rèn)隱藏按鈕 */
      display: none;
      /* 將按鈕向上移動一行 */
      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)建一個新的偽元素
      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], '...'); // 按鈕前方模擬省略號
          }
        });

        // 為所有按鈕綁定點擊事件
        expandButtons.forEach((button, index) => {
          button.addEventListener('click', () => {
            // 切換文本的max-height屬性,實現(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)于前端實現(xiàn)文本超出指定行數(shù)顯示”展開”和”收起”效果的文章就介紹到這了,更多相關(guān)前端文本超出指定行數(shù)展開收起內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

    JavaScript事件流之事件處理和傳播機制深入理解

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

    js中浮點型運算BUG的解決方法說明

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

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

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

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

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

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

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

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

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

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

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

    JS中的事件委托實例淺析

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

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

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

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

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

最新評論

寿宁县| 赣州市| 永福县| 新丰县| 台安县| 眉山市| 五河县| 会泽县| 辽阳县| 东乡县| 大化| 化州市| 衡山县| 娱乐| 肃南| 富川| 铁岭市| 廊坊市| 冀州市| 满洲里市| 石嘴山市| 台湾省| 肥乡县| 宝鸡市| 平果县| 雷波县| 凌海市| 昂仁县| 海阳市| 新绛县| 马龙县| 宁都县| 阿图什市| 车险| 宜州市| 苗栗县| 凌云县| 建水县| 梁山县| 荥阳市| 巴青县|