【问题标题】:how to use transition in CSS and HTML如何在 CSS 和 HTML 中使用过渡
【发布时间】:2022-01-27 08:19:08
【问题描述】:

我想用 HTML 和 CSS 创建一个导航栏,当我将鼠标光标悬停在每个项目上时,我希望在项目(如边框底部)下方出现一条小线,并带有 0.2 秒的过渡。我希望这条线从右到左出现 我搜索了一下,发现应该使用这段代码:

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>

【问题讨论】:

    标签: javascript html jquery css bootstrap-4


    【解决方案1】:

    很遗憾,我们无法为边框的宽度设置动画(或设置)。所以我们需要另一种方式。

    使用 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)来定位您的元素。 FlexboxCSS Grid Layout 已经超越了这些技术,它们可以让您对布局进行更多(和更好)的控制。因此,我建议您研究上面的两个链接,并找到有关创建布局的最新教程(YouTube 中有关于 Flexbox 和 Grid 的介绍)。

    【讨论】:

      【解决方案2】:

      到宽度的过渡没有做任何事情,因为你永远不会改变“li”元素的宽度。 “过渡”属性的工作方式是通过动画更改您选择的属性。 如果您想在悬停时更改文本颜色并进行良好的过渡,您可以执行以下操作:

      span {
        color: #000;
        transition: color 0.2s;
      }
      
      span:hover {
        color: #ccc;
      }
      

      我认为您可以使用"after" pseudo selector 通过模拟底部边框来实现您想要的效果。由于不能直接修改边框的长度。

      Here's a working example

      【讨论】:

        猜你喜欢
        • 2023-02-10
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        • 2014-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多