CSS 动画在 Chrome/Webkit 浏览器上失败

标签 css google-chrome css-transitions css-animations

我目前正在设计一个博客,我在 Chrome(和一般的 webkit 浏览器)上发现了一个奇怪的故障。

我将“阅读更多”链接放在方括号中,表示我想在悬停时以某种方式移动,然后在鼠标移开时返回。 它在 IE 或 Firefox 上就像一个魅力,但在 Chromium 中,当动画结束时,它会跳回初始位置(我认为它会在屏幕上弹出 url 时停止)。

.read_more {
  font-family: sans-serif;
  color: black;
  text-decoration: none;
  padding-left: 20px;
}
.read_more:hover {
  color: black;
}
.read_more:before {
  content: '[ ';
  transition: all ease-out .35s;
}
.read_more:after {
  content: ' ]';
  transition: all ease-out .35s;
}
.read_more:hover:after {
  transform: translateX(4px);
  transition: all ease-out .35s;
}
.read_more:hover:before {
  transform: translateX(-4px);
  transition: all ease-out .35s;
}
 <h2>This is an article</h2>
<p>
  Ut viverra vel eros ut laoreet. Pellentesque eu imperdiet eros, eu pharetra libero. Aenean id tempor arcu. Lorem ipsum dolor sit amet, consectetur adipiscing elit [...]
</p>
<a class="read_more" href="#">Read more</a>

有没有人有解决这个问题的想法?

这是我的代码的代码笔:codepen

最佳答案

默认情况下 :before:after 伪元素是 inline elements . CSS specs transformable elements 中未列出行内元素所以他们根本不应该动画。

也就是说,将 display:inline-block; 添加到伪元素可以解决您的问题,因为内联 block 元素是可转换的:

.read_more {
  font-family: sans-serif;
  color: black;
  text-decoration: none;
  padding-left: 20px;
}
.read_more:hover {
  color: black;
}
.read_more:before {
  content: '[ ';
  display:inline-block;
  transition: all ease-out .35s;
}
.read_more:after {
  content: ' ]';
  display:inline-block;
  transition: all ease-out .35s;
}
.read_more:hover:after {
  transform: translateX(4px);
  transition: all ease-out .35s;
}
.read_more:hover:before {
  transform: translateX(-4px);
  transition: all ease-out .35s;
}
<h2>This is an article</h2>
<p>
  Ut viverra vel eros ut laoreet. Pellentesque eu imperdiet eros, eu pharetra libero. Aenean id tempor arcu. Lorem ipsum dolor sit amet, consectetur adipiscing elit [...]
</p>
<a class="read_more" href="#">Read more</a>

关于CSS 动画在 Chrome/Webkit 浏览器上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707481/

相关文章:

javascript - 从数组中添加和删除值

css - 在其上加载 png 图像时,背景图像有时会消失

Javascript - 谷歌地图简单 map 不在网络服务器上呈现图 block - 在本地工作

java - 手机上的 Android 主机网站

html - 在悬停时使 float 图像透明会导致它变为 'jump'

javascript - 将下载的文件移动到特定位置的包管理器

node.js - Nodejs 响应时间太长

javascript - Chrome content.min.js 错误 : Cannot read property 'parentNode' of undefined

jquery - 为什么 CSS3 过渡不能工作多次

javascript - 直接在 JS 中动态改变 transition 还是使用 css classes 对性能更好?