粘性定位兼容性问题

来源:9-7 表格布局详解

人生的起源

2022-02-17

粘性定位好像不兼容IE,有什么办法让它兼容吗?

写回答

1回答

西门老舅

2022-02-17

你好,不支持黏性定位的,我们都是配合JS去模拟的。

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Document</title>

    <style>

      body {

        height: 3000px;

      }

      div {

        width: 400px;

        height: 30px;

        background: red;

        position: absolute;

        top: 100px;

      }

    </style>

  </head>

  <body>

    <div>aaaaaaaaaaaaa</div>

    <script>

      let div = document.querySelector("div")

      let divTop = div.offsetTop


      window.onscroll = function () {

        if (divTop <= pageYOffset) {

          div.style.position = "fixed"

          div.style.top = 0

        } else {

          div.style.position = "absolute"

          div.style.top = divTop + "px"

        }

      }

    </script>

  </body>

</html>



0
1
人生的起源
非常感谢!
2022-02-17
共1条回复

前端内功修炼 5大主流布局系统进阶

前端内功修炼:5大主流布局系统进阶

758 学习 · 231 问题

查看课程