【问题标题】:Left Ellipsis Overflow on IE/EdgeIE/Edge 上的左省略号溢出
【发布时间】:2018-04-09 00:51:12
【问题描述】:

.left-ellipsis {
  text-align: left;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  direction: rtl;
}

.left-ellipsis span {}


/* Media query for IE only. */

@media screen and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
  .left-ellipsis span {}
}
<strong>With overflowing content</strong>
<br>
<i>Expected to see <strong>1 0002 0003 0004 0005 end</strong></i>
<div style="border: 1px solid #666; width: 200px; padding: 5px;">
  <div class="left-ellipsis">
    <span dir="ltr">123 456 789 0001 0002 0003 0004 0005 end</span>
  </div>
</div>
<br>
<strong>Without overflowing content</strong>
<br>
<i>Expected to see <strong>0002 0003 0004 0005 end</strong> without ...</i>
<div style="border: 1px solid #666; width: 200px; padding: 5px;">
  <div class="left-ellipsis">
    <span dir="ltr">0002 0003 0004 0005 end</span>
  </div>
</div>

问题:我们希望在 div 左侧的溢出文本上​​显示省略号。

问题:该解决方案在 Chrome 和 Firefox 中运行良好,但在 IE 或 Edge 中无法正常运行。省略号正确显示在左侧,但数据是从右侧截取的,而不是从左侧截取的。

尝试

  • 我尝试在跨度中添加一个“float:right”,然后 IE/Edge 显示正确的数据,但省略号完全消失。
  • 我们已经通过搜索 SO 寻找“方向”解决方案来达到这一点。它似乎适用于不错的浏览器。

非常感谢任何帮助。这是一个相当大的挑战。

编辑:这是一个人们可以尝试的小工具: https://plnkr.co/edit/AWyzZLhnLbt4DStnU5hW?p=preview

尝试使用@ovokuro 发布的文章提供的代码,我们得到chrome 来显示这个,这也不好。:

【问题讨论】:

  • @jaunt 为什么要删除我的 plunker?不允许在 SO 上使用 plunkers 吗?
  • 它们是,但如果可以的话,最好使用 sn-p,因为它可以更容易地复制到答案中,不认为有一个链接也有太多目的 - 将其添加回来如果你愿意:)
  • 这篇文章可能有一些建议:hugogiraudel.com/2014/12/16/css-riddle-reverse-ellipsis
  • 你愿意在其中添加一些 JS 吗?
  • @andi 我想我们肯定可以。至今没用过JS。

标签: html css internet-explorer microsoft-edge


【解决方案1】:

这里有一些使用 JS 的尝试。我没有要测试的 IE,但希望它能给你一些逻辑,让你继续前进。基本思想是测试内容及其容器的宽度,同时一次删除一个字符以查看它何时适合。 (我使用 jQuery 只是因为它对我来说更快,但你当然不需要它。)

$('.left-ellipsis').each(function() {
	var $span = $(this).find('span');
	if ($span.width() > $(this).width()) {
		$span.addClass('ellipsis');
		while ($span.width() > $(this).width()) {
			$span.text($span.text().substr(1));
		}
	}
});
.left-ellipsis {
  overflow: hidden;
}

.left-ellipsis span {
	white-space:nowrap;
}

.left-ellipsis span.ellipsis {
	float:right;
}

.left-ellipsis span.ellipsis:before {
	content:"...";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>With overflowing content</strong><br>
<i>Expected to see <strong>1 0002 0003 0004 0005 end</strong></i>
<div class="left-ellipsis" style="border: 1px solid #666; width: 200px; padding:5px;">
	<span>123 456 789 0001 0002 0003 0004 0005 end</span>
</div>
<br>
<br>
<strong>Without overflowing content</strong><br>
<i>Expected to see <strong>0002 0003 0004 0005 end</strong> without ...</i>
<div class="left-ellipsis" style="border: 1px solid #666; width: 200px; padding: 5px;">
	<span>0002 0003 0004 0005 end</span>
</div>

【讨论】:

  • +1。看起来它确实适用于IE。当内容不溢出时,有没有一种简单的方法可以让文本左对齐?
  • 我认为您应该能够将float:right 移动到仅在溢出情况下使用的span.ellipsis
  • 谢谢@andi。这里还有一种情况,就是在屏幕宽度改变时更新省略号。我在这里添加了一个 plunker:plnkr.co/edit/rolCM3HD0Cw3LBVKEU4c?p=preview
  • 啊...在这种情况下,您首先需要将原始文本存储在数据属性中(因为上面的代码删除了字符)。然后,在调整大小处理程序中,首先将文本设置回原始文本,如果之前添加了类ellipsis,则删除它,然后重新运行上面的函数。
  • 对 plunkr 进行了一些更改以向您展示:plnkr.co/edit/rtsEbTF0shmFQAm2l2yL?p=preview
猜你喜欢
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
相关资源
最近更新 更多