Create the top navigation, and the navigation bar flashes as the page scrolls down

Hello everyone, I"m going to create a top navigation because for some reason I have to use absolute positioning. But when the scroll bar goes down, the navigation bar will flash very tired. The code is as follows:

< html >
< head >

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<style>
     body, html {
            padding:0;
            margin: 0;
            width: 100%;
            height: 100%;
            position: relative;
        }
  -sharptop {
            position: absolute;
            left:0;
            top:0;
            height: 20px;
            width: 100%;
            background-color: red;
        }
        -sharpbody {
            width: 100%;
            height: 150%;
            background-color: yellow;
        }
</style>

< / head >
< body >
< div id= "top" > < / div >
< div id= "body" > < / div >
< / body >

< script >
var doc = document;
var Dtop = doc.getElementById ("top");
var dBody = doc.getElementById (" body");

function getScrollTop () {

var scrollTop=0;
if(document.documentElement&&document.documentElement.scrollTop)
{
    scrollTop=document.documentElement.scrollTop;
}
else if(document.body)
{
    scrollTop=document.body.scrollTop;
}
return scrollTop;

}

window.addEventListener ("scroll", function (e) {

)
var e = window.event || e;
var scrollTop = getScrollTop();
Dtop.style.top = scrollTop + "px"

})

< / script >
< / html >

find a solution, thank you

Nov.29,2021

because the trigger speed of scroll is faster than the rendering speed, for example, when you scroll down 100, the event triggers, changes the value of top, and the page renders, and then when you go down 100, this speed exceeds the speed of rendering calculation, and the rendering is not completed yet, and the frame drop occurs

.

the solution is to do it directly with css. Change top's position: absolute to position: fixed , and then remove the bound event
. This is a solution, if there is any other need, say.

< hr > < H2 > add < / H2 >

Sorry I didn't notice the need to use absolute positioning before
if you have to use absolute positioning, you can try requestAnimationFrame
the reason for dropping frames is the lack of Synchronize between the scroll event and rendering, then there is no need for the scroll event, and the requestAnimationFrame
requestAnimationFrame method will be called before each redraw, so that there will be no Synchronize performance
performance. You can take a look at the last reference article
the compatibility query is also found in the later reference article

  Easy CSS fix for fixed positioning on Android 2.2 and 2.3  

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3785b-4d73b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3785b-4d73b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?