欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue 折疊展示多行文本組件的實(shí)現(xiàn)代碼

 更新時間:2021年10月13日 15:53:11   作者:喬路非  
這篇文章主要介紹了vue 折疊展示多行文本組件,自動根據(jù)傳入的expand判斷是否需要折疊,非常完美,文章通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

折疊展示多行文本組件

折疊展示多行文本組件,自動根據(jù)傳入的expand判斷是否需要折疊
兩種模式:展開/收起展示全文本(默認(rèn))、popover展示全文本

先上代碼

<template>
  <div class="text-expand" ref="textExpand">
    <div v-if="!(showPopover && showPopoverJudge)">
      <span class="text-expand-content" :style="expandStyle">
        {{ (text === null || text === undefined || text === '') ? '--' : text }}
      </span>
      <div class="expander">
        <span
          v-if="showBtn && showBtnJudge"
        >
          <span
            v-if="!showFull"
            class="action action-expand"
            @click.stop="showFullFn(true)"
          >
            展開
            <i v-if="showBtnIcon" class="iconfont iconxiajiantou" />
          </span>
          <span
            v-else
            class="action action-pack"
            @click.stop="showFullFn(false)"
          >
            收起
            <i v-if="showBtnIcon" class="iconfont iconshangjiantou" />
          </span>
        </span>
      </div>
    </div>
    <el-popover
      v-else
      :placement="popoverPlace"
      trigger="hover">
      <div class="popover-content">
        {{ text }}
      </div>
      <span class="text-expand-content" :style="expandStyle" slot="reference">{{ text }}</span>
    </el-popover>
  </div>
</template>
<script>
export default {
  name: "TextExpand",
  props: {
    text: { // 文本內(nèi)容
      type: String,
      default: () => ''
    },
    expand: { // 折疊顯示行數(shù)
      type: Number,
      default: () => 3
    },
    showBtn: { // 展開、折疊按鈕
      type: Boolean,
      default: true
    },
    showBtnIcon: { // 展開、折疊icon
      type: Boolean,
      default: true
    },
    showPopover: { // popover顯示全文本
      type: Boolean,
      default: false
    },
    popoverPlace: { // popover位置
      type: String,
      default: 'bottom'
    }
  },
  data () {
    return {
      showFull: false, // 是否展示全文本
      expandStyle: '',
      showBtnJudge: false, // 判斷是否需要折疊展示按鈕
      showPopoverJudge: false // 判斷是否需要折疊展示popover
    }
  },
  watch: {
    text: function (val) {
      this.judgeExpand()
    }
  },
  mounted () { 
    this.judgeExpand()
  },
  methods: {
    showFullFn (value) {
      this.expandStyle = value ? '' : `display: -webkit-box;word-break: break-all;-webkit-line-clamp: ${this.expand};-webkit-box-orient: vertical;text-overflow: ellipsis;overflow: hidden;`
      this.showFull = value
    },
    judgeExpand () { // 判斷是否需要折疊
      this.$nextTick(() => {
        const { expand } = this;
        const textExpandStyle = window.getComputedStyle(this.$refs.textExpand)
        const textExpandHeight = parseFloat(textExpandStyle.height) //獲取總高度
        const textExpandLineHeight = parseFloat(textExpandStyle.lineHeight) //獲取行高
        // 計算行高
        const rects = Math.ceil(textExpandHeight / textExpandLineHeight)
        if (rects <= expand) { // 不需要折疊展示
          this.showBtnJudge = false
          this.showPopoverJudge = false
        } else {
          this.showBtnJudge = true
          this.showPopoverJudge = true
          this.expandStyle = `display: -webkit-box;word-break: break-all;-webkit-line-clamp: ${this.expand};-webkit-box-orient: vertical;text-overflow: ellipsis;overflow: hidden;`
        }
      })
    }

  }
}
</script>
<style lang="less" scoped>
.text-expand{
  &-content{
    word-break: break-all;
    white-space: pre-wrap;
  }
  .expander {
    text-align: left;
    margin-top: 6px;
    .action {
      display: inline-block;
      font-size: 14px;
      color: #0281F0;
      cursor: pointer;
      i {
        display: inline;
        font-size: 12px;
      }
    }
    .action.action-pack {
      margin-left: 0;
    }
  }
}
.popover-content{
  max-width: 40vw;
  max-height: 30vh;
  overflow: hidden;
  word-break: break-all;
  overflow-y: auto;
}
</style>

用法

<text-expand :text="text" :expand="2" />

在這里插入圖片描述

在這里插入圖片描述

<text-expand :text="text" :expand="2" :showBtnIcon="false">

在這里插入圖片描述
在這里插入圖片描述

<text-expand :text="text" :expand="2" :showPopover="true">

在這里插入圖片描述
在這里插入圖片描述

到此這篇關(guān)于vue 折疊展示多行文本組件的文章就介紹到這了,更多相關(guān)vue 折疊展示多行文本組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue Prop屬性功能與用法實(shí)例詳解

    Vue Prop屬性功能與用法實(shí)例詳解

    這篇文章主要介紹了Vue Prop屬性功能與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了vue.js中Prop屬性的功能、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-02-02
  • Vue.js中用v-bind綁定class的注意事項(xiàng)

    Vue.js中用v-bind綁定class的注意事項(xiàng)

    關(guān)于數(shù)據(jù)綁定一個常見需求就是操作元素的class列表和它的內(nèi)聯(lián)樣式。因?yàn)樗鼈兌际菍傩?,我們可以?v-bind 處理它們,但是使用v-bind綁定class的時候我們要有一些注意事項(xiàng),下面這篇文章就給大家分享了下要注意的方面,希望能對大家有所幫助,下面來一起看看吧。
    2016-12-12
  • vue?element表格某一列內(nèi)容過多,超出省略號顯示的實(shí)現(xiàn)

    vue?element表格某一列內(nèi)容過多,超出省略號顯示的實(shí)現(xiàn)

    這篇文章主要介紹了vue?element表格某一列內(nèi)容過多,超出省略號顯示的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Vue3?setup?的作用實(shí)例詳解

    Vue3?setup?的作用實(shí)例詳解

    setup?用來寫組合式?API,從生命周期的角度,相當(dāng)于取代了?beforeCreate(),這篇文章主要介紹了Vue3?setup?的作用,需要的朋友可以參考下
    2022-12-12
  • Vue3基于husky的代碼檢查工作流

    Vue3基于husky的代碼檢查工作流

    husky是一個git hooks工具(git的鉤子工具,可以在特定時機(jī)執(zhí)行特定的命令),這篇文章主要介紹了Vue3-基于husky的代碼檢查工作流,需要的朋友可以參考下
    2023-11-11
  • vue-cli4使用全局less文件中的變量配置操作

    vue-cli4使用全局less文件中的變量配置操作

    這篇文章主要介紹了vue-cli4使用全局less文件中的變量配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Vue3基于?rem?比例縮放方案示例詳解

    Vue3基于?rem?比例縮放方案示例詳解

    這篇文章主要介紹了Vue3基于rem比例縮放方案,本縮放方案置于hooks中即可,文中通過示例代碼介紹了vue-cli3 中使用rem布局的方法,需要的朋友可以參考下
    2023-05-05
  • Vue中$attrs與$listeners的使用教程

    Vue中$attrs與$listeners的使用教程

    這篇文章主要介紹了Vue中$attrs與$listeners的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果

    vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果

    這篇文章主要介紹了vue項(xiàng)目實(shí)例中用query傳參如何實(shí)現(xiàn)跳轉(zhuǎn)效果,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue + echarts實(shí)現(xiàn)中國省份地圖點(diǎn)擊聯(lián)動

    vue + echarts實(shí)現(xiàn)中國省份地圖點(diǎn)擊聯(lián)動

    這篇文章主要介紹了vue + echarts實(shí)現(xiàn)中國地圖省份點(diǎn)擊聯(lián)動,需要的朋友可以參考下
    2022-04-04

最新評論