当前位置:   article > 正文

js判断文字超出两行...,并展示查看更多按钮_js判断字符串是否超过两行

js判断字符串是否超过两行

HTML

<div class="card-content">
    <el-scrollbar style="height: 100%;">
        <ul class="card-box" v-if="warningData.length">
            <li class="card-cell" v-for="(item, index) in warningData" :key="index">
                <el-row type="flex" justify="end" v-if="item.isBeyond">
                    <span class="arrow-right" @click="item.isExpand = !item.isExpand">
                        {{item.isExpand ? '折叠详情' : '展开详情'}}
                    </span>
                </el-row>
                <el-row :class="['desc-row', {'ellipsisFont': item.isBeyond && !item.isExpand }]">
                    <div class="real-box">{{item.description}}</div>
                    <div class="virtually-box">{{item.description}}</div>
                </el-row>
            </li>
        </ul>
        <div v-else class="empty-txt">暂无数据</div>
    </el-scrollbar>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

JS

mounted(){
    this.$nextTick(() => {
        this.judgeHeight()
    })
    window.addEventListener('resize', () => {
        this.judgeHeight()
    })
},
methods: {
    judgeHeight(){
        if(this.warningData.length) {
            const ELArr = document.querySelectorAll('.card-content .virtually-box')
            let _transData = JSON.parse(JSON.stringify(this.warningData))
            _transData.forEach((item, index) => {
                item.isBeyond = false;
                item.isExpand = false;
                if(ELArr[index].clientHeight > 48){
                    item.isBeyond = true;
                }
            })
            this.warningData = JSON.parse(JSON.stringify(_transData))
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

CSS

html {
    background-color: #efefef;
}
* {
    padding: 0;
    margin: 0;
}
.card-content {
    height: 600px;
    padding: 20px 30px;
    box-sizing: border-box;
}
.card-box {
    padding: 0;
}
.card-cell {
    background-color: #fff;
    list-style: none;
    padding: 15px 20px;
    border-radius: 6px;
}
.card-cell:not(:last-child) {
    margin-bottom: 15px;
}
.desc-row {
    height: auto;
    overflow: hidden;
    position: relative;
    color: #666;
    line-height: 24px;
}
.desc-row.ellipsisFont {
    height: 48px;
}
.ellipsisFont .real-box {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}
.virtually-box {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: -1;
}
.arrow-right {
    color: #5197E6;
    cursor: pointer;
    margin-bottom: 10px;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

效果图

在这里插入图片描述
点击展开详情
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/91738
推荐阅读
相关标签
  

闽ICP备14008679号