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元素仅在其父元素内生效
相关推荐
Kagol35 分钟前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路2 小时前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide2 小时前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter2 小时前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸3 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000004 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉4 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
兆子龙4 小时前
从高阶函数到 Hooks:React 如何减轻开发者的心智负担(含 Demo + ahooks 推荐)
前端
狗胜4 小时前
测试文章 - API抓取
前端