最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 22:22:43
文档

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

vue实现文章内容过长点击阅读全文功能的实例:直接上代码: html: <div class=bodyFont clearfloat id=bodyFont ref=bodyFont :class={bodyHeight:contentStatus}> <div v-html=content></div> </div> <
推荐度:
导读vue实现文章内容过长点击阅读全文功能的实例:直接上代码: html: <div class=bodyFont clearfloat id=bodyFont ref=bodyFont :class={bodyHeight:contentStatus}> <div v-html=content></div> </div> <


直接上代码:

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实现文章内容过长点击阅读全文功能的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

文档

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

vue实现文章内容过长点击阅读全文功能的实例:直接上代码: html: <div class=bodyFont clearfloat id=bodyFont ref=bodyFont :class={bodyHeight:contentStatus}> <div v-html=content></div> </div> <
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top