如何在HTML中为多个子页面添加一个统一的页眉?
P粉293341969
P粉293341969 2023-09-03 12:51:38
[CSS3讨论组]
<p>我正在开发一个导航栏系统,并想知道如何使子页面更改,但原始页面的标题仍然存在。(我正在使用repl.it,提前告知)。</p> <p>我还没有在文件夹中放置任何内容。 我还希望当我悬停在具有“active”类(灰色的那个)的项目上时,它不会改变颜色。</p> <p> <pre class="brush:css;toolbar:false;">@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@700&amp;display=swap'); html body{ background: #0E1212; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: none; font-family: 'Roboto Mono', monospace; } li { float: left; } li a { display: block; color: #DBDBDB; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { color: #682AE9; animation-name: example; animation-duration: 0.5s; animation-iteration-count: 1; animation-fill-mode: forwards; } .active { color: #808080; } @keyframes example { 0% {color: #DBDBDB;} 100% {color: #622cd8;) }</pre> <pre class="brush:html;toolbar:false;">&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;link rel="stylesheet" href="home.css"&gt; &lt;script src='script.js'&gt; &lt;/script&gt; &lt;title&gt;Home&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;/head&gt; &lt;body&gt; &lt;ul id='menu'&gt; &lt;li&gt;&lt;a class="active" href="#home" id="home"&gt;.home()&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a class="inactive" href="#news"&gt;.about()&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a class="inactive" href="#contact"&gt;.stuff()&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a class="inactive" href="#about"&gt;.apply()&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt;</pre> </p>
P粉293341969
P粉293341969

全部回复(1)
P粉642436282

回答你问题中关于保持激活类在悬停时颜色不变的部分,我所做的就是创建了另一个 @keyframe ,使得在 0%100% 时它们都保持灰色。然后我使用 transition: 0.5s; 来使颜色变化的动画过渡平滑。

    @import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@700&display=swap');
html body {
  background: #0E1212;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: none;
  font-family: 'Roboto Mono', monospace;
}

li {
  float: left;
}

li a {
  display: block;
  color: #DBDBDB;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  color: #682AE9;
  transition: 0.5s;
  animation-name: example;
  animation-duration: 0.5s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

.active {
  color: #808080;
}

.active:hover {
  color: #808080;
}

@keyframes .active {
  0% {
    color: #808080;
  }
  100% {
    color: #808080;
    )
  }
 
  @keyframes example {
    0% {
      color: #DBDBDB;
    }
    100% {
      color: #622cd8;
      )
    }
<!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="home.css">
  <script src='script.js'>
  </script>
  <title>Home</title>
</head>

<body>



  </head>

  <body>

    <ul id='menu'>
      <li><a class="active" href="#home" id="home">.home()</a></li>
      <li><a class="inactive" href="#news">.about()</a></li>
      <li><a class="inactive" href="#contact">.stuff()</a></li>
      <li><a class="inactive" href="#about">.apply()</a></li>
    </ul>

  </body>

</html>

如你所见,当你悬停在第一个链接上(即激活状态),它仍然是灰色的,其他链接在 0.5s 的时间内变为紫色。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号