【发布时间】:2013-01-01 14:39:54
【问题描述】:
我遇到了 CSS transition 属性在页面加载时被触发的问题。
问题是,当我将color transition 应用于一个元素(例如:transition: color .2s)时,当页面第一次加载我的元素时,我的元素会从黑色闪烁到它自己指定的颜色。
假设我有以下代码:
CSS
p.green {
color: green;
transition: color .2s;
-moz-transition: color .2s;
-webkit-transition: color .2s;
-o-transition: color .2s;
}
p.green:hover {
color: yellow;
}
HTML
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="js/main.js"></script>
<link href="css/main.css" rel="stylesheet" />
</head>
<body>
<p class="green">The Flashing Text</p>
</body>
</html>
在页面加载时,我的p.green 将从black 淡化为green。
我不想将颜色转换应用到:hover 伪类,因为它不会应用转换onMouseLeave。
文本在网页上闪烁真的很烦人。到目前为止,我一直在避免使用过渡,除非我真的需要它们,即使如此我也要小心使用。如果有一些我没有看到的非常明显的解决方案,那就太好了!
这发生在谷歌浏览器上。我没有在其他浏览器中测试过。
jsfiddle.net/ShyZp/2 (感谢@Shmiddty)
【问题讨论】:
-
快速思考:
window.onload = function(){document.body.className += " loaded";}body.loaded p.green{transition:color .2s;} -
如果您只有少数具有颜色转换的类,这将起作用。我正在寻找一个非 JavaScript 的答案,最好...
-
我无法在 Chrome 中重现问题:jsfiddle.net/ShyZp
-
我在 chrome 中不会出现这个问题,请在 jsfiddle 中向我们提供您的确切代码...
-
问题可以在这里看到,但这仅在两个声明不在同一个文件中发生时才有效:jsfiddle.net/ShyZp/2
标签: html css google-chrome