【问题标题】:Chrome bug with animating ellipsis text that changes colour. the text moves to the right带有动画省略号文本的 Chrome 错误会改变颜色。文本向右移动
【发布时间】:2017-03-08 13:15:28
【问题描述】:

这是我遇到的问题的一个示例。单击中间的锚点会使其向右移动。

var anchors = document.getElementsByTagName('a');

var onSelect = function () {
  var i, a;
  for (i = 0; i < anchors.length; i += 1) {
    a = anchors[i];
    if (a === this)
    	continue;
    a.classList.remove('selected');
  }
	this.classList.toggle('selected');
};

(function () {
  for (var i = 0; i < anchors.length; i += 1) {
    anchors[i].addEventListener('click', onSelect);
  }
}());
.table {
  display: table;
  background-color: indigo;
  width: 600px;
}

.row {
  display: table-row;
  background-color: seagreen;
}

.cell {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

a {
  padding: 10px;
  background-color: lightblue;
  display: inline-block;
  transition: 0.5s ease;
  width: 140px;
  height: 30px;
  line-height: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-decoration: none;
  color: black;
}

a.selected {
  background-color: blue;
  color: white;
}
<div class="table">
	<div class="row">
    <div class="cell">
      <a href="#">A New Hope</a>
    </div>
    <div class="cell">
      <a href="#">The Empire Strikes Back</a>
    </div>
    <div class="cell">
      <a href="#">Return of the Jedi</a>
    </div>
  </div>
</div>

如果我不更改文本颜色或删除省略号,则不会发生。我只能在 chrome 中重现它,所以我认为这是该浏览器的问题。

想知道是否有已知的解决方法。我的 chrome 版本是 53.0.2785.143 m

jsfiddle 中的相同示例: https://jsfiddle.net/f5k8p3tv/

【问题讨论】:

  • 一个令人难以置信的 hacky 解决方法是从父节点中删除 'cell' 类,然后将其重新添加。不幸的是,这样做元素会跳动一点,这并不是真正可取的。
  • 谢谢。我认为这会起作用,因为“文本对齐:中心;”是导致错误发生的因素之一。如果我删除它,一切正常(除了没有居中对齐)

标签: javascript css google-chrome css-animations


【解决方案1】:

在锚元素上将display:inline-block 设置为display:block

不知何故,它试图重新评估它的“内联”位置并导致它移动。 由于您使用的是表格,因此不需要内联功能,因为每个元素在表格行中都有固定的位置。

var anchors = document.getElementsByTagName('a');

var onSelect = function () {
  var i, a;
  for (i = 0; i < anchors.length; i += 1) {
    a = anchors[i];
    if (a === this)
    	continue;
    a.classList.remove('selected');
  }
	this.classList.toggle('selected');
};

(function () {
  for (var i = 0; i < anchors.length; i += 1) {
    anchors[i].addEventListener('click', onSelect);
  }
}());
.table {
  display: table;
  background-color: indigo;
  width: 600px;
}

.row {
  display: table-row;
  background-color: seagreen;
}

.cell {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

a {
  padding: 10px;
  background-color: lightblue;
  display: block;
  transition: 0.5s ease;
  width: 140px;
  height: 30px;
  line-height: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-decoration: none;
  color: black;
}

a.selected {
  background-color: blue;
  color: white;
}
<div class="table">
	<div class="row">
    <div class="cell">
      <a href="#">A New Hope</a>
    </div>
    <div class="cell">
      <a href="#">The Empire Strikes Back</a>
    </div>
    <div class="cell">
      <a href="#">Return of the Jedi</a>
    </div>
  </div>
</div>

【讨论】:

  • 谢谢。这行得通。我进一步添加了 margin: auto 到锚点,使它们水平居中。它现在看起来与另一个示例相同,但没有相同的错误。
猜你喜欢
  • 1970-01-01
  • 2010-12-27
  • 2013-08-10
  • 1970-01-01
  • 2012-04-02
  • 2017-04-07
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多