Vue中一行文字过多时,超出文本内容用省略号...隐藏显示

   <div class="info-title">
文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多文字过多
    </div>
.info-title {
  color: #19d3ea;
  font-size: 18px;
  width: 100%;   /*一定要设置宽度,或者元素内含的百分比*/
  overflow:hidden; /*溢出的部分隐藏*/
  white-space: nowrap; /*文本不换行*/
  text-overflow:ellipsis;/*ellipsis:文本溢出显示省略号(...);clip:不显示省略标记(...),而是简单的裁切*/
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 实现文本超出字数时显示省略号,可以使用 CSS 的 `text-overflow: ellipsis` 属性。具体步骤如下: 1. 在 CSS 设置需要显示省略号的元素的样式: ```css .text-ellipsis { white-space: nowrap; /* 不换行 */ overflow: hidden; /* 超出部分隐藏 */ text-overflow: ellipsis; /* 显示省略号 */ } ``` 2. 在 Vue 模板使用 `v-bind:class` 绑定样式类,并设置 `title` 属性为完整文本: ```html <p v-bind:class="{ 'text-ellipsis': isOverflow }" title="完整文本">{{ text }}</p> ``` 其,`isOverflow` 是一个计算属性,用于判断文本是否超出,`text` 是需要显示文本。 3. 在计算属性计算文本是否超出: ```javascript computed: { isOverflow() { const el = this.$el.querySelector('.text-ellipsis'); return el ? el.offsetWidth < el.scrollWidth : false; } } ``` 使用 `querySelector` 获取元素,然后判断元素的实际宽度是否大于容器的宽度,如果大于就返回 `true`,否则返回 `false`。如果获取不到元素,就返回 `false`。 完整代码示例: ```html <template> <div> <p v-bind:class="{ 'text-ellipsis': isOverflow }" title="完整文本">{{ text }}</p> </div> </template> <script> export default { data() { return { text: '这是一段很长很长很长的文本,超过了容器宽度' }; }, computed: { isOverflow() { const el = this.$el.querySelector('.text-ellipsis'); return el ? el.offsetWidth < el.scrollWidth : false; } } }; </script> <style> .text-ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100px; border: 1px solid #ccc; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值