JS to achieve the effect of mobile terminal ceiling


Today to write a simple ceiling, for your reference, the specific content is as follows

First of all, list the attributes that need to be used for ceiling
**Scrolltop gets the current scrolling distance (that is, the distance from the box to the top)
Offset top the height of the box from the top
Offset height the height of the box itself
Scroll the distance to scroll

If you want to write a ceiling, you must first understand these attributes (of course, it is also very simple, I believe you will have some harvest after reading it)

Write according to the ideas in the picture:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">


      width: 100%;
      height: 40px;
      background: lightgreen;
      text-align: center;
      line-height: 40px;
      height: 1000px;
      background: lightyellow;

      position: fixed;

    < div > I'm going to be on top

    const head = document.querySelector('.header');
      // console.log ( document.documentElement.offsetTop )// 0 HTML distance from top
      // console.log ( document.querySelector ('. Header'). OffsetHeight) // 40 height of the red box
      // console.log ( window.scrollY )// rolling distance
      if(window.scrollY > head.offsetHeight){

Please look forward to the effect icon (in production…)

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.