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元素仅在其父元素内生效
相关推荐
CharlesYu0110 小时前
前端性能优化的第一性原理,是不断缩短“用户发起意图 → 获得可用结果”之间的时间
前端
平头哥技术团队10 小时前
Day 21 _ 页内锚点_给每段起个 id,目录写 href=_#id_,点一下页面就滚到那一段
前端·html·html5
子兮曰12 小时前
Bun v1.4.1 深度解析:从 Zig 到 Rust,一场 11 天、64 个 AI 代理的语言迁徙
前端·后端·bun
人民广场吃泡面12 小时前
什么是AI Agent?它又能给前端带来哪些效率提升?
前端·人工智能
中科三方12 小时前
两家域名注册商资质被ICANN终止:企业域名资产安全再受关注
前端·网络·安全·域名
bug总结12 小时前
uniapp vue3全局方法注册使用
前端·javascript·uni-app
华无丽言13 小时前
如何在宜搭中实现获取子表中的字段值赋值到父表中?
前端·javascript·低代码
IT_陈寒13 小时前
Vue的computed属性竟然坑了我一把
前端·人工智能·后端
威斯软科的老司机14 小时前
通俗讲解 CNN 图像识别、向量 Embedding、Softmax 概率计算这三块的简化原理
前端·人工智能·ui·数字孪生