Vue实现长文本阅读更多功能

Vue实现长文本阅读更多功能

vue实现

HTML

<div class="sidebar-box" ref="sidebarBox" style="height:auto">
              <p class="jj" ref="p1" style="margin: 0"> {{item.jj}}</p>
              <p class="yj-class" ref="p2" v-html="item.qtms"></p>
              <p v-if="getMoreCon" class="read-more"><a href="#" class="readMoreButton" @click="getMore">阅读更多</a>            </p>
 </div>

CSS

.readMoreButton {
    border-top: 1px solid #96d1f8;
    background: #65a9d7;
    background: -webkit-gradient(linear, left top, left bottom, from(#3e779d), to(#65a9d7));
    background: -moz-linear-gradient(top, #3e779d, #65a9d7);
    padding: 5px 10px;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;


    /* overboard shadows for Opera (and why spec version listed first) */
    box-shadow: rgba(0,0,0,1) 0 1px 0, rgba(0,0,0,90) 0 0 10px, rgba(0,0,0,90) 0 0 20px, rgba(0,0,0,90) 0 0 30px;

    -webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
    -moz-box-shadow: rgba(0,0,0,1) 0 1px 0;

    text-shadow: rgba(0,0,0,.4) 0 1px 0;
    color: white;
    font-size: 14px;
    font-family: Georgia, serif;
    text-decoration: none;
    vertical-align: middle;
  }
  .readMoreButton:hover {
    border-top-color: #28597a;
    background: #28597a;
    color: #ccc;
  }
  .readMoreButton:active {
    border-top-color: #1b435e;
    background: #1b435e;
  }
  .sidebar-box {
    float: left;
    width: 375px;
    margin: 0 20px 0 0;
  }
  .sidebar-box {
   /* max-height: 120px;*/
    position: relative;
    padding: 10px;
    overflow: hidden;
  }
  .sidebar-box .read-more {
    position: absolute;
    bottom: 0; left: 0;
    width: 100%;
    text-align: center;
    margin: 0;
    padding: 30px 0 30px 0;

    /* "transparent" only works here because == rgba(0,0,0,0) */
    background-image: -moz-linear-gradient(top, transparent, black);
    background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, transparent),color-stop(1, #FFFFFF));
  }

JS

//获取数据后
 setTimeout(()=>{
                let sourHeight=this.$refs.p1.offsetHeight+this.$refs.p2.offsetHeight;
                if ((this.docmHeight/2+100)<sourHeight){
                  this.getMoreCon=true
                  this.$refs.sidebarBox.style.height=this.docmHeight/2+100+'px'
                }
              },1)

// 阅读更多按钮
  getMore(){
        this.$refs.sidebarBox.style.height='auto'
        this.getMoreCon=false
      },
 //变量
    data() {
        return {
            getMoreCon:false
        }
    },

jquery实现

原地址https://c.runoob.com/codedemo/5400/

HTML

<script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
<div id="page-wrap">

  
  <div class="sidebar-box gray">
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae</p> <p>ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>				
    <p class="read-more"><a href="#" class="button">阅读更多</a></p>
  </div>
  <div class="sidebar-box red">
    <p>Et netus et malesuada fames ac turpis egestas. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
    <p class="read-more"><a href="#" class="button">阅读更多</a></p>
  </div>

</div>

CSS

* { margin: 0; padding: 0; }
body { font: 14px/1.5 Georgia, serif; color: white; background: black; }

article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }

#page-wrap { width: 960px; margin: 80px auto; }

h1 { box-shadow: 0 0 20px black; }

.button {
  border-top: 1px solid #96d1f8;
  background: #65a9d7;
  background: -webkit-gradient(linear, left top, left bottom, from(#3e779d), to(#65a9d7));
  background: -moz-linear-gradient(top, #3e779d, #65a9d7);
  padding: 5px 10px;
  -webkit-border-radius: 8px;
  -moz-border-radius: 8px;
  border-radius: 8px;


  /* overboard shadows for Opera (and why spec version listed first) */
  box-shadow: rgba(0,0,0,1) 0 1px 0, rgba(0,0,0,90) 0 0 10px, rgba(0,0,0,90) 0 0 20px, rgba(0,0,0,90) 0 0 30px;

  -webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
  -moz-box-shadow: rgba(0,0,0,1) 0 1px 0;

  text-shadow: rgba(0,0,0,.4) 0 1px 0;
  color: white;
  font-size: 14px;
  font-family: Georgia, serif;
  text-decoration: none;
  vertical-align: middle;
}
.button:hover {
  border-top-color: #28597a;
  background: #28597a;
  color: #ccc;
}
.button:active {
  border-top-color: #1b435e;
  background: #1b435e;
}
p { margin: 0 0 15px 0; }
.sidebar-box { 
  float: left; 
  width: 250px;
  margin: 0 20px 0 0;
}
.sidebar-box {
  max-height: 120px;
  position: relative;
  padding: 20px;
  overflow: hidden;
}
.sidebar-box .read-more { 
  position: absolute; 
  bottom: 0; left: 0;
  width: 100%; 
  text-align: center; 
  margin: 0; 
  padding: 30px 0 30px 0; 

  /* "transparent" only works here because == rgba(0,0,0,0) */ 
  background-image: -moz-linear-gradient(top, transparent, black);
  background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, transparent),color-stop(1, black));
}
.gray {
  background-color: #444;
  background-color: rgb(89,89,89);
}
.red {
  background-color: red;
}
.red .read-more { 
  /* transparent doesn't work in this context, must use RGBa for both */
  background-image: -moz-linear-gradient(top, rgba(255,0,0,0), rgba(255,0,0,100));
  background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, rgba(255,0,0,0)),color-stop(1, rgba(255,0,0,100)));
}

JS

// DOM Ready
$(function() {

  var $el, $ps, $up, totalHeight;

  $(".sidebar-box .button").click(function() {

    // IE 7 doesn't even get this far. I didn't feel like dicking with it.

    totalHeight = 0

    $el = $(this);
    $p  = $el.parent();
    $up = $p.parent();
    $ps = $up.find("p:not('.read-more')");

    // measure how tall inside should be by adding together heights of all inside paragraphs (except read-more paragraph)
    $ps.each(function() {
      totalHeight += $(this).outerHeight();
      // FAIL totalHeight += $(this).css("margin-bottom");
    });

    $up
      .css({
      // Set height to prevent instant jumpdown when max height is removed
      "height": $up.height(),
      "max-height": 9999
    })
      .animate({
      "height": totalHeight
    });

    // fade out read-more
    $p.fadeOut();

    // prevent jump-down
    return false;

  });

});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值