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元素仅在其父元素内生效
相关推荐
谙忆10241 小时前
前端图片直传对象存储:OSS/S3 预签名 URL、STS 临时凭证与回调校验
前端
CHNE_TAO_EMSM2 小时前
Android studio 打开文件时自动下载源码
前端·javascript·android studio
一孤程3 小时前
Airtest自动化测试第五篇:小程序与Web测试——跨平台自动化全覆盖
前端·自动化测试·小程序·自动化·测试·airtest
IT_陈寒3 小时前
SpringBoot自动配置不是你以为的那样的智能
前端·人工智能·后端
yume_sibai4 小时前
大屏数据可视化 - 边框红绿呼吸灯实现详解
前端·信息可视化·typescript
竹林8184 小时前
从 ethers.js 迁移到 Viem:一个签名验证 Bug 让我彻底放弃旧爱
javascript
Hyyy4 小时前
很多Desktop都在上的Computer Use是什么
前端·llm
慢功夫4 小时前
开篇:VS Code 为什么不是一个普通 React App
前端·visual studio code
你挚爱的强哥4 小时前
Vue2 实现 1.5s 十连击监听(连续点击若干次),封装通用可复用点击检测工具,不用 data!Vue 封装通用连击监听方法,支持自定义时长与点击次数
前端·javascript·vue.js
半个落月5 小时前
用 LangChain.js 手写一个能读写文件和执行命令的 Mini Cursor
javascript·人工智能·后端