CSS实现文本溢出省略号或完整显示

8,420次阅读
没有评论

共计 2179 个字符,预计需要花费 6 分钟才能阅读完成。

目录

  • 前言
  • 1. 省略号
  • 2. 完整展示
  • 3. Demo

前言

文本内容超出容器宽度的问题,为了保持页面布局的整洁,通常会使用省略号来隐藏多余的内容

一共有两种方式:

  1. 设定省略号
  2. 完整展示

1. 省略号

文本溢出时显示省略号

.item-value {
  flex-basis: 70%; 
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis; 
  cursor: pointer; 
}

在某些场景下,用户可能需要查看被省略的完整文本

通过 CSS 中的伪类和事件状态,可以实现类似长按或点击查看完整内容的效果

.item-value:active {
  white-space: normal; 
  overflow: visible; 
  text-overflow: initial; 
}

2. 完整展示

.item-value {
  flex-basis: 70%; 
  white-space: nowrap; 
  overflow: visible; 
  white-space: normal; 
  text-overflow: clip; 
  cursor: pointer; 
}

3. Demo

整体 Demo 配合如下:

template>
  view>
    
    view class="search-class bg-white">
      view class="search-button bg-white" style="display: flex; justify-content: space-around;">
        button v-if="checkPermission('ship:risk:create')" class="bg-green" style="width: 40%;" @click="dataInfo()"> 新增风险 button>
      view>
    view>

    
    view class="data-content">
      uni-card class="card" v-for="item in dataList" :key="item.id" :extra="item.shipCod">
        
        uni-list>
          
          view class="list-item-container">
            text class="item-title"> 位置:text>
            text class="item-value">{{item.vesselPosition}}text>
          view>

          
          view class="list-item-container">
            text class="item-title"> 描述:text>
            text class="item-value">{{item.vesselDescribe}}text>
          view>

          
          view class="list-item-container">
            text class="item-title"> 图片:text>
            view class="item-value">
              image :src="item.vesselPicture" mode="widthFix" class="vessel-image" @click="previewImage(item.vesselPicture)">image>
            view>
          view>
        uni-list>
      uni-card>
    view>
  view>
template>

script>
export default {
  data() {
    return {
      dataList: [
        { id: 1, vesselPosition: '广州港', vesselDescribe: '这是一个非常长的描述,测试文本溢出显示效果。', vesselPicture: 'https://via.placeholder.com/150', shipCod: 'COD001' },
        { id: 2, vesselPosition: '深圳港', vesselDescribe: '描述二', vesselPicture: 'https://via.placeholder.com/150', shipCod: 'COD002' },
        
      ]
    };
  },
  methods: {
    checkPermission(permission) {
      
      return true; 
    },
    dataInfo() {
      
      console.log('新增风险');
    },
    previewImage(imageUrl) {
      
      console.log('预览图片:', imageUrl);
    }
  }
};
script>

style scoped>
.data-content {
  padding: 20px;
}

.card {
  margin-bottom: 20px;
}

.list-item-container {
  display: flex;
  margin-bottom: 10px;
}

.item-title {
  font-weight: bold;
  flex-basis: 30%;
}

.item-value {
  flex-basis: 70%; 
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis; 
  cursor: pointer; 
}


.item-value:active {
  white-space: normal; 
  overflow: visible; 
  text-overflow: initial; 
}

.vessel-image {
  width: 100px;
  height: auto;
}
style>

原文地址: CSS 实现文本溢出省略号或完整显示

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-23发表,共计2179字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)