vue实现超过两行显示展开收起

vue实现超过两行显示展开收起

基于vue-cli2,sass,vant(ui组件):https://youzan.github.io/vant/#/zh-CN/home

具体代码如下:

<template>
  <div>
    <div class="group">
      <div class="text more" ref="more">
        占位
      </div>
      <div class="list" v-for="(item, index) in historyList" :key="index">
        <van-row>
          <van-col span="12">{{ item.version }}</van-col>
          <van-col class="t_right l_text" span="12">{{ item.time }}</van-col>
        </van-row>
        <div class="l_title">{{ item.title }}</div>
        <div
          class="text"
          ref="textContainer"
          :class="{ retract: item.status }"
          :style="{ 'max-height': item.status ? textHeight : '' }"
        >
          {{ item.content }}
        </div>
        <span
          v-if="item.status !== null"
          class="link"
          @click="more(index)"
          >{{ item.status ? "展开" : "收起" }}</span
        >
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      textHeight: '',
      historyList: [
        {
          version: '7.1.4',
          title: '本次更新',
          content:
            '-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐;-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
          time: '2周前'
        },
        {
          version: '7.1.4',
          title: '本次更新',
          content:
            '-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
          time: '5周前'
        },
        {
          version: '7.1.4',
          title: '本次更新',
          content:
            '-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐;-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
          time: '6周前'
        },
        {
          version: '7.1.4',
          title: '本次更新',
          content:
            '-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐',
          time: '9周前'
        }
      ]
    }
  },
  mounted () {
    this.historyList.forEach((ele, index) => {
      this.$set(
        this.historyList,
        index,
        Object.assign({}, ele, { status: null })
      )
    })
    // DOM 加载完执行
    this.$nextTick(() => {
      this.calculateText()
      //console.log(this.historyList)
    })

    window.onresize = () => {
      this.historyList.forEach((ele, index) => {
        this.$set(
          this.historyList,
          index,
          Object.assign({}, ele, { status: null })
        )
      })
      setTimeout(() => {
        this.calculateText()
      }, 0)
    }
  },
  methods: {
    // 计算文字 显示展开 收起
    calculateText () {
      // 获取一行文字的height 计算当前文字比较列表文字
      let oneHeight = this.$refs.more.scrollHeight
      let twoHeight = oneHeight * 2 || 40
      this.textHeight = `${twoHeight}px`
      let txtDom = this.$refs.textContainer
      for (let i = 0; i < txtDom.length; i++) {
        let curHeight = txtDom[i].offsetHeight
        if (curHeight > twoHeight) {
          this.$set(
            this.historyList,
            i,
            Object.assign({}, this.historyList[i], { status: true })
          )
        } else {
          this.$set(
            this.historyList,
            i,
            Object.assign({}, this.historyList[i], { status: null })
          )
        }
      }
    },
    more (index) {
      this.historyList[index].status = !this.historyList[index].status
    }
  }
}
</script>
<style lang="scss" scoped>
.group {
  .list {
    padding: 5px 0;
    border-bottom: 1px solid #eaeaea;
  }
  .text {
    position: relative;
    color: #000;
    font-size: 14px;
  }
  .more {
    visibility: hidden;
  }
  .link {
    font-size: 12px;
    color: #2d95fe;
  }
  .retract {
    position: relative;
    overflow: hidden;
  }

  .retract:after {
    content: "...";
    position: absolute;
    bottom: 0;
    right: 2px;
    width: 25px;
    padding-left: 25px;
    background: linear-gradient(to right, transparent, #fff 45%);
  }
}
</style>

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Vue 的计算属性和 CSS 样式来实现这个功能。 1. 首先,您需要将文本内容存储在一个 Vue data 属性中: ```js data() { return { text: '这是一段超过三行的文本内容,需要显示展开按钮' } } ``` 2. 接下来,您可以编写一个计算属性来确定文本是否超过三行: ```js computed: { isTextOverflow() { const element = document.createElement('div') element.innerText = this.text element.style.position = 'absolute' element.style.visibility = 'hidden' element.style.height = 'auto' element.style.width = '300px' // 假设文本宽度为 300px document.body.appendChild(element) const isOverflow = element.offsetHeight > 3 * parseFloat(window.getComputedStyle(element).lineHeight) document.body.removeChild(element) return isOverflow } } ``` 此计算属性将创建一个 div 元素,将文本内容插入其中,然后将其添加到页面中以获取其高度。然后,它将检查文本是否超过三行,通过计算元素的高度和行高来实现。最后,它将从页面中删除元素,并返回一个布尔值,指示文本是否超过三行。 3. 最后,您可以使用 v-if 指令根据计算属性的返回值来显示或隐藏展开按钮: ```html <div> <p :class="{ 'text-overflow': isTextOverflow }">{{ text }}</p> <button v-if="isTextOverflow">展开</button> </div> ``` 在此示例中,我们为文本段落添加了一个名为 "text-overflow" 的 CSS 类,以便在文本超过三行时将其截断。我们还使用 v-if 指令来确定是否应显示展开按钮。 最后,您可以使用 CSS 样式来设置文本段落的截断和展开按钮的样式: ```css .text-overflow { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; } button { background-color: #007bff; color: #fff; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer; } ``` 这将通过使用 -webkit-box 属性将文本段落截断为三行,并使用省略号来表示文本的截断。展开按钮将具有蓝色背景和白色文本,并具有一些基本的边框半径和内边距。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值