简体   繁体   English

-webkit-line-clamp:2 在离子 4 中不工作

[英]-webkit-line-clamp: 2 not working in ionic 4

I am using -webkit-line-clamp: 2 to limit the number of lines appearing in an ionic4 app, but it does not comes up as expected on initial load.我正在使用-webkit-line-clamp: 2来限制 ionic4 应用程序中出现的行数,但它在初始加载时没有按预期出现。 Instead, it comes up when I edit the css using inspect in chrome.相反,当我在 chrome 中使用检查编辑 css 时,它会出现。 Is there any workaround to fix it?有什么办法可以解决吗? The css used is:使用的 css 是:

display: -webkit-box;
overflow: hidden;
text-overflow: ellipsis;
-webkit-box-orient: vertical;

 p { width: 300px;important: display; -webkit-box:important; overflow: hidden;important: text-overflow; ellipsis:important; -webkit-box-orient: vertical !important; -webkit-line-clamp: 3 !important }
 <p>   In this example the <code>-webkit-line-clamp</code> lorem ipsum lorem ipsum <code>3</code>, which means the text is clamped after three lines.   An ellipsis will be shown at the point where the text is clamped. </p>

  • Make a class if u want to use it multiple times or id if you want to use it once如果您想多次使用它,请制作class ,如果您想使用一次,请制作id
  • Then implement that class or id in your root css files(global or variable) by using '!important'然后使用'!important'在您的根 css 文件(全局或变量)中实现class or id
  • It will surely work for you它肯定会为你工作

I have been facing the exact same issue and also made a question regarding it here: Ionic 4: -webkit-line-clamp property not applying on inital load我一直面临着完全相同的问题,并在这里提出了一个问题: Ionic 4: -webkit-line-clamp property not apply on inital load

After days of trying to figure out what has been causing this.经过几天试图找出造成这种情况的原因。 I have found that in my case I have ion-slides ( https://ionicframework.com/docs/api/slides ) implemented in my HTML (The same HTML file where I am facing this issue) which seems to be causing for all my styles to still apply besides from -webkit-line-clamp: 2我发现在我的情况下,我在我的 HTML 中实现了ion-slideshttps://ionicframework.com/docs/api/slides )(同样的 Z4C4AD5FCA2E7A3F74ZDBB1CED00381AA4 似乎是我面临的所有文件的问题)我的 styles 除了来自-webkit-line-clamp: 2之外仍然适用

When I remove all instances of ion-slides within that file then my -webkit-line-clamp: 2 css rule applies correctly on initial load.当我删除该文件中的所有ion-slides实例时,我的-webkit-line-clamp: 2 css 规则在初始加载时正确应用。 I'm still trying to figure out how I can have both ion-slides and -webkit-line-clamp: 2 however i'll update this answer if I end up finding a proper solution.我仍在试图弄清楚如何同时拥有ion-slides-webkit-line-clamp: 2但是如果我最终找到合适的解决方案,我会更新这个答案。

UPDATE更新

In my particular case.在我的特殊情况下。 I have ion-slides and an *ngFor loop to create mutilple ion-slide depending on the data returned.我有ion-slides和 *ngFor 循环来根据返回的数据创建多个ion-slide I noticed that I would be rendering the ion-slides last as it was dependent on the data returned from a HTTP call which seemed to have an impact on the rest of the HTML elements and their styling (In this case the -webkit-line-clamp rule) when the ion-slides was fully rendered on the page.我注意到我将最后渲染ion-slides ,因为它取决于从 HTTP 调用返回的数据,这似乎对 Z4C4AD5FCA2E7A3F74DBB1CED00381AA4 的-webkit-line-clamp有影响-webkit-line-clamp规则)当ion-slides完全呈现在页面上时。

The problem seems to be based on a timing issue and the solution for my particular case was to fetch the data first for the ion-slides content and then the rest after that.问题似乎是基于时间问题,我的特殊情况的解决方案是首先获取ion-slides内容的数据,然后再获取 rest。 I know this may be quite a vague answer but this issue seems to be very edge case-y and I hope this helps you to resolving this issue.我知道这可能是一个相当模糊的答案,但这个问题似乎非常极端,我希望这可以帮助你解决这个问题。

UPDATE 2更新 2

It seems that when I thought I had the issue fixed, I started running into again.似乎当我认为我已经解决了问题时,我又开始遇到了。 There is a module that exists that can provide the same functionality as the CSS rule here ( https://www.npmjs.com/package/ngx-ellipsis ) however I noticed when loading many elements, it can have a slight performance impact.有一个模块可以提供与此处的 CSS 规则相同的功能( https://www.npmjs.com/package/ngx-ellipsis )但是我注意到在加载许多元素时,它可能会对性能产生轻微影响。 If you don't want to face this performance impact.如果您不想面对这种性能影响。 You can limit the width and height of the element to limit the amount of characters shown您可以限制元素的宽度和高度以限制显示的字符数量

For ionic 4 you have to add style="-webkit-box-orient: vertical" to the html element For example:对于 ionic 4,您必须将style="-webkit-box-orient: vertical"添加到 html 元素例如:

<p style="-webkit-box-orient: vertical">
In this example the <code>-webkit-line-clamp</code> lorem ipsum lorem ipsum <code>3</code>, which means the text is clamped after three lines.
  An ellipsis will be shown at the point where the text is clamped.
</p>
p {
 width: 300px;
  overflow: hidden;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;  
}

Remember to check for white-space , especially if you are inside <ion-label> :记得检查white-space ,特别是如果你在<ion-label>里面:

 p { width: 300px;important: display; -webkit-box:important; overflow: hidden;important: text-overflow; ellipsis:important: -webkit-box-orient; vertical !important; -webkit-line-clamp: 3 !important white-space: normal; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM