最近一个朋友,要写这个小功能,问到了我,我就也写下

代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style type="text/css" media="screen">
     .pd{height:300px;width:300px;overflow:hidden;border:1px solid #000;}
  </style>
</head>
<body>
  <div id="app">
    <div class='pd' :style="{height:height1}">
      <div class="txt" ref = 'txt'>
         {{dataText}}
      </div>
    </div>
    <span @click="showMore">{{txt}}</span>
  </div>
  <script src="https://unpkg.com/vue"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    var app = new Vue({
      el: '#app',
      data(){
        return{
          isShow: true,
          dataText:"",
          txt: '',
          height1:"",
          vultrHeight:""
        }
      },
      methods: {
        showMore(){
          console.log(this.isShow)
          if(this.isShow){
             this.isShow = false
              this.height1 = "300px"
               this.txt = '显示全部'
          }else{
            this.isShow = true
            this.height1 = this.vultrHeight+"px"
            console.log(this.vultrHeight+"---px")
            this.txt = '收起'
          }
        }
      },
      created: function () {
        let that = this
        axios.get('https://interface.meiriyiwen.com/article/today?dev=1')
        .then(function (response) {
          console.log(response.data.data.content);
          that.dataText = response.data.data.content
          that.$nextTick(()=>{
                console.log('dom update')
                that.isShow = false
                that.vultrHeight = that.$refs.txt.offsetHeight
                console.log(that.vultrHeight+"px")
                if(that.vultrHeight>300){
                    that.txt = "查看全部"
                  }else{
                    that.txt=""
                  }
              })
        })
        .catch(function (error) {
          console.log(error);
        });
      }
    
    })
  </script>
</body>
</html>

codepen

See the Pen read more by extra (@it5200) on CodePen.


扫描二维码,在手机上阅读!