更新:当我在内部放置CSS时,问题消失了。我进入了chrome浏览器的开发工具,并比较了重新加载页面时发生的情况。第一张图片使用内部CSS,第二张图片使用外部样式表,您可以看到background-color
属性的不同结果:
Initial post:我不知道为什么会这样,这里是demonstration。基本上,a
需要一些时间才能完全显示在页面上。
“完全”出现所需的时间与transition: background 5s;
中指定的持续时间相同,为5秒。
在另一个未观察到效果的page上有相同的代码,我也在同一浏览器中将其打开。
这是我在Visual Studio和Chrome中的代码,在Edge中观察到相同的行为,但在Firefox中观察不到,在Firefox中,它会按预期加载并且:hover
效果正常。在撰写本文时,所有3种浏览器均已更新为最新版本。
仅当我链接到外部CSS样式表时,才会发生这种情况。当我将CSS复制到html文件时,问题就消失了!!!
CSS
a {
color: #fff;
border: none;
margin: 20px;
padding: 10px 20px;
display: inline-block;
font: bold 18px sans-serif;
background: #fd7c2a;
text-decoration: none;
-webkit-transition: background 5s; /* For Safari 3.0 to 6.0 */
transition: background 5s; /* For modern browsers */
}
a:hover {
background: #3cc16e;
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content="" />
<!-- Specifies a description of the page. Search engines can pick up this description to show with the results of searches -->
<meta name="keywords" content="" />
<!-- Specifies a comma-separated list of keywords - relevant to the page -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<title></title>
</head>
<body>
<p><a href="#">Hover on me</a></p>
</body>
</html>
我解决了这个问题,一直在通过Google寻找答案。解决方案是在HTML文件中添加<script> </script>
开头和结尾标签,并以空格分隔。显然,这是前面提到的浏览器中的错误。