How to position the header at the top using CSS sticky layout

How to position the header at the top using CSS sticky layout

Application scenarios:

One of the new requirements is to do a questionnaire survey, which will inevitably involve many questions, and multiple people need to score under one question. This phenomenon will occur when sliding on the mobile phone, and you will forget the question when you swipe up. Therefore, after calculating a certain distance, the title needs to be positioned. Make the title that slides to the area always fixed at the top to facilitate scoring.

Solution:

1. The first thing that comes to mind is the fixed layout, which means that the title is fixed at the top of the screen when it reaches a certain distance. (It can be done, but the process is not very smooth)
2. Sticky layout

Sticky can meet this requirement very well, but compatibility (compatible with IE) needs to be considered.

Sticky implementation ideas:

1. First, you need to record the position of the slider and listen for scroll events:

window.addEventListener("scroll", this.handleScroll);

Remember to remove this event before the component is destroyed beforeDestroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  }

2. Calculate the height of each question’s title from the top of the screen and form an array.

// Remember to set a ref for each question. My ref is the id of each question.

-- Vue abbreviation ---
<div
  :ref="question.id"
  v-for="(question, index) of formData.questions"
  :key="index"
>


// To prevent the disorder problem, I also performed a sorting ---- js -----
 this.topPositionArr = [];
  for (let key in this.$refs) {
      for (let idx in this.$refs[key]) {
        this.topPositionArr.push(this.$refs[key][idx].offsetTop);
      }
    }
    this.topPositionArr = [...new Set(this.topPositionArr)];
    this.topPositionArrtopPositionArr = this.topPositionArr.sort(
      (a, b) => {
        return a - b;
      }
    );        

3. Compare the distance monitored by the slide with the obtained title, and select the located title.

// Compatible with different browsers. Different browsers have different ways of obtaining offsetTop. handleScroll() {
    this.scrollTop = document.documentElement.scrollTop
  ? document.documentElement.scrollTop
  : document.body.scrollTop;
  
let scrollTop = this.scrollTop;
// The scrollTop of the first question remains unchanged, and the scrollTop of the second question changes this.topPositionArr.forEach((item, idx) => {
  if (idx > 1) {
    this.scrollTop = scrollTop + 220;
  }
  //Why do this? Because IE does not support sticky layout, we need a plug-in to assist if (this.scrollTop > item) {
    let elements = document.getElementById(`sticky${idx}`);
    stickyfill.add(elements);
  }
});
}

4. IE compatibility issues

Because IE does not support sticky layout, we need the wheel stickyfill

npm install stickyfill 
yarn add stickyfill

---vue---
The header div tag needs to be given a dynamic id to indicate uniqueness

<div
  :id="`sticky${question.index}`"
  class="isSticky"
>

Single file import

Import it into the page you need:
<script src="path/to/stickyfill.min.js"></script>
var Stickyfill = require("stickyfill");
var stickyfill = Stickyfill();

js file:

For specific methods, see 3 above
let elements = document.getElementById(`sticky${idx}`);
stickyfill.add(elements);

The above can be perfectly realized~~~
PS: If you only need to be compatible with Google, then you don't need to go through so much trouble. You can directly use the stiky layout without the need for wheels--
You can refer to https://www.zhangxinxu.com/wordpress/2018/12/css-position-sticky/Xu Xin's article

Final effect picture:

This is the end of this article about how to use CSS sticky layout to position the header at the top. For more relevant CSS sticky content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Docker case analysis: Building a Redis service

>>:  React's reconciliation algorithm Diffing algorithm strategy detailed explanation

Recommend

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

Example of how to check the capacity of MySQL database table

This article introduces the command statements fo...

How to implement Mysql scheduled tasks under Linux

Assumption: The stored procedure is executed ever...

Example of compiling LNMP in Docker container

Table of contents 1. Project Description 2. Nginx...

About VUE's compilation scope and slot scope slot issues

What are slots? The slot directive is v-slot, whi...

MySQL Series 13 MySQL Replication

Table of contents 1. MySQL replication related co...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

NestJs uses Mongoose to operate MongoDB

I recently started learning the NestJs framework....

Example code for implementing verification code login in SMS API in Node

1. Node server setup + database connection The op...

CSS3 sample code to achieve element arc motion

How to use CSS to control the arc movement of ele...

What are the new CSS :where and :is pseudo-class functions?

What are :is and :where? :is() and :where() are p...

The best way to solve the 1px border on mobile devices (recommended)

When developing for mobile devices, you often enc...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...