溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Html5中怎么實(shí)現(xiàn)溢出部分顯示省略號(hào)功能

發(fā)布時(shí)間:2022-04-25 10:33:29 來源:億速云 閱讀:134 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“Html5中怎么實(shí)現(xiàn)溢出部分顯示省略號(hào)功能”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

利用多行溢出實(shí)現(xiàn)“展開”“收起”

多行溢出省略Css:

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

設(shè)計(jì)是這樣的。。

Html5中怎么實(shí)現(xiàn)溢出部分顯示省略號(hào)功能

點(diǎn)擊展開.png

Html5中怎么實(shí)現(xiàn)溢出部分顯示省略號(hào)功能

點(diǎn)擊收起.png

接下來我來講講我的實(shí)現(xiàn)

<div class="review">
 孫燕姿導(dǎo)師評(píng)語:這位同學(xué)唱功基礎(chǔ)扎實(shí),轉(zhuǎn)音和高低音的切換非常自然,整首歌曲感情飽滿,非常不錯(cuò)。整首歌曲感情飽滿,非常不錯(cuò)。整首歌曲感情飽滿,非常不錯(cuò)。
</div>
<style>
.review{
  padding-top: .2rem;
  padding-bottom: .1rem;
  margin-left: .9rem;
  font-size: .32rem;
  color:#b85423;
  line-height: 1.5em;
  position: relative;
}
.ellipsis{
  overflow : hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.spread{
  padding-right: .2rem;
  position: absolute;
  bottom: .09rem;
  right:0;

}
.spread i{
  width: .2266rem;
  height:.2266rem;
  display: inline-block;
  vertical-align: middle;
  background-image: url(/cdn_img/plusSprite.png);
  background-repeat: no-repeat;
  background-size: .2266rem .72rem;
}
.spread i.plus{
  background-position: 0 0;
}
.spread i.reduce{
  background-position: 0 -0.4933rem;
}
</style>
$('.teacher-review').forEach(function(v,i){
  // 引入flexible.js 進(jìn)行移動(dòng)端適配 
  // 根據(jù)flexible 獲取rem 即 window.rem
  if(v.clientHeight > (1.27*window.rem)){
    var el = document.createElement('span');
    el.innerHTML = '... <i class="plus"></i>點(diǎn)擊展開';
    el.className = 'spread';
    // 由于每條評(píng)論相間背景
    el.style.backgroundColor = i%2 == 0 ? '#ffca48' : '#ffd358';
    v.appendChild(el);
    // multi 是顯示溢出的標(biāo)志
    $(v).addClass('ellipsis multi') 
  }
})
// 點(diǎn)擊判斷收起還是展開
$('.review').on('click','.multi',function(e){
  var $this = $(this)
  if($this.hasClass('ellipsis')){
    $this.removeClass('ellipsis').find('span').html('<i class="reduce"></i>點(diǎn)擊收起');
  }else{
    $this.addClass('ellipsis').find('span').html('... <i class="plus"></i>點(diǎn)擊展開');
  }
})

結(jié)果是這樣的。。。。

Html5中怎么實(shí)現(xiàn)溢出部分顯示省略號(hào)功能

這里講解下展開收起的思路:

出現(xiàn)展開的情況是因?yàn)閮?nèi)容溢出,那內(nèi)容沒溢出就不需要顯示“點(diǎn)擊展開的按鈕啦”,好心煩,突然接到需求,都沒心情熱飯吃。。

熱飯過程中靈感一閃,有了!判斷下內(nèi)容的高度,只要大于指定的高度就能解決溢出顯示“點(diǎn)擊展開”,不溢出就不顯示。

這里的multi class 是為了區(qū)分內(nèi)容溢出還是沒溢出~~~~

如果內(nèi)容顯示溢出就加個(gè)ellipsis class,一旦點(diǎn)擊,判斷存在ellipsis class 說明內(nèi)容溢出啦 ,這時(shí)候移除ellipsis class 就展開內(nèi)容了 是不是很簡單。 剩下的就不說了,大家應(yīng)該都明白了~~

“Html5中怎么實(shí)現(xiàn)溢出部分顯示省略號(hào)功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI