Html ceiling effect, html ceiling

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.