自学内容网 自学内容网

CSS滑动门与粘性定位

滑动门

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

具体实现

<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>
*{
  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;
}

粘性定位

具体实现

<!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元素仅在其父元素内生效

原文地址:https://blog.csdn.net/2301_80241120/article/details/144351758

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!