js多行省略兼容常用浏览器

<template>

  <div id="app1">


    <p ref="word" :id="'overflowhidden'+index" v-for="(item,index) in data">{{subWord('overflowhidden'+index, 3, item)}}</p>


  </div>
</template>

<script>
  export default {
    name: 'HelloWorld',
    data() {
      return {
        data: null,
      }

    },
    created() {

    },
    mounted() {

      let self = this;
      setTimeout(function () {
        self.data = ['S处理的基本思路是获取到文本中设置的line-height属性。在获取到相关属性后,根据传入的显示行数来计算整个文本的高度。A package is a combination of metadata, configuration, and software that is prepared in a way that a package management program (for example: apt on Ubuntu, yum on Redhat Enterprise Linux, pip for Python packages and gem for RubyGems) can use to properly and reliably install software and related configuration data on a computer.', 'h处理的基本思路是获取到文本中设置的line-height属性。在获取到相关属性后,根据传入的显示行数来计算整个文本的高度。A package is a combination of metadata, configuration, and software that is prepared in a way that a package management program (for example: apt on Ubuntu, yum on Redhat Enterprise Linux, pip for Python packages and gem for RubyGems) can use to properly and reliably install software and related configuration data on a computer.', 'k处理的基本思路是获取到文本中设置的line-height属性。在获取到相关属性后,根据传入的显示行数来计算整个文本的高度。A package is a combination of metadata, configuration, and software that is prepared in a way that a package management program (for example: apt on Ubuntu, yum on Redhat Enterprise Linux, pip for Python packages and gem for RubyGems) can use to properly and reliably install software and related configuration data on a computer.'];
      }, 1000)
    },
    methods: {

      subWord(id, rows, str) {
        if (!str || str == '') {
          return ''
        }
        this.$nextTick(function () {
          var text = document.getElementById(id);
          var lineHeight = 24;   //获取到line-height样式设置的值 必须要有
          var at = rows * parseInt(lineHeight);      //计算包含文本的div应该有的高度
          var tempstr = str;                       //获取到所有文本
          text.innerHTML = tempstr;                //将所有文本写入html中
          var len = tempstr.length;
          var i = 0;
          if (text.offsetHeight <= at) {             //如果所有文本在写入html后文本没有溢出,那不需要做溢出处理
            /*text.innerHTML = tempstr;*/
          }
          else {                                   //否则 一个一个字符添加写入 不断判断写入后是否溢出
            var temp = "";
            text.innerHTML = temp;

            while (text.offsetHeight <= at) {

              temp = tempstr.substring(0, i + 1);
              i++;
              text.innerHTML = temp;
              /*console.log(temp,text.offsetHeight , at)*/
            }
            var slen = temp.length;
            tempstr = temp.substring(0, slen - 1);
            len = tempstr.length
            text.innerHTML = tempstr.substring(0, len - 3) + "...";     //替换string后面三个字符
            text.height = at + "px";                                  //修改文本高度 为了让CSS样式overflow:hidden生效
          }
        })


      },

    },
  }
</script>

<style scoped lang="less">
  * {
    padding: 0;
    margin: 0;
  }

  * {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }

  a {
    text-decoration: none;
  }

  .clearfix:before,
  .clearfix:after {
    content: ' ';
    display: table;
  }

  .clearfix:after {
    clear: both;
  }

  input {
    outline: none;
  }

  ol,
  ul {
    list-style: none;
  }

  body {
    padding: 20px;
  }

  #app1 {
    line-height: 24px;
    p {
      margin: 40px auto;
      width: 400px;
      line-height: 24px;
      font-size: 12px;
    }
  }

</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值