vue實現(xiàn)文章內(nèi)容過長點擊閱讀全文功能的實例
更新時間:2017年12月28日 08:37:42 作者:蓓蕾心晴
下面小編就為大家分享一篇vue實現(xiàn)文章內(nèi)容過長點擊閱讀全文功能的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
直接上代碼:
html:
<div class="bodyFont clearfloat" id="bodyFont" ref="bodyFont" :class="{bodyHeight:contentStatus}"> <div v-html="content"></div> </div> <div class="contentToggle" v-if="contentStatus" @click="contentStatus=!contentStatus">閱讀全文</div>
css:
.bodyFont{ .font-dpr(16px); color: #333; text-align: left; line-height:58px; word-break:break-all; word-wrap:break-word; padding-bottom: 30px; height:auto; overflow: hidden; max-height: 100%; p{margin:16px 0 0 0;} } .bodyHeight{ height:5000px; } .contentToggle{ height:60px; line-height:60px; text-align: center; color:@red; border:1px solid @red; border-radius: 5px; .font-dpr(14px); margin-bottom:30px; }
js:
data(){ return { contentStatus:false, curHeight:0, bodyHeight:5000 } }, mounted(){ setTimeout(()=>{ this.contentToggle(); },500) }, methods:{ contentToggle(){ this.curHeight=this.$refs.bodyFont.offsetHeight; if(this.curHeight>this.bodyHeight){ this.contentStatus=true; }else{ this.contentStatus=false; } }, }
效果如圖:
實現(xiàn)思路與注意的點:
1、獲取內(nèi)容的高度要等到dom加載完成之后,在mounted里加一個setTimeout函數(shù),保證能真正獲取到
2、當(dāng)內(nèi)容的高度高于自己設(shè)定的要展示的高度的時候,則只限定在自己要展示的高度中,加一個class解決,注意要overflow:hidden;
以上這篇vue實現(xiàn)文章內(nèi)容過長點擊閱讀全文功能的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項目中使用particles實現(xiàn)粒子背景效果及遇到的坑(按鈕沒有點擊響應(yīng))
為了提高頁面展示效果,登錄界面內(nèi)容比較單一的,粒子效果作為背景經(jīng)常使用到,vue工程中利用vue-particles可以很簡單的實現(xiàn)頁面的粒子背景效果,本文給大家分享在實現(xiàn)過程中遇到問題,需要的朋友一起看看吧2020-02-02