JS文字超出规定长度,点击按钮展开与收缩;

文字太长,点击向下的箭头(按钮),展开文字, 箭头变成向上箭头,点击这个向上的箭头,收缩,同时箭头变成向下。
箭头是用两张图片来完成的,有些是展开与收起的文字按钮,写法相似。

<template>
  <div class="box">
    <p class="title"></p>
    <p class="sug" :class="{ close: infoLength > 1 && !more }">
      <span ref="info">秋高气爽,桂花飘香,收获的季节,看到人们都在忙忙碌碌的收获属于自己的果实,也许
        就是应了那句话,春种一棵树,秋收万颗子。
      </span>
      <span :class="{ absolute: !more }" v-if="infoLength > 1"  @click="lookMore">
        <img v-show="more" src="../static/icon/up.png" alt="">
        <img v-show="!more" src="../static/icon/down.png" alt="">
      </span>
    </p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      infoLength: '',
      more: false
    };
  },
  methods: {
    lookMore() {
      this.more = !this.more;
    },
    // 获取info行数
    getLineNum() {
      // getClientRects()获取元素占据页面的所有矩形区域
      this.infoLength = this.$refs.info.getClientRects().length;
    }
  },
  mounted() {
    this.getLineNum();
  },
};
</script>
<style lang="scss" scoped>
.box {
  margin: 20px 0;
  padding: 24px;
 .title{
   
 }
  .sug {
    padding: 0 px2rem(30) 0 px2rem(47);
    font-size: 24px;
    color: darkblue;
    line-height: px2rem(40);
    position: relative;
    img {
      vertical-align:middle;
      width: 12px;
      height: 12px;
    }
    .absolute {
      position: absolute;
      top: 0;
      right: 4px;
    }
  }
  .close {
    overflow: hidden; //超出部分隐藏
    text-overflow: ellipsis; //当对象内文本溢出时显示省略标记(...)
    display: -webkit-box; //弹性伸缩盒子模型显示
    -webkit-box-orient: vertical; // 甚至伸缩盒对象的子元素排列方式
    -webkit-line-clamp: 1; // 限制一个块元素显示文本的行数
  }
}
</style>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值