欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端實(shí)現(xiàn)文本溢出展開和收起功能

 更新時間:2025年04月15日 11:16:21   作者:小周同學(xué)_丶  
在現(xiàn)代網(wǎng)頁設(shè)計(jì)中,文本是網(wǎng)頁中最重要的內(nèi)容之一,然而,當(dāng)文本超出其容器的大小時,會發(fā)生文本溢出的問題,文本溢出不僅會影響網(wǎng)頁的視覺效果,還會影響網(wǎng)頁的可讀性和可用性,所以本文給大家介紹了前端實(shí)現(xiàn)文本溢出展開和收起功能的方法,需要的朋友可以參考下

判斷文本溢出

眾所周知,單行文本溢出打點(diǎn)僅需要:

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

而單行文本判斷是否溢出的關(guān)鍵就是element.scrollWidth > element.clientWidth

需要注意的是,當(dāng)使用以上方式判斷的時候,不要給元素加上overflow: hidden的樣式,不然獲取的clientWidth一直都是等于scrollWidth。

示例

先看下元素結(jié)構(gòu):

<div class="wrapper">
  <div class="text">
    Lorem ipsum dolor sit amet consectetur adipisicing elit. nulla facere
    obcaecati consequatur quisquam adipisci veritatis! Deserunt nostrum
    doloribus minima voluptatum labore.
    <span class="more">展開</span>
    <span class="collapse">收起</span>
  </div>
</div>

文本在.text的元素中,里面有展開收起兩個按鈕

再寫點(diǎn)樣式:

.wrapper {
  width: fit-content;
  height: fit-content;
  border-radius: 8px;
  border: 1px solid #00bfbf;
  padding: 8px;
}
 
.text {
  width: 300px;
  font-size: 14px;
  line-height: 20px;
}

于是就得到如下圖所示的展示效果:

初始化將展開/收起按鈕隱藏 

/* 展開/收起按鈕初始隱藏 */
.text .more {
  display: none;
}
.text .collapse {
  display: none;
}

要使用scrollWidth去判斷文本是否溢出,關(guān)鍵需要給.text添加white-space: nowrap;

當(dāng)需要給.text元素添加單行文本溢出的3行代碼,不要直接添加到.text類名下(直接寫overflow: hidden就不能使用scrollWidth判斷文本溢出了)

另加一個類名,比如:.ellipsis,然后使用js判斷文本是否溢出,再給該元素添加該類名。

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

讓.more按鈕僅在.ellipsis下展示,再給.more按鈕寫點(diǎn)樣式,css代碼如下:

/* 溢出 */
.text.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  position: relative;
}
/* 文字溢出 - 展開按鈕 */
.text.ellipsis .more {
  display: block;
}
 
.more {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  display: block;
  color: #00bfbf;
  background-color: #fff;
  font-size: 14px;
  line-height: 20px;
  width: fit-content;
  cursor: pointer;
}
 
.more::after {
  content: "";
  display: block;
  position: absolute;
  height: 20px;
  width: 60px;
  right: 28px;
  top: 0;
  background: linear-gradient(
    to right,
    rgba(255, 255, 255, 0),
    rgba(255, 255, 255, 0.6),
    rgba(255, 255, 255, 1)
  );
}

就得到以下效果:

js判斷文本溢出如下:

const isTextOverflowX = (elem) => {
  return text.clientWidth < text.scrollWidth;
};
 
const text = document.getElementsByClassName("text")[0];
 
const isTextOverflow = isTextOverflowX(text);
 
if (isTextOverflow) {
  text.classList.add("ellipsis");
}

判斷文本溢出后,才會給文字添加overflow: hidden,為了避免頁面文字閃爍,給初始文本元素添加opacity: 0,在判斷完畢后,設(shè)置opacity: 1

修改一下css,js代碼

.text {
  ...
  white-space: nowrap;
  opacity: 0;
}
 
/* 未溢出 */
.text.normal {
  white-space: unset; // 讓文字正常換行
  opacity: 1;
}
/* 溢出 */
.text.ellipsis {
  ...
  opacity: 1;
}
if (isTextOverflow) {
  text.classList.add("ellipsis");
} else {
  text.classList.add("normal");
}

分別給展開、收起按鈕添加點(diǎn)擊事件,事件僅需要添加、刪除類名即可

const more = document.getElementsByClassName("more")[0];
more.addEventListener("click", () => {
  text.classList.remove("ellipsis");
  text.classList.add("expand");
});
 
const collapse = document.getElementsByClassName("collapse")[0];
collapse.addEventListener("click", () => {
  text.classList.remove("expand");
  text.classList.add("ellipsis");
});

這里又加了個新類名.expand為了控制文本展開后的按鈕顯示隱藏

/* 文本展開 */
.text.expand {
  white-space: unset;
  opacity: 1;
}
/* 文本展開 - 收起按鈕 */
.text.expand .collapse {
  display: inline-block;
}

最終效果如下:

完整代碼

HTML: 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>文字溢出隱藏</title>
    <link rel="stylesheet" href="./index.css" rel="external nofollow"  />
    <script src="./index.js" defer></script>
  </head>
  <body>
    <div class="wrapper">
      <div class="text">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Nostrum animi
        cum consequuntur beatae, culpa impedit excepturi fuga, nulla facere
        obcaecati consequatur quisquam adipisci veritatis! Deserunt nostrum
        doloribus minima voluptatum labore.
        <span class="more">展開</span>
        <span class="collapse">收起</span>
      </div>
    </div>
  </body>
</html>

CSS: 

.wrapper {
  width: fit-content;
  height: fit-content;
  border-radius: 8px;
  border: 1px solid #00bfbf;
  padding: 8px;
  margin: 30px auto;
}
 
.text {
  width: 300px;
  font-size: 14px;
  line-height: 20px;
  white-space: nowrap;
  opacity: 0;
}
/* 展開/收起按鈕初始隱藏 */
.text .more {
  display: none;
}
.text .collapse {
  display: none;
}
 
/* 未溢出 */
.text.normal {
  white-space: unset;
  opacity: 1;
}
/* 溢出 */
.text.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  position: relative;
  opacity: 1;
}
/* 文字溢出 - 展開按鈕 */
.text.ellipsis .more {
  display: block;
}
 
/* 文本展開 */
.text.expand {
  white-space: unset;
  opacity: 1;
}
/* 文本展開 - 收起按鈕 */
.text.expand .collapse {
  display: inline-block;
}
 
.more {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  display: block;
  color: #00bfbf;
  background-color: #fff;
  font-size: 14px;
  line-height: 20px;
  width: fit-content;
  cursor: pointer;
}
 
.more::after {
  content: "";
  display: block;
  position: absolute;
  height: 20px;
  width: 60px;
  right: 28px;
  top: 0;
  background: linear-gradient(
    to right,
    rgba(255, 255, 255, 0),
    rgba(255, 255, 255, 0.6),
    rgba(255, 255, 255, 1)
  );
}
 
.collapse {
  color: #00bfbf;
  cursor: pointer;
}

JS: 

const isTextOverflowX = (elem) => {
  return text.clientWidth < text.scrollWidth;
};
 
const text = document.getElementsByClassName("text")[0];
 
const isTextOverflow = isTextOverflowX(text);
 
if (isTextOverflow) {
  text.classList.add("ellipsis");
} else {
  text.classList.add("normal");
}
 
const more = document.getElementsByClassName("more")[0];
more.addEventListener("click", () => {
  text.classList.remove("ellipsis");
  text.classList.add("expand");
});
 
const collapse = document.getElementsByClassName("collapse")[0];
collapse.addEventListener("click", () => {
  text.classList.remove("expand");
  text.classList.add("ellipsis");
});
 

拓展

多行文本溢出

多行文本展開收起的思路一樣的

需要修改下文本溢出判斷函數(shù),使用clientHeightscrollHeight判斷:

const isTextOverflowY = (elem) => {
  return text.clientHeight < text.scrollHeight;
};

.ellipsis溢出css修改為多行文本溢出打點(diǎn)即可

.ellipsis {
    overflow : hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

以上就是前端實(shí)現(xiàn)文本溢出展開和收起功能的詳細(xì)內(nèi)容,更多關(guān)于前端文本溢出展開收起的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 利用JavaScript模擬京東按鍵輸入功能

    利用JavaScript模擬京東按鍵輸入功能

    這篇文章主要給大家介紹了關(guān)于如何利用JavaScript模擬京東按鍵輸入功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Django 中的templates使用示例詳解

    Django 中的templates使用示例詳解

    Django模板系統(tǒng)提供了一套豐富的工具集,能夠幫助開發(fā)者將動態(tài)數(shù)據(jù)有效地渲染到HTML頁面中,它通過特殊的語法支持變量插入、邏輯標(biāo)簽和過濾器的使用,本文給大家介紹Django 中的templates使用,感興趣的朋友一起看看吧
    2024-10-10
  • javascript中encodeURI和decodeURI方法使用介紹

    javascript中encodeURI和decodeURI方法使用介紹

    encodeURI和decodeURI是成對來使用的,因?yàn)闉g覽器的地址欄有中文字符的話,可以會出現(xiàn)不可預(yù)期的錯誤,所以可以encodeURI把非英文字符轉(zhuǎn)化為英文編碼,decodeURI可以用來把字符還原回來
    2013-05-05
  • ES6代碼轉(zhuǎn)ES5詳細(xì)教程(babel安裝使用教程)

    ES6代碼轉(zhuǎn)ES5詳細(xì)教程(babel安裝使用教程)

    Babel 是一個廣泛使用的 ES6 轉(zhuǎn)碼器,可以將 ES6 代碼轉(zhuǎn)為 ES5 代碼,從而在老版本的瀏覽器執(zhí)行,這意味著,你可以用 ES6 的方式編寫程序,又不用擔(dān)心現(xiàn)有環(huán)境是否支持,這篇文章主要介紹了ES6代碼轉(zhuǎn)ES5教程(babel安裝使用教程),需要的朋友可以參考下
    2023-01-01
  • 現(xiàn)代 JavaScript 開發(fā)編程風(fēng)格Idiomatic.js指南中文版

    現(xiàn)代 JavaScript 開發(fā)編程風(fēng)格Idiomatic.js指南中文版

    下面的章節(jié)描述的是一個 合理 的現(xiàn)代 JavaScript 開發(fā)風(fēng)格指南,并非硬性規(guī)定。其想送出的核心理念是高度統(tǒng)一的代碼風(fēng)格(the law of code style consistency)。
    2014-05-05
  • 十分鐘教你上手ES2020新特性

    十分鐘教你上手ES2020新特性

    這篇文章主要介紹了十分鐘教你上手ES2020新特性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • javascript加載導(dǎo)出3mf文件

    javascript加載導(dǎo)出3mf文件

    3MF是一種開放標(biāo)準(zhǔn)的文件格式,專門用于三維制造和打印,這篇文章主要介紹了如何使用JavaScript實(shí)現(xiàn)加載導(dǎo)出3mf文件,感興趣的可以了解下
    2024-11-11
  • JavaScript前端實(shí)現(xiàn)快照的示例代碼

    JavaScript前端實(shí)現(xiàn)快照的示例代碼

    snapshot 翻譯為快照,用于直觀獲取頁面在某個運(yùn)行時的狀態(tài),本文主要為大家詳細(xì)介紹 snapshot 工具實(shí)現(xiàn)的原理,以及其在項(xiàng)目中的使用,需要的可以參考下
    2023-11-11
  • js實(shí)現(xiàn)AI五子棋人機(jī)大戰(zhàn)

    js實(shí)現(xiàn)AI五子棋人機(jī)大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)AI五子棋人機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • javascript 組合按鍵事件監(jiān)聽實(shí)現(xiàn)代碼

    javascript 組合按鍵事件監(jiān)聽實(shí)現(xiàn)代碼

    這篇文章主要介紹了javascript 組合按鍵事件監(jiān)聽實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評論