vue组件-文字太多出现展开收起按钮或者hover的时候展示全部,没有的情况下默认展示

说明:
组件封装了两种情况:
1:超过有省略号,有展开收起按钮
2.超过部分省略号,并且hover的时候有提示,没有展开收起按钮

调用:

/**
title:显示的内容
lineHeight:单行的行高
eclipse: 第几行出现省略号
ishasOpenBtnType: 字符串,默认为1
                  1:超过有省略号,有展开收起按钮
                  2.超过部分省略号,并且hover的时候有提示,没有展开收起按钮
**/
<eclipse :title="title" lineHeight="18" eclipse="3" ishasOpenBtnType="1">
</eclipse>

组件完整代码:

<template>
    <div class="eclipse_contain">
        <div v-if="ishasOpenBtnType == '1' || !ishasOpenBtnType">
            <div v-if="(!ifMore && !isOpen) || (ifMore && isOpen)" class="normal_content" ref="ellipse" :style="{'line-height':lineHeight+'px'}">
                {{title}}
            </div>
            <div v-if="ifMore && isOpen" class="ellipsisLine2" :style="{'line-height':lineHeight+'px','-webkit-line-clamp':eclipse}">
                {{title}}
            </div>
            <div class="btn_group" v-if="ifMore">
                <span v-if="isOpen" @click="closeH">收起</span>
                <span v-else @click="openH">展开</span>
            </div>
        </div>
        <div v-else>
            <div v-if="!ifMore" class="normal_content" ref="ellipse" :style="{'line-height':lineHeight+'px'}">
                {{title}}
            </div>
            <el-tooltip v-if="ifMore" class="item" effect="dark" :content="title" placement="top">
                <div class="ellipsisLine2" :style="{'line-height':lineHeight+'px','-webkit-line-clamp':eclipse}">
                    {{title}}
                </div>
            </el-tooltip>
        </div>
    </div>
</template>
<script>
export default {
    name:"eclips",
    props:{
        title:String,
        lineHeight:String,
        eclipse:String,
        isShow:Boolean,
        ishasOpenBtnType:String,
    },
    data(){
        return {
            ifMore:false,//是否超过高度
            isOpen:false,//是否已经展开
        }
    },
    watch:{
        title(newVal,oldVal){
            this.getHeight();
        }
    },
    methods:{
        getHeight(){
            if(this.$refs.ellipse.clientHeight > Number(this.lineHeight)*Number(this.eclipse)){
                this.ifMore = true;
            }else{
                this.ifMore = false;
            }
        },
        closeH(){
            this.isOpen = false;
        },
        openH(){
            this.isOpen = true;
        },
    },
    mounted(){
        this.getHeight();
    }
}
</script>
<style scoped lang="scss">
.eclipse_contain{
    width:100%;
    display:table;
}
.normal_content{
    width:100%;
    white-space: normal;
    display: -webkit-box;
    word-break: break-all;
}
.ellipsisLine2{
    white-space: normal;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    word-break: break-all;
}
.btn_group{
    width:100%;
    span{
        float:right;
        color:#4c88bd;
        cursor: pointer;
        font-size:12px;
        margin-right:10px;
    }
}
</style>

效果图:
第一种展开收起按钮:

第二种:文字太长展示省略号,hover的时候显示所有

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值