CSS滑动门与粘性定位

滑动门

大家在网页中经常会见到这样一种导航效果,因为使用频率广泛,所以广大的程序员给它起了一个名字,叫做滑动门.在学习滑动门之前,首先你要了解什么是滑动门

具体实现

html 复制代码
<div class="slide">
  <ul>
    <li>
      <a href="#">手机</a>
      <span class="iconfont icon-jiantou_liebiaoxiangyou"></span>
      <div class="slide-list">小米1</div>
    </li>
    <li>
      <a href="#">电视</a>
      <span class="iconfont icon-jiantou_liebiaoxiangyou"></span>
      <div class="slide-list">小米2</div>
    </li>
  </ul>
</div>
html 复制代码
*{
  margin: 0;
  padding: 0;
}
.slide {
  width: 234px;
  height: 420px;
  background-color: rgba(105, 101, 101, .6);
  padding: 20px 0;
  position: relative;
  margin: 100px;
}
.slide ul{
  list-style: none;
}
.slide ul li{
  width: 100%;
  height: 42px;
  line-height: 42px;
  text-align: left;
  padding-left: 30px;
  box-sizing: border-box;
}
.slide ul li:hover{
  background:#ff6700;
}
.slide ul li a{
  display: inline-block;
  text-decoration: none;
  color: #fff;
}
.slide-list {
  width: 992px;
  height: 460px;
  background-color: #fff;
  border: 1px solid #e0e0e0;
  border-left: none;
  box-shadow: 0 8px 16px rgba(0, 0, 0, .18);
  box-sizing: border-box;
  position: absolute;
  top: 0;
  left: 234px;
  display: none;
}
.slide li:hover>.slide-list {
  display: block;
}
.iconfont{
  display: inline-block;
  font-size: 24px;
  color: #fff;
  position: absolute;
  right: 10px;
}

粘性定位

具体实现

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
     * {
      margin: 0;
      padding: 0;
     }
    .ad {
      width: 100%;
      background-color: #ff6524;
     }
    .ad img{
      display: block;
      width: 1190px;
      height: 100px;
      margin: 0 auto;
     }
    .wrap{
      width: 80%;
      margin: 0 auto;
     }
    .head {
      width: 100%;
      height: 50px;
      line-height: 50px;
      position: sticky;
      top: 0px;
     }
    .head ul {
      list-style: none;
     }
    .head ul li {
      float: left;
      padding: 0 10px;
     }
    h3 {
      height: 300px;
     }
  </style>
</head>
<body>
  <div class="container">
    <div class="ad">
      <img src="./ad.png" alt="">
    </div>
    <div class="wrap">
      <div class="head">
        <ul>
          <li>导航1</li>
          <li>导航2</li>
          <li>导航3</li>
          <li>导航4</li>
        </ul>
      </div>
      <div class="body">
        <h3>标题1</h3>
        <h3>标题2</h3>
        <h3>标题3</h3>
        <h3>标题4</h3>
        <h3>标题5</h3>
        <h3>标题6</h3>
        <h3>标题7</h3>
      </div>
    </div>
  </div>
</body>
</html>

粘性定位为什么不生效?

  1. 父元素不能overflow:hidden或者overflow:auto属性
  2. 必须指定top、bottom、left、right4个值之一,否则只会处于相对定位
  3. 父元素的高度不能低于 sticky 的高度
  4. sticky元素仅在其父元素内生效
相关推荐
崔庆才丨静觅13 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606114 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了14 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅14 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅14 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅14 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment15 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅15 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊15 小时前
jwt介绍
前端
爱敲代码的小鱼15 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax