网页伸缩式导航栏(附源码)

网页伸缩式导航栏

效果图如下:
请添加图片描述
文件目录:
在这里插入图片描述
html代码:
index.html

<!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">
  <title>伸缩式导航栏</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>
  <div class="navbar">
    <input type="checkbox" name="" id="">
    <span></span>
    <span></span>
    <ul>
      <li><a href="javascript:void(0)">点赞</a></li>
      <li><a href="javascript:void(0)">投币</a></li>
      <li><a href="javascript:void(0)">收藏</a></li>
      <li><a href="javascript:void(0)">关注</a></li>
      <li><a href="javascript:void(0)">分享</a></li>
    </ul>
  </div>
</body>
</html>

css代码:
index.css

* {
  /* 页面初始化,清除元素原有的内外边距 */
  margin: 0;
  padding: 0;
}

body {
  height: 100vh;
  /* 网页背景渐变色 */
  background: linear-gradient(#99f, #f99);
}

body,
.navbar,
.navbar ul {
  display: flex;
  justify-content: center;
  align-items: center;
}
.navbar {
  position: relative;
  padding: 20px;
  border-radius: 50px;
  background-color: #fff;
}
.navbar input {
  width: 40px;
  height: 40px;
  /* 透明度为0 */
  opacity: 0;
  cursor: pointer;
}
.navbar span {
  position: absolute;
  left: 25px;
  top: calc(50% - 10px);
  width: 30px;
  height: 4px;
  border-radius: 15px;
  background-color: #999;
  /* span覆盖着复选框,鼠标放到span上点不中复选框 */
  pointer-events: none;
  /* 收回来的时候刚好相反 */
  transition: transform 0.3s ease-in-out, top 0.3x ease-in-out 0.3s;
}
.navbar span:nth-child(3) {
  top: calc(50% + 6px);
}
.navbar ul {
  width: 0;
  /* 隐藏溢出的元素 */
  overflow: hidden;
  /* 将文字一行显示 */
  white-space: nowrap;
  transition: all 0.5s;
}
.navbar ul li {
  list-style: none;
  margin: 0 15px;
}
.navbar ul li a {
  text-decoration: none;
  font-size: 20px;
  font-weight: 700;
  color: #333;
}
.navbar ul li a:hover {
  color: #fb7299;
}
/* :checked是当复选框选中的时候, */
.navbar input:checked ~ ul {
  width: 350px;
}
.navbar input:checked ~ span:nth-child(2) {
  top: calc(50% - 2px);
  transform: rotate(-45deg);
  background-color: #fb7299;
  /* 先做top 然后0.3后执行transform */
  transition: top 0.3s ease-in-out, transform 0.3s ease-in-out 0.3s;
}
.navbar input:checked ~ span:nth-child(3) {
  top: calc(50% - 2px);
  transform: rotate(45deg);
  background-color: #fb7299;
  transition: top 0.3s ease-in-out, transform 0.3s ease-in-out 0.3s;
}

请添加图片描述

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程ID

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值