如何在 CSS 和 HTML 中使用过渡
how to use transition in CSS and HTML
我想用 HTML 和 CSS 创建一个导航栏,我希望当我将鼠标光标悬停在每个项目上时,项目下方会出现一条小线(如 border-bottom)有 0.2s 的过渡。我希望这条线从右到左出现
我搜索了一下,发现我应该使用这个代码:
transition : width time ;
喜欢
transition : width 0.2s ;
但是当我使用此代码时,“宽度”不起作用并且未定义。
你能帮我吗?
body
{
background-color: gray;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
ul
{
list-style-type: none;
margin: 0;
padding:0;
overflow: hidden;
background-color: #333;
border-radius: 50px;
width: 90%;
}
li
{
float: left;
transition:width 0.2s;
}
li a
{
display: block;
text-decoration: none;
color: rgb(255, 255, 255);
text-align: center;
padding: 15px 20px;
}
.searchbtn
{
float: right;
}
li:hover
{
background-color: rgb(43, 43, 43) ;
border-bottom: 3px solid white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<title>Title</title>
<link rel="icon" type="icon/x-type" href="IMG/fav.png">
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Support</a></li>
<li class="searchbtn"><a href="#">Search</a></li>
</ul>
</body>
</html>
向宽度的过渡没有做任何事情,因为您永远不会更改“li”元素的宽度。
“过渡”属性 的工作方式是通过在 属性 你 select 中动画变化。
如果你想在悬停时改变文本颜色并有一个很好的过渡,你可以这样做:
span {
color: #000;
transition: color 0.2s;
}
span:hover {
color: #ccc;
}
我认为您可以使用 "after" pseudo selector 通过模拟底部边框来实现您想要的效果。由于不能直接修改边框的长度
遗憾的是,我们无法设置(或设置)边框的 width。所以我们需要另一种方式。
您可以使用 CSS 创建 pseudo-elements,就像 ::before
和 ::after
在另一个元素内创建块一样。对于您的情况,我们可以使用上述任一伪元素在每个 li
.
的底部创建一个 border like 栏
然后可以像常规 HTML 元素一样对该栏进行动画处理。这意味着我们可以改变宽度、高度、颜色等,但仍然让它看起来像一个边框。
在下面的示例中,我在 li
元素上使用了 ::after
伪元素来在列表项中创建类似边框的元素。我使用 ::after
因为边框需要 在 li
.
中的内容之后
元素以 scaleX
转换开始,该转换操纵伪元素的水平大小。每当您悬停时,scaleX
将恢复为全宽。
使用 transform
而不是 width
的原因是 transform
针对过渡和动画进行了优化,尽管没有法律禁止过渡 width
。
body {
background-color: gray;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
border-radius: 50px;
width: 90%;
}
li {
/**
* This needs to be position: relative so that the ::after element
* will stay be relative to the size of this element.
* Try to remove it and see what happens.
*/
position: relative;
float: left;
transition: border-color 0.2s;
}
li:hover {
background-color: rgb(43, 43, 43);
}
li a {
display: block;
text-decoration: none;
color: rgb(255, 255, 255);
text-align: center;
padding: 15px 20px;
}
li::after {
content: "";
display: block;
position: absolute;
bottom: 0;
left: 0;
height: 3px;
width: 100%;
transform-origin: right center;
transform: scaleX(0);
transition: transform 0.2s;
background-color: white;
z-index: 1;
}
li:hover::after {
transform: scaleX(1);
}
.searchbtn {
float: right;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<title>Title</title>
<link rel="icon" type="icon/x-type" href="IMG/fav.png">
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Support</a></li>
<li class="searchbtn"><a href="#">Search</a></li>
</ul>
</body>
</html>
旁注:您正在使用一些旧技术(例如 float
)来定位您的元素。 Flexbox and CSS Grid Layout 超越了这些技术,让您对布局有更多(更好)的控制。因此,我建议您研究上面的两个链接,并找到有关创建布局的最新教程(YouTube 中包含有关 Flexbox 和 Grid 的内容)。
我想用 HTML 和 CSS 创建一个导航栏,我希望当我将鼠标光标悬停在每个项目上时,项目下方会出现一条小线(如 border-bottom)有 0.2s 的过渡。我希望这条线从右到左出现 我搜索了一下,发现我应该使用这个代码:
transition : width time ;
喜欢
transition : width 0.2s ;
但是当我使用此代码时,“宽度”不起作用并且未定义。 你能帮我吗?
body
{
background-color: gray;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
ul
{
list-style-type: none;
margin: 0;
padding:0;
overflow: hidden;
background-color: #333;
border-radius: 50px;
width: 90%;
}
li
{
float: left;
transition:width 0.2s;
}
li a
{
display: block;
text-decoration: none;
color: rgb(255, 255, 255);
text-align: center;
padding: 15px 20px;
}
.searchbtn
{
float: right;
}
li:hover
{
background-color: rgb(43, 43, 43) ;
border-bottom: 3px solid white;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<title>Title</title>
<link rel="icon" type="icon/x-type" href="IMG/fav.png">
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Support</a></li>
<li class="searchbtn"><a href="#">Search</a></li>
</ul>
</body>
</html>
向宽度的过渡没有做任何事情,因为您永远不会更改“li”元素的宽度。 “过渡”属性 的工作方式是通过在 属性 你 select 中动画变化。 如果你想在悬停时改变文本颜色并有一个很好的过渡,你可以这样做:
span {
color: #000;
transition: color 0.2s;
}
span:hover {
color: #ccc;
}
我认为您可以使用 "after" pseudo selector 通过模拟底部边框来实现您想要的效果。由于不能直接修改边框的长度
遗憾的是,我们无法设置(或设置)边框的 width。所以我们需要另一种方式。
您可以使用 CSS 创建 pseudo-elements,就像 ::before
和 ::after
在另一个元素内创建块一样。对于您的情况,我们可以使用上述任一伪元素在每个 li
.
的底部创建一个 border like 栏
然后可以像常规 HTML 元素一样对该栏进行动画处理。这意味着我们可以改变宽度、高度、颜色等,但仍然让它看起来像一个边框。
在下面的示例中,我在 li
元素上使用了 ::after
伪元素来在列表项中创建类似边框的元素。我使用 ::after
因为边框需要 在 li
.
元素以 scaleX
转换开始,该转换操纵伪元素的水平大小。每当您悬停时,scaleX
将恢复为全宽。
使用 transform
而不是 width
的原因是 transform
针对过渡和动画进行了优化,尽管没有法律禁止过渡 width
。
body {
background-color: gray;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
border-radius: 50px;
width: 90%;
}
li {
/**
* This needs to be position: relative so that the ::after element
* will stay be relative to the size of this element.
* Try to remove it and see what happens.
*/
position: relative;
float: left;
transition: border-color 0.2s;
}
li:hover {
background-color: rgb(43, 43, 43);
}
li a {
display: block;
text-decoration: none;
color: rgb(255, 255, 255);
text-align: center;
padding: 15px 20px;
}
li::after {
content: "";
display: block;
position: absolute;
bottom: 0;
left: 0;
height: 3px;
width: 100%;
transform-origin: right center;
transform: scaleX(0);
transition: transform 0.2s;
background-color: white;
z-index: 1;
}
li:hover::after {
transform: scaleX(1);
}
.searchbtn {
float: right;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<title>Title</title>
<link rel="icon" type="icon/x-type" href="IMG/fav.png">
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Support</a></li>
<li class="searchbtn"><a href="#">Search</a></li>
</ul>
</body>
</html>
旁注:您正在使用一些旧技术(例如 float
)来定位您的元素。 Flexbox and CSS Grid Layout 超越了这些技术,让您对布局有更多(更好)的控制。因此,我建议您研究上面的两个链接,并找到有关创建布局的最新教程(YouTube 中包含有关 Flexbox 和 Grid 的内容)。