当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue实现文章内容过长点击阅读全文功能的实例

vue实现文章内容过长点击阅读全文功能的实例

2018年02月13日  | 移动技术网IT编程  | 我要评论

花都人事局,潍坊在线,芒果tv会员

直接上代码:

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;
 }
 },
}

效果如图:

实现思路与注意的点:

1、获取内容的高度要等到dom加载完成之后,在mounted里加一个settimeout函数,保证能真正获取到

2、当内容的高度高于自己设定的要展示的高度的时候,则只限定在自己要展示的高度中,加一个class解决,注意要overflow:hidden;

以上这篇vue实现文章内容过长点击阅读全文功能的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网