Html ceiling effect, html ceiling
I. HTML
In HTML, you need to give the div an id
<body><div id="head"></div></body>
Ii. CSS
<style> #head { background-color: #989898
; width: 100%; height: 100px; margin-top: 100px; } #head [data-fixed="fixed"]{ position: fixed; top:0; left: 0; margin: 0; }
</style>
3. JS1 and process-oriented
<script> var obj = document.getElementById("head"); var ot = obj.offsetTop; document.onscroll = function () { var st = document.body.scrollTop || document.documentElement.scrollTop; obj.setAttribute("data-fixed",st >= ot?"fixed":"")}</script>
2. Object-oriented 1) Encapsulation Method
/** Encapsulate the ceiling function, which must be implemented in combination with css. * You can also use js to change the style and modify it on your own. */Function ceiling (obj) {var ot = obj. offsetTop; document. onscroll = function () {var st = document. body. scrollTop | document.doc umentElement. scrollTop; obj. setAttribute ("data-fixed", st> = ot? "Fixed ":"");}}
2) Call Method
<Script src = "ceiling. js "> </script> <script> window. onload = function () {/* Get object */var wrap = document. getElementById ("head"); ceiling (wrap)/* call the ceiling function */}; </script>