{{item.jj}}

当前位置:   article > 正文

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

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>
  • 1
  • 2
  • 3
  • 4
  • 5

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));
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

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
        }
    },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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)));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

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;

  });

});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/76803
推荐阅读
相关标签
  

闽ICP备14008679号